Class: Scriptster::ShellCmd

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/scriptster/shellcmd.rb

Overview

Represent an executed shell command.

The command will be executed in the constructor. It runs in the foreground, so your application will block until it’s finished executing. The logs, however, will be printed real-time as the command prints its output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

log, #log, set_file, set_format, set_name, set_verbosity

Constructor Details

#initialize(cmd, opts = {}) ⇒ ShellCmd

Initialise the object and run the command

Parameters:

  • cmd (String)

    The command line to be run.

  • opts (Hash) (defaults to: {})

    Various options of the command.

Options Hash (opts):

  • :show_out (Boolean)

    Care about STDOUT flag.

  • :out_level (Boolean)

    To which log level to print the output [default: :info].

  • :show_err (Boolean)

    Care about STDERR flag.

  • :raise (Boolean)

    Raise on error flag.

  • :tag (String)

    Logger tag (defaults to the first word of the command line).

  • :expect (Integer, Array<Integer>)

    Expected return values.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scriptster/shellcmd.rb', line 56

def initialize(cmd, opts={})
  @out = ""
  @err = ""

  @show_out = false
  @out_level = :info
  @show_err = true
  @raise = true
  @tag = cmd.split[0]
  @expect = 0

  opts.each do |k, v|
    self.instance_variable_set("@#{k.to_s}", v)
  end

  @cmd = cmd
  @status = nil

  run
end

Instance Attribute Details

#errString

The content of the STDERR of the command.

Returns:

  • (String)

    the current value of err



39
40
41
# File 'lib/scriptster/shellcmd.rb', line 39

def err
  @err
end

#outString

The content of the STDOUT of the command.

Returns:

  • (String)

    the current value of out



39
40
41
# File 'lib/scriptster/shellcmd.rb', line 39

def out
  @out
end

#statusProcess::status

The exit status of the command.

Returns:

  • (Process::status)

    the current value of status



39
40
41
# File 'lib/scriptster/shellcmd.rb', line 39

def status
  @status
end