Class: Simple::CLI::Helpers::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/cli/helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd, *args) ⇒ Command

Returns a new instance of Command.



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

def initialize(cmd, *args)
  @cmd = cmd
  @args = [cmd] + args
end

Instance Method Details

#check_success!Object



84
85
86
87
# File 'lib/simple/cli/helpers.rb', line 84

def check_success!
  return if @process_status.success?
  raise "#{@cmd} failed with #{@process_status.exitstatus}: #{self}"
end

#runObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/simple/cli/helpers.rb', line 69

def run
  ::Simple::CLI.logger.info "> #{self}"
  if @args.length > 1
    system to_s
  else
    system @args.first
  end
ensure
  @process_status = $?
end

#shObject



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

def sh
  ::Simple::CLI.logger.info "> #{self}"
  stdout_str, @process_status = Open3.capture2(*@args, binmode: true)
  stdout_str
end

#success?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/simple/cli/helpers.rb', line 80

def success?
  @process_status.success?
end

#to_sObject

Returns the command as a single string, escaping things as necessary.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/simple/cli/helpers.rb', line 90

def to_s
  require "shellwords"

  escaped_args = @args.map do |arg|
    escaped = Shellwords.escape(arg)
    next escaped if escaped == arg
    next escaped if arg.include?("'")
    "'#{arg}'"
  end
  escaped_args.join(" ")
end