Class: OctocatalogDiff::Util::ScriptRunner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ScriptRunner

Create the object - the object is a configured script, which can be executed multiple times with different environment varibles.

Parameters:

  • opts (Hash) (defaults to: {})

    Options hash opts (Required) Path to script, relative to ‘scripts` directory opts (Optional) Logger object opts (Optional) Directory where a similarly-named script MAY exist



27
28
29
30
31
32
33
34
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 27

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

#exitcodeObject (readonly)

Returns the value of attribute exitcode.



18
19
20
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 18

def exitcode
  @exitcode
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 18

def logger
  @logger
end

#scriptObject (readonly)

Returns the value of attribute script.



18
19
20
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 18

def script
  @script
end

#script_srcObject (readonly)

Returns the value of attribute script_src.



18
19
20
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 18

def script_src
  @script_src
end

#stderrObject (readonly)

Returns the value of attribute stderr.



18
19
20
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 18

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



18
19
20
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 18

def stdout
  @stdout
end

Instance Method Details

#outputString

All output from the latest execution of the command.

Returns:

  • (String)

    Combined output of STDOUT and STDERR



70
71
72
73
74
75
76
77
78
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 70

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.

Parameters:

  • opts (Hash) (defaults to: {})

    Options hash opts (Required) Directory where script is to be executed opts (Optional Array) Command line arguments opts (Optional Array) Environment variables to pass (default: HOME, PATH) opts (Optional) Environment variable

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/octocatalog-diff/util/scriptrunner.rb', line 44

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