class Command

Basic class for wrapping shell execution

Attributes

working_dir[RW]

Public Class Methods

from_file(_file_name) click to toggle source
# File lib/straight_line/common/command.rb, line 19
def self.from_file(_file_name); end
new(command, args = []) click to toggle source
# File lib/straight_line/common/command.rb, line 6
def initialize(command, args = [])
  @command = command
  @args = args || []
  @working_dir = Dir.pwd
  @sub_commands = []
end

Public Instance Methods

arg(argument, *args) click to toggle source
# File lib/straight_line/common/command.rb, line 13
def arg(argument, *args)
  @args << argument
  @args += args if args
  self
end
run(return_stderr = false) click to toggle source
# File lib/straight_line/common/command.rb, line 21
def run(return_stderr = false)
  Dir.chdir working_dir do
    command_with_params = "#{@command} #{@args.join ' '}"

    if return_stderr
      res, status = Open3.capture2e command_with_params
    else
      res, stderr, status = Open3.capture3(command_with_params)
    end
    unless status.exitstatus.zero?
      output = return_stderr ? res : "#{res}\n#{stderr}"
      raise ShellError, %(Command `#{command_with_params}` exited with
        status code: #{status.exitstatus}. Command outputted:\n #{output})
    end

    sub_res = run_sub_commands return_stderr

    res + "\n" + sub_res
  end
end
run_sub_commands(return_stderr) click to toggle source
# File lib/straight_line/common/command.rb, line 42
def run_sub_commands(return_stderr)
  sub_res = ''
  unless @sub_commands.empty?
    sub_res = @sub_commands.map do |sub_command|
      sub_command.run return_stderr
    end.join("\n")
  end
  sub_res
end
sub_command(command) click to toggle source
# File lib/straight_line/common/command.rb, line 52
def sub_command(command)
  unless command.is_a? Command
    raise ArgumentError, 'command must be of type straight_line/common/command'
  end
  @sub_commands << command
end