class GS::Command
Command
object for running a command with various options.
-
This is usually not instantiated directly, but rather by way of calling
GS.run
.
Attributes
Public Class Methods
Initializes a new command instance.
-
If no configuration object is passed or is
nil
, the value ofGS.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 ofGS.configuration.logger
is used as the logger object
@param [Configuration] configuration the configuration object @param [Hash] options the key-value pairs of command options @param [Logger] logger the logger object
# 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
Public Instance Methods
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
.
@param name [String] the name of the option @param value [Object] the value of the option
# File lib/gs-ruby/command.rb, line 43 def option(name, value = nil) options[name] = value end
Executes the command with the specified input.
@param [String] inputs the input arguments to the command
@return [Process::Status] describing the result of running the command
# 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
Private Instance Methods
# File lib/gs-ruby/command.rb, line 67 def build_switches options .map { |(n, v)| v ? "-s#{n}=#{v}" : "-d#{n}" } .join(' ') end