Class: Babushka::Shell

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

Defined Under Namespace

Classes: ShellCommandFailed

Constant Summary

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)


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

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



22
23
24
# File 'lib/babushka/shell.rb', line 22

def cmd
  @cmd
end

#envObject (readonly)

Returns the value of attribute env



22
23
24
# File 'lib/babushka/shell.rb', line 22

def env
  @env
end

#inputObject (readonly)

Returns the value of attribute input



22
23
24
# File 'lib/babushka/shell.rb', line 22

def input
  @input
end

#optsObject (readonly)

Returns the value of attribute opts



22
23
24
# File 'lib/babushka/shell.rb', line 22

def opts
  @opts
end

#resultObject (readonly)

Returns the value of attribute result



22
23
24
# File 'lib/babushka/shell.rb', line 22

def result
  @result
end

#stderrObject (readonly)

Returns the value of attribute stderr



22
23
24
# File 'lib/babushka/shell.rb', line 22

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout



22
23
24
# File 'lib/babushka/shell.rb', line 22

def stdout
  @stdout
end

Instance Method Details

#ok?Boolean



41
42
43
# File 'lib/babushka/shell.rb', line 41

def ok?
  result == 0
end

#run(&block) ⇒ Object



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

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