Module: AwesomeSpawn

Extended by:
AwesomeSpawn
Included in:
AwesomeSpawn
Defined in:
lib/awesome_spawn.rb,
lib/awesome_spawn/version.rb,
lib/awesome_spawn/command_result.rb,
lib/awesome_spawn/no_such_file_error.rb,
lib/awesome_spawn/command_result_error.rb

Defined Under Namespace

Classes: CommandResult, CommandResultError, NoSuchFileError

Constant Summary collapse

VERSION =
"1.1.1"

Instance Method Summary collapse

Instance Method Details

#build_command_line(command, params = nil) ⇒ String

Build the full command line.

Parameters:

  • command (String)

    The command to run

  • params (Hash, Array) (defaults to: nil)

    Optional command line parameters. They can be passed as a Hash or associative Array. The values are sanitized to prevent command line injection. Keys as symbols are prefixed with --, and _ is replaced with -.

    • {:key => "value"} generates --key value
    • {"--key" => "value"} generates --key value
    • {:key= => "value"} generates --key=value
    • {"--key=" => "value"} generates --key=value
    • {:key_name => "value"} generates --key-name value
    • {:key => nil} generates --key
    • {"-f" => ["file1", "file2"]} generates -f file1 file2
    • {nil => ["file1", "file2"]} generates file1 file2

Returns:

  • (String)

    The full command line



124
125
126
127
# File 'lib/awesome_spawn.rb', line 124

def build_command_line(command, params = nil)
  return command.to_s if params.nil? || params.empty?
  "#{command} #{assemble_params(sanitize(params))}"
end

#run(command, options = {}) ⇒ CommandResult

Execute command synchronously via Kernel.spawn and gather the output stream, error stream, and exit status in a CommandResult.

Examples:

With normal output

result = AwesomeSpawn.run('echo Hi')
# => #<AwesomeSpawn::CommandResult:0x007f9d1d197320 @exit_status=0>
result.output       # => "Hi\n"
result.error        # => ""
result.exit_status  # => 0

With error output as well

result = AwesomeSpawn.run('echo Hi; echo "Hi2" 1>&2')
# => <AwesomeSpawn::CommandResult:0x007ff64b98d930 @exit_status=0>
result.output       # => "Hi\n"
result.error        # => "Hi2\n"
result.exit_status  # => 0

With exit status that is not 0

result = AwesomeSpawn.run('false')
#<AwesomeSpawn::CommandResult:0x007ff64b971410 @exit_status=1>
result.exit_status  # => 1

With parameters sanitized

result = AwesomeSpawn.run('echo', :params => {:out => "; rm /some/file"})
# => #<AwesomeSpawn::CommandResult:0x007ff64baa6650 @exit_status=0>
result.command_line
# => "echo --out \\;\\ rm\\ /some/file"

With data to be passed on stdin

result = AwesomeSpawn.run('cat', :in_data => "line1\nline2")
=> #<AwesomeSpawn::CommandResult:0x007fff05b0ab10 @exit_status=0>
result.output
=> "line1\nline2"

Parameters:

  • command (String)

    The command to run

  • options (Hash) (defaults to: {})

    The options for running the command. Also accepts any option that can be passed to Kernel.spawn, except :out and :err.

Options Hash (options):

  • :params (Hash, Array)

    The command line parameters. See #build_command_line for how to specify params.

  • :in_data (String)

    Data to be passed on stdin. If this option is specified you cannot specify :in.

Returns:

  • (CommandResult)

    the output stream, error stream, and exit status

Raises:

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/awesome_spawn.rb', line 56

def run(command, options = {})
  raise ArgumentError, "options cannot contain :out" if options.include?(:out)
  raise ArgumentError, "options cannot contain :err" if options.include?(:err)
  raise ArgumentError, "options cannot contain :in if :in_data is specified" if options.include?(:in) && options.include?(:in_data)
  options = options.dup
  params  = options.delete(:params)
  in_data = options.delete(:in_data)

  output        = ""
  error         = ""
  status        = nil
  command_line  = build_command_line(command, params)

  begin
    output, error = launch(command_line, in_data, options)
    status = exitstatus
  ensure
    output ||= ""
    error  ||= ""
    self.exitstatus = nil
  end
rescue Errno::ENOENT => err
  raise NoSuchFileError.new(err.message) if NoSuchFileError.detected?(err.message)
  raise
else
  CommandResult.new(command_line, output, error, status)
end

#run!(command, options = {}) ⇒ CommandResult

Same as #run, additionally raising a CommandResultError if the exit status is not 0.

Examples:

With exit status that is not 0

error = AwesomeSpawn.run!('false') rescue $!
# => #<AwesomeSpawn::CommandResultError: false exit code: 1>
error.message # => false exit code: 1
error.result  # => #<AwesomeSpawn::CommandResult:0x007ff64ba08018 @exit_status=1>

Returns:

  • (CommandResult)

    the output stream, error stream, and exit status

Raises:



95
96
97
98
99
100
101
102
103
104
# File 'lib/awesome_spawn.rb', line 95

def run!(command, options = {})
  command_result = run(command, options)

  if command_result.exit_status != 0
    message = "#{command} exit code: #{command_result.exit_status}"
    raise CommandResultError.new(message, command_result)
  end

  command_result
end