Module: VagrantK8s::CommandRunner

Defined in:
lib/vagrant-k8s/command_runner.rb

Defined Under Namespace

Classes: CommandError

Class Method Summary collapse

Class Method Details

.run(ui, command, chdir: nil, tolerate: nil, tolerate_message: nil) ⇒ Object

tolerate: optional Regexp. When the command fails but its stderr matches it, the failure is treated as a benign no-op (e.g. kubectl wait finding no matching resources) — logged as detail instead of raising. tolerate_message: friendly text to log in that case instead of echoing the raw (and alarming) stderr.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant-k8s/command_runner.rb', line 32

def run(ui, command, chdir: nil, tolerate: nil, tolerate_message: nil)
  ui.detail("Executing: #{command.join(' ')}")
  stdout, stderr, status = Open3.capture3(*command, chdir: chdir)
  ui.detail(stdout.rstrip) unless stdout.empty?

  if !status.success? && tolerate && stderr.match?(tolerate)
    note = tolerate_message || stderr.rstrip
    ui.detail(note) unless note.to_s.empty?
    return stdout
  end

  ui.error(stderr.rstrip) unless stderr.empty?
  raise CommandError, "Command failed (exit #{status.exitstatus}): #{command.first}" unless status.success?

  stdout
rescue Errno::ENOENT
  raise CommandError, "Executable not found: #{command.first}"
end