Class: Shellfold::Command

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/shellfold.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, desc: nil, out: $stdout, live_log: false, log_failure: false, last_output_max: 200, **kwargs) ⇒ Command

Returns a new instance of Command.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



10
11
12
# File 'lib/shellfold.rb', line 10

def command
  @command
end

#descObject (readonly)

Returns the value of attribute desc.



11
12
13
# File 'lib/shellfold.rb', line 11

def desc
  @desc
end

#last_output_maxObject (readonly)

Returns the value of attribute last_output_max.



15
16
17
# File 'lib/shellfold.rb', line 15

def last_output_max
  @last_output_max
end

#live_logObject (readonly)

Returns the value of attribute live_log.



13
14
15
# File 'lib/shellfold.rb', line 13

def live_log
  @live_log
end

#log_failureObject (readonly)

Returns the value of attribute log_failure.



14
15
16
# File 'lib/shellfold.rb', line 14

def log_failure
  @log_failure
end

#outObject (readonly)

Returns the value of attribute out.



12
13
14
# File 'lib/shellfold.rb', line 12

def out
  @out
end

Instance Method Details

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/shellfold.rb', line 87

def running?
  synchronize{ @running }
end