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, last_output_max: 200, **kwargs) ⇒ Command

Returns a new instance of Command.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shellfold.rb', line 15

def initialize(*args, desc: nil,
                      out: $stdout,
                      last_output_max: 200, **kwargs)
  super()

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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#descObject (readonly)

Returns the value of attribute desc.



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

def desc
  @desc
end

#outObject (readonly)

Returns the value of attribute out.



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

def out
  @out
end

#out_barObject (readonly)

Returns the value of attribute out_bar.



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

def out_bar
  @out_bar
end

Instance Method Details

#run(ignore_failure: true) ⇒ Object



31
32
33
34
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
# File 'lib/shellfold.rb', line 31

def run(ignore_failure: true)
  running!
  write_out{"#{desc}"}

  thr = Thread.new do
    loop do
      sleep 10
      break unless running?
      write_out{' '} if not @been_here.tap{@been_here = true}
      write_out{'.'}
    end
  end

  on_command_finish = proc do
    if not command.status.success?
      write_out{" [FAILED: #{command.status.inspect}]"}
      if ignore_failure
        write_out{"\n"}
      else
        msg = ["# COMMAND: #{command.command}\n",
               "# LAST OUTPUT BEGIN:\n",
               *[*command.stdout.lines,
                 *command.stderr.lines].reverse[0...@last_output_max].reverse,
               "# LAST OUTPUT END\n"].join
        write_out{"\n#{msg}"}
      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
    thr.kill
  end

  command
end

#run!Object



27
28
29
# File 'lib/shellfold.rb', line 27

def run!
  run(ignore_failure: false)
end

#running?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/shellfold.rb', line 75

def running?
  synchronize{ @running }
end