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



43
44
45
46
# File 'lib/simple/cli/helpers.rb', line 43

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

Instance Method Details

#check_success!Object



70
71
72
73
# File 'lib/simple/cli/helpers.rb', line 70

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

#runObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/simple/cli/helpers.rb', line 54

def run
  STDERR.puts "> #{self}"
  rv = if @args.length > 1
         system to_s
       else
         system @args.first
       end

  @process_status = $?
  rv
end

#shObject



48
49
50
51
52
# File 'lib/simple/cli/helpers.rb', line 48

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

#success?Boolean



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

def success?
  @process_status.success?
end

#to_sObject

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



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/simple/cli/helpers.rb', line 76

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