Module: Moonshot::Shell

Overview

Mixin providing the Thor::Shell methods and other shell execution helpers.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sh_out(cmd, fail: true, stdin: '') ⇒ Object

Run a command, returning stdout. Stderr is suppressed unless the command returns non-zero.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moonshot/shell.rb', line 5

def sh_out(cmd, fail: true, stdin: '')
  r_in, w_in = IO.pipe
  r_out, w_out = IO.pipe
  r_err, w_err = IO.pipe
  w_in.write(stdin)
  w_in.close
  pid = Process.spawn(cmd, in: r_in, out: w_out, err: w_err)
  Process.wait(pid)

  r_in.close
  w_out.close
  w_err.close
  stdout = r_out.read
  r_out.close
  stderr = r_err.read
  r_err.close

  if fail && $CHILD_STATUS.exitstatus != 0
    raise "`#{cmd}` exited #{$CHILD_STATUS.exitstatus}\n" \
         "stdout:\n" \
         "#{stdout}\n" \
         "stderr:\n" \
         "#{stderr}\n"
  end
  stdout
end

Instance Method Details

#sh_step(cmd, args = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/moonshot/shell.rb', line 41

def sh_step(cmd, args = {})
  msg = args.delete(:msg) || cmd
  if msg.length > (terminal_width - 18)
    msg = "#{msg[0..(terminal_width - 22)]}..."
  end
  ilog.start_threaded(msg) do |step|
    out = sh_out(cmd, args)
    yield step, out if block_given?
    step.success
  end
end

#shellObject



33
34
35
# File 'lib/moonshot/shell.rb', line 33

def shell
  @thor_shell ||= Thor::Base.shell.new
end