Class: CommandRunner::CommandInstance

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

Instance Method Summary collapse

Constructor Details

#initialize(default_args, default_timeout, default_environment, allowed_sub_commands, debug_log, split_stderr, options) ⇒ CommandInstance

Returns a new instance of CommandInstance.



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/command_runner.rb', line 208

def initialize(default_args, default_timeout, default_environment, allowed_sub_commands, debug_log, split_stderr, options)
  unless default_args.first.is_a? Array
    raise "First argument must be an array of command line args. Found #{default_args}"
  end

  @default_args = default_args
  @default_timeout = default_timeout
  @default_environment = default_environment
  @allowed_sub_commands = allowed_sub_commands
  @debug_log = debug_log
  @split_stderr = split_stderr
  @options = options
end

Instance Method Details

#run(*args, timeout: nil, environment: {}) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/command_runner.rb', line 222

def run(*args, timeout: nil, environment: {})
  args_list = *args

  if !args_list.nil? && !args_list.empty? && args_list.first.is_a?(Array)
    if args_list.length > 1
      raise "Unsupported args list length: #{args_list.length}"
    else
      args_list = args_list.first
    end
  end

  # Check sub command if needed
  if !args_list.nil? && !args_list.empty? &&
      !@allowed_sub_commands.empty? && !@allowed_sub_commands.include?(args_list.first)
    raise "Illegal sub command '#{args_list.first}'. Expected #{allowed_sub_commands} (#{allowed_sub_commands.include?(args_list.first)})"
  end

  full_args = @default_args.dup
  full_args[0] += args_list.map {|arg| arg.to_s }
  CommandRunner.run(*full_args,
                    timeout: (timeout || @default_timeout),
                    environment: @default_environment.merge(environment),
                    debug_log: @debug_log,
                    split_stderr: @split_stderr,
                    options: @options)
end