Class: GS::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/gs-ruby/command.rb

Overview

Command object for running a command with various options.

  • This is usually not instantiated directly, but rather by way of calling GS.run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: nil, options: {}, logger: nil) ⇒ Command

Initializes a new command instance.

  • If no configuration object is passed or is nil, the value of GS.configuration is used as the configuration object

  • Any options passed are merged with GS.configuration.default_options so that any option can be deleted if necessary

  • If no logger object is passed or is nil, the value of GS.configuration.logger is used as the logger object

Parameters:

  • configuration (Configuration)

    the configuration object

  • options (Hash)

    the key-value pairs of command options

  • logger (Logger)

    the logger object


29
30
31
32
33
# File 'lib/gs-ruby/command.rb', line 29

def initialize(configuration: nil, options: {}, logger: nil)
  @configuration = configuration || GS.configuration
  @options = @configuration.default_options.merge(options)
  @logger = logger || @configuration.logger
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration


10
11
12
# File 'lib/gs-ruby/command.rb', line 10

def configuration
  @configuration
end

#loggerObject

Returns the value of attribute logger


13
14
15
# File 'lib/gs-ruby/command.rb', line 13

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options


10
11
12
# File 'lib/gs-ruby/command.rb', line 10

def options
  @options
end

Instance Method Details

#option(name, value = nil) ⇒ Object

Sets a command option with an optional value.

  • If a value is passed, the resulting switch will be in the form -sName=Value. If no value is passed, the resulting switch will be in the form -dName.

Parameters:

  • name (String)

    the name of the option

  • value (Object) (defaults to: nil)

    the value of the option


43
44
45
# File 'lib/gs-ruby/command.rb', line 43

def option(name, value = nil)
  options[name] = value
end

#run(inputs) ⇒ Process::Status

Executes the command with the specified input.

Parameters:

  • inputs (String)

    the input arguments to the command

Returns:

  • (Process::Status)

    describing the result of running the command


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gs-ruby/command.rb', line 52

def run(inputs)
  command = "#{configuration.bin_path} #{build_switches} #{inputs}"

  status = nil
  logger.info(command)
  Open3.popen2e(command) do |_, stdout_and_stderr, wait_thr|
    logger.info(stdout_and_stderr.read)
    status = wait_thr.value
  end

  status
end