Class: OctocatalogDiff::Util::ScriptRunner
- Inherits:
-
Object
- Object
- OctocatalogDiff::Util::ScriptRunner
- Defined in:
- lib/octocatalog-diff/util/scriptrunner.rb
Overview
This is a utility class to execute a built-in script.
Defined Under Namespace
Classes: ScriptException
Instance Attribute Summary collapse
-
#exitcode ⇒ Object
readonly
Returns the value of attribute exitcode.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#script ⇒ Object
readonly
Returns the value of attribute script.
-
#script_src ⇒ Object
readonly
Returns the value of attribute script_src.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ ScriptRunner
constructor
Create the object - the object is a configured script, which can be executed multiple times with different environment varibles.
-
#output ⇒ String
All output from the latest execution of the command.
-
#run(opts = {}) ⇒ Object
Execute the script from a given working directory, with additional environment variables specified in the options hash.
Constructor Details
#initialize(opts = {}) ⇒ ScriptRunner
Create the object - the object is a configured script, which can be executed multiple times with different environment varibles.
25 26 27 28 29 30 31 32 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 25 def initialize(opts = {}) @logger = opts[:logger] @script_src = find_script(opts.fetch(:default_script), opts[:override_script_path]) @script = temp_script(@script_src) @stdout = nil @stderr = nil @exitcode = nil end |
Instance Attribute Details
#exitcode ⇒ Object (readonly)
Returns the value of attribute exitcode.
16 17 18 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 16 def exitcode @exitcode end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
16 17 18 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 16 def logger @logger end |
#script ⇒ Object (readonly)
Returns the value of attribute script.
16 17 18 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 16 def script @script end |
#script_src ⇒ Object (readonly)
Returns the value of attribute script_src.
16 17 18 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 16 def script_src @script_src end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
16 17 18 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 16 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
16 17 18 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 16 def stdout @stdout end |
Instance Method Details
#output ⇒ String
All output from the latest execution of the command.
68 69 70 71 72 73 74 75 76 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 68 def output return if @exitcode.nil? [ 'STDOUT:', @stdout.split(/\n/).map { |line| " #{line}" }, 'STDERR:', @stderr.split(/\n/).map { |line| " #{line}" } ].flatten.compact.join("\n") end |
#run(opts = {}) ⇒ Object
Execute the script from a given working directory, with additional environment variables specified in the options hash.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 42 def run(opts = {}) working_dir = opts.fetch(:working_dir) assert_directory_exists(working_dir) argv = opts.fetch(:argv, []) logger = opts[:logger] || @logger pass_env_vars = [opts[:pass_env_vars], 'HOME', 'PATH'].flatten.compact env = opts.select { |k, _v| k.is_a?(String) } pass_env_vars.each { |var| env[var] ||= ENV[var] } env['PWD'] = working_dir cmdline = [script, argv].flatten.compact.map { |x| Shellwords.escape(x) }.join(' ') log(:debug, "Execute: #{cmdline}", opts[:logger]) @stdout, @stderr, status = Open3.capture3(env, cmdline, unsetenv_others: true, chdir: working_dir) @exitcode = status.exitstatus @stderr.split(/\n/).select { |line| line =~ /\S/ }.each { |line| log(:debug, "STDERR: #{line}", logger) } log(:debug, "Exit status: #{@exitcode}", logger) return @stdout if @exitcode.zero? raise ScriptException, output end |