Class: GS::Command
- Inherits:
-
Object
- Object
- GS::Command
- 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
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(configuration: nil, options: {}, logger: nil) ⇒ Command
constructor
Initializes a new command instance.
-
#option(name, value = nil) ⇒ Object
Sets a command option with an optional value.
-
#run(inputs) ⇒ Process::Status
Executes the command with the specified input.
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 ofGS.configurationis used as the configuration object -
Any options passed are merged with
GS.configuration.default_optionsso that any option can be deleted if necessary -
If no logger object is passed or is
nil, the value ofGS.configuration.loggeris used as 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 = @configuration..merge() @logger = logger || @configuration.logger end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/gs-ruby/command.rb', line 10 def configuration @configuration end |
#logger ⇒ Object
Returns the value of attribute logger.
13 14 15 |
# File 'lib/gs-ruby/command.rb', line 13 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/gs-ruby/command.rb', line 10 def 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.
43 44 45 |
# File 'lib/gs-ruby/command.rb', line 43 def option(name, value = nil) [name] = value end |
#run(inputs) ⇒ Process::Status
Executes the command with the specified input.
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 |