class Balmora::Shell
Attributes
exec[RW]
home[R]
status[RW]
user_id[R]
Public Class Methods
factory(state)
click to toggle source
# File lib/balmora/shell.rb, line 29 def self.factory(state) return self.new(state.logger, state.options[:dry]) end
new(logger, dry, home = nil)
click to toggle source
# File lib/balmora/shell.rb, line 33 def initialize(logger, dry, home = nil) @logger = logger @dry = dry @run = ::Object.method(:'`') @system = ::Object.method(:system) @status = Proc.new() { $?.exitstatus } @home = Dir.home() @user_id ||= `id -un; id -gn`.strip().split("\n").join(':') @sudo_stack = [] end
Public Instance Methods
expand(path)
click to toggle source
# File lib/balmora/shell.rb, line 115 def expand(path) if path.start_with?('~') || path.start_with?('/') return ::File.expand_path(path) end return path end
expression(command)
click to toggle source
# File lib/balmora/shell.rb, line 45 def expression(command) Expression.new(command) end
run(command, options = {})
click to toggle source
# File lib/balmora/shell.rb, line 61 def run(command, options = {}) command = sudo() + command shell_command = command. collect() { |part| if part.instance_of?(Expression) next part.command end next Shellwords.escape(part) }. join(' ') dry = false if options[:change] == true && @dry dry = true shell_command = "dry: #{shell_command}" end if options[:verbose] != false @logger.info(shell_command) else @logger.debug(shell_command) end if dry return 0, '' end method = @run if options[:system] == true method = @system end result = method.call(shell_command) status = @status.call() if options[:raise] == true && status != 0 raise Error.new("Failed to execute command: #{shell_command}", status) end return status, result end
run!(command, options = {})
click to toggle source
# File lib/balmora/shell.rb, line 107 def run!(command, options = {}) return run(command, options.merge(raise: true))[1] end
sudo()
click to toggle source
# File lib/balmora/shell.rb, line 49 def sudo() if @sudo_stack.length == 0 return [] end if @sudo_stack[-1] == true return ['sudo'] else return ['sudo', '--user', @sudo_stack[-1]] end end
sudo!(sudo) { || ... }
click to toggle source
# File lib/balmora/shell.rb, line 123 def sudo!(sudo) @sudo_stack.push(sudo) yield ensure @sudo_stack.pop() end
system(command, options = {})
click to toggle source
# File lib/balmora/shell.rb, line 111 def system(command, options = {}) return run(command, options.merge(raise: true, system: true))[1] end