lib/fulmar/shell.rb in fulmar-shell-1.2.0 vs lib/fulmar/shell.rb in fulmar-shell-1.3.0
- old
+ new
@@ -3,49 +3,62 @@
# This shell is part of the fulmar deployment tools
# it can be used stand-alone, though
module Fulmar
# Implements simple access to shell commands
class Shell
- VERSION = '1.2.0'
+ VERSION = '1.3.0'
attr_accessor :debug, :last_output, :last_error, :quiet
attr_reader :path
- def initialize(path, host = 'localhost')
- @path = File.expand_path((path.nil? || path.empty?) ? '.' : path)
+ def initialize(path = '.', host = 'localhost')
@host = host
+ @path = (path.nil? || path.empty?) ? '.' : path
+ @path = File.expand_path(@path) if local?
@last_output = []
@last_error = []
@debug = false
@quiet = false
+ @environment = {}
end
- def run(command)
+ def run(command, options = {})
command = [command] if command.class == String
- command.unshift "cd #{@path}"
+ # is a custom path given?
+ if options[:in]
+ # is it absolute?
+ path = options[:in][0, 1] == '/' ? options[:in] : "#{@path}/#{options[:in]}"
+ else
+ path = @path
+ end
+ command.unshift "cd #{path}"
+
+ # invoke a login shell?
+ shell_command = options[:login] ? 'env -i bash -lc' : 'bash -c'
+
if local?
- execute("sh -c '#{escape_for_sh(command.join(' && '))}'")
+ execute("#{shell_command} '#{escape_for_sh(command.join(' && '))}'")
else
- remote_command = escape_for_sh('/bin/sh -c \'' + escape_for_sh(command.join(' && ')) + '\'')
+ remote_command = escape_for_sh("#{shell_command} '#{escape_for_sh(command.join(' && '))}'")
execute("ssh #{@host} '#{remote_command}'")
end
end
def local?
@host == 'localhost'
end
def path=(path)
- @path = File.expand_path(path)
+ @path = local? ? File.expand_path(path) : path
end
protected
# Run the command and capture the output
def execute(command)
- # DEBUG, baby!
+ # Ladies and gentleman: More debug, please!
puts command if @debug
stdin, stdout, stderr, wait_thr = Open3.popen3(command)
# Remove annoying newlines at the end