Class: Babushka::Shell
Defined Under Namespace
Classes: ShellCommandFailed
Constant Summary
- BUF_SIZE =
1024 * 16
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
-
#initialize(*cmd) ⇒ Shell
constructor
A new instance of Shell.
- #ok? ⇒ Boolean
- #run(&block) ⇒ Object
Constructor Details
#initialize(*cmd) ⇒ Shell
Returns a new instance of Shell
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. 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
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd
20 21 22 |
# File 'lib/babushka/shell.rb', line 20 def cmd @cmd end |
#env ⇒ Object (readonly)
Returns the value of attribute env
20 21 22 |
# File 'lib/babushka/shell.rb', line 20 def env @env end |
#input ⇒ Object (readonly)
Returns the value of attribute input
20 21 22 |
# File 'lib/babushka/shell.rb', line 20 def input @input end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts
20 21 22 |
# File 'lib/babushka/shell.rb', line 20 def opts @opts end |
#result ⇒ Object (readonly)
Returns the value of attribute result
20 21 22 |
# File 'lib/babushka/shell.rb', line 20 def result @result end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr
20 21 22 |
# File 'lib/babushka/shell.rb', line 20 def stderr @stderr end |
#stdout ⇒ Object (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
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 Logging.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 |