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.



68
69
70
71
# File 'lib/simple/cli/helpers.rb', line 68

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

Instance Method Details

#check_success!Object



94
95
96
97
# File 'lib/simple/cli/helpers.rb', line 94

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

#runObject



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

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

#shObject



73
74
75
76
77
# File 'lib/simple/cli/helpers.rb', line 73

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

#success?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/simple/cli/helpers.rb', line 90

def success?
  @process_status.success?
end

#to_sObject

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/simple/cli/helpers.rb', line 100

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