Class: Babushka::Shell

Inherits:
Object show all
Includes:
LogHelpers
Defined in:
lib/babushka/shell.rb

Defined Under Namespace

Classes: ShellCommandFailed

Constant Summary collapse

BUF_SIZE =
1024 * 16

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Constructor Details

#initialize(*cmd) ⇒ Shell

Returns a new instance of Shell.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/babushka/shell.rb', line 22

def initialize *cmd
  @opts = cmd.extract_options!
  raise ArgumentError, "You can't use :spinner and :progress together in Babushka::Shell." if opts[:spinner] && opts[:progress]
  raise ArgumentError, "wrong number of arguments (0 for 1+)" if cmd.empty?
  @env = cmd.first.is_a?(Hash) ? cmd.shift : {}
  @cmd = cmd
  @input = if opts[:input].respond_to?(:read)
    opts[:input]
  elsif !opts[:input].nil?
    StringIO.new(opts[:input])
  end

  @progress = nil
  @spinner_offset = -1
  @should_spin = opts[:spinner] && !Base.task.opt(:debug)
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def cmd
  @cmd
end

#envObject (readonly)

Returns the value of attribute env.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def env
  @env
end

#inputObject (readonly)

Returns the value of attribute input.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def input
  @input
end

#optsObject (readonly)

Returns the value of attribute opts.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def opts
  @opts
end

#resultObject (readonly)

Returns the value of attribute result.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def result
  @result
end

#stderrObject (readonly)

Returns the value of attribute stderr.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



20
21
22
# File 'lib/babushka/shell.rb', line 20

def stdout
  @stdout
end

Instance Method Details

#ok?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/babushka/shell.rb', line 39

def ok?
  result == 0
end

#run(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/babushka/shell.rb', line 43

def run &block
  @stdout, @stderr = '', ''
  @result = invoke
  print "#{" " * (@progress.length + 1)}#{"\b" * (@progress.length + 1)}" unless @progress.nil?

  if block_given?
    yield(self)
  elsif ok?
    stdout.chomp
  else
    raise ShellCommandFailed.new(cmd, stdout, stderr)
  end
end