Class: Computer::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/computer/shell.rb

Overview

use nested class for “base” for running commands - why? why not?

Class Method Summary collapse

Class Method Details

.call(cmd) ⇒ Object

use call for the “to-the-metal” command execution



15
16
17
18
# File 'lib/computer/shell.rb', line 15

def self.call( cmd )
  stdout, stderr, status = Open3.capture3( cmd )
  [stdout, stderr, status]
end

.run(cmd) ⇒ Object

use run for “porcelain / high-level” command execution



22
23
24
25
26
27
28
29
30
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
# File 'lib/computer/shell.rb', line 22

def self.run( cmd )
  ## add pwd (print working directory to output?) - why? why not?
  print "---> (shell run) >#{cmd}<..."
  stdout, stderr, status = Open3.capture3( cmd )

  ## todo: add colors (red, green) - why? why not?
  if status.success?
    print " OK"   ## todo/check: use OK (0) - why? why not?
    print "\n"
  else
    print " FAIL (#{status.exitstatus})"
    print "\n"
  end

  unless stdout.empty?
    puts stdout
  end

  unless stderr.empty?
    ## todo/check: or use >2: or &2: or such
    ##  stderr output not always an error (that is, exit status might be 0)
    STDERR.puts "2>"
    STDERR.puts stderr
  end

  if status.success?
    stdout   # return stdout string
  else
    STDERR.puts "!! ERROR: Shell.run >#{cmd}< failed with exit status #{status.exitstatus}:"
    STDERR.puts stderr

    raise ShellError, "Shell.run >#{cmd}< failed with exit status #{status.exitstatus}<: #{stderr}"
  end
end