class Shellfold::Command

Attributes

command[R]
desc[R]
last_output_max[R]
live_log[R]
log_failure[R]
out[R]

Public Class Methods

new(*args, desc: nil, out: $stdout, live_log: false, log_failure: false, last_output_max: 200, **kwargs) click to toggle source
Calls superclass method
# File lib/shellfold.rb, line 17
def initialize(*args, desc: nil,
                      out: $stdout,
                      live_log: false,
                      log_failure: false,
                      last_output_max: 200, **kwargs)
  super()

  kwargs.merge!(live_stderr: out, live_stdout: out) if live_log

  @command = Mixlib::ShellOut.new(*args, **kwargs)
  @desc = desc || command.command
  @out = out
  @live_log = live_log
  @log_failure = log_failure
  @last_output_max = last_output_max
  @running = false
end

Public Instance Methods

run() click to toggle source
# File lib/shellfold.rb, line 35
def run
  running!

  progress_bar_thr = nil

  unless live_log
    write_out{desc}
    progress_bar_thr = Thread.new do
      loop do
        sleep 10
        break unless running?
        write_out do
          [@been_here.tap{@been_here = true} ? nil : ' ', '.'].compact.join
        end
      end
    end
  else
    write_out{desc + "\n"}
  end

  on_command_finish = proc do
    next if live_log
    if not command.status.success?
      write_out{" [FAILED: #{command.status.inspect}]"}
      if log_failure
        msg = ["# COMMAND: #{command.command}\n",
               "# LAST OUTPUT BEGIN:\n",
               *[*command.stdout.lines,
                 *command.stderr.lines].last(last_output_max),
               "# LAST OUTPUT END\n"].join
        write_out{"\n#{msg}"}
      else
        write_out{"\n"}
      end
    else
      write_out{" [DONE]\n"}
    end
  end

  begin
    command.run_command
    stopped!
    on_command_finish.call
  rescue Mixlib::ShellOut::CommandTimeout
    on_command_finish.call
  ensure
    progress_bar_thr.kill if progress_bar_thr
  end

  command
end
running?() click to toggle source
# File lib/shellfold.rb, line 87
def running?
  synchronize{ @running }
end

Private Instance Methods

running!() click to toggle source
# File lib/shellfold.rb, line 97
def running!
  synchronize{ @running = true }
end
stopped!() click to toggle source
# File lib/shellfold.rb, line 101
def stopped!
  synchronize{ @running = false }
end
write_out(&blk) click to toggle source
# File lib/shellfold.rb, line 93
def write_out(&blk)
  synchronize{ out.write(blk.call) }
end