Module: Simple::CLI::Helpers

Defined in:
lib/simple/cli/helpers.rb

Overview

Helpers are mixed into all CLI modules. They implement the following methods, mostly to help with integrating external commands:

  • sys

  • sys!

  • sh!

  • die!

Defined Under Namespace

Classes: Command

Constant Summary collapse

SSH =
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

Instance Method Summary collapse

Instance Method Details

#confirm!(msg) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple/cli/helpers.rb', line 21

def confirm!(msg)
  STDERR.puts <<~MSG
    #{msg}

    Press return to continue, ^C to cancel...
  MSG

  STDIN.gets
rescue Interrupt
  logger.error "Cancelled by user"
  exit 1
end

#die!(msg) ⇒ Object



12
13
14
15
# File 'lib/simple/cli/helpers.rb', line 12

def die!(msg)
  STDERR.puts msg
  exit 1
end

#loggerObject



17
18
19
# File 'lib/simple/cli/helpers.rb', line 17

def logger
  ::Simple::CLI.logger
end

#sh!(cmd, *args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple/cli/helpers.rb', line 45

def sh!(cmd, *args)
  command = Command.new(cmd, *args)
  result = command.sh
  command.check_success!
  first_line, more = result.split("\n", 2)
  if more == ""
    first_line
  else
    result
  end
end

#ssh!(target, command, user: nil) ⇒ Object



41
42
43
# File 'lib/simple/cli/helpers.rb', line 41

def ssh!(target, command, user: nil)
  sys! "#{ssh_command(target, user: user)} #{command}"
end

#ssh_command(host, user: nil) ⇒ Object



36
37
38
39
# File 'lib/simple/cli/helpers.rb', line 36

def ssh_command(host, user: nil)
  host = "#{user}@#{host}" if user
  "#{SSH} #{host}"
end

#sys(cmd, *args) ⇒ Object



57
58
59
60
61
# File 'lib/simple/cli/helpers.rb', line 57

def sys(cmd, *args)
  command = Command.new(cmd, *args)
  command.run
  command.success?
end

#sys!(cmd, *args) ⇒ Object



63
64
65
66
67
68
# File 'lib/simple/cli/helpers.rb', line 63

def sys!(cmd, *args)
  command = Command.new(cmd, *args)
  command.run
  command.check_success!
  true
end