Class: Scripto::RunCommands::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/scripto/run_commands.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, args) ⇒ CommandLine

Returns a new instance of CommandLine.



55
56
57
58
59
60
61
62
63
64
# File 'lib/scripto/run_commands.rb', line 55

def initialize(command, args)
  self.command = command
  self.args = begin
    if args
      args.map(&:to_s)
    else
      []
    end
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



53
54
55
# File 'lib/scripto/run_commands.rb', line 53

def args
  @args
end

#commandObject

Returns the value of attribute command.



53
54
55
# File 'lib/scripto/run_commands.rb', line 53

def command
  @command
end

Instance Method Details

#captureObject



71
72
73
74
75
76
77
78
79
# File 'lib/scripto/run_commands.rb', line 71

def capture
  begin
    captured = `#{self}`
  rescue Errno::ENOENT
    raise RunError, "#{self} failed : ENOENT (No such file or directory)"
  end
  raise!($CHILD_STATUS) if $CHILD_STATUS != 0
  captured
end

#raise!(status) ⇒ Object

Raises:



81
82
83
84
85
86
87
# File 'lib/scripto/run_commands.rb', line 81

def raise!(status)
  if status.termsig == Signal.list["INT"]
    raise "#{self} interrupted"
  end

  raise RunError, "#{self} failed : #{status.to_i / 256}"
end

#runObject



66
67
68
69
# File 'lib/scripto/run_commands.rb', line 66

def run
  system(command, *args)
  raise!($CHILD_STATUS) if $CHILD_STATUS != 0
end

#to_sObject



89
90
91
92
93
94
95
96
# File 'lib/scripto/run_commands.rb', line 89

def to_s
  if !args.empty?
    escaped = args.map { Shellwords.escape(_1) }
    "#{command} #{escaped.join(" ")}"
  else
    command
  end
end