Class: Minfra::Cli::Runner

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/minfra/cli/runner.rb

Direct Known Subclasses

HelmRunner, KubeCtlRunner

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #deprecated, #error, #exit_error, #info, #warn

Constructor Details

#initialize(cmd, exit_on_error: true, runner: :popen, on_output: nil, test: false) ⇒ Runner

Returns a new instance of Runner.



67
68
69
70
71
72
73
# File 'lib/minfra/cli/runner.rb', line 67

def initialize(cmd, exit_on_error: true, runner: :popen, on_output: nil, test: false)
  @cmd = cmd
  @exit_on_error = exit_on_error
  @runner = runner
  @on_output = on_output
  @test = test
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



65
66
67
# File 'lib/minfra/cli/runner.rb', line 65

def cmd
  @cmd
end

#exit_on_errorObject (readonly)

Returns the value of attribute exit_on_error.



65
66
67
# File 'lib/minfra/cli/runner.rb', line 65

def exit_on_error
  @exit_on_error
end

#on_outputObject (readonly)

Returns the value of attribute on_output.



65
66
67
# File 'lib/minfra/cli/runner.rb', line 65

def on_output
  @on_output
end

#runnerObject (readonly)

Returns the value of attribute runner.



65
66
67
# File 'lib/minfra/cli/runner.rb', line 65

def runner
  @runner
end

Class Method Details

.run(cmd, **args) ⇒ Object



61
62
63
# File 'lib/minfra/cli/runner.rb', line 61

def self.run(cmd, **args)
  new(cmd, **args).run
end

Instance Method Details

#runObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/minfra/cli/runner.rb', line 75

def run
  debug("running (#{@runner}): #{@cmd}")
  res = Result.new(on_output:)
  return res if @test
  case @runner
        when :system
          run_system(res)
        when :popen
          run_threaded(res)
        when :exec
          run_exec(res)
        else
          raise "unknown runner #{@runner}"
        end

  if res.error?
    error "command failed: #{@cmd}"
    debug res.stdout
    info res.stderr
  end
  if exit_on_error && res.error?
    info "command exiting on error (#{res.exitstatus})"
    exit 1
  end
  res
end