lib/fulmar/shell.rb in fulmar-shell-1.4.0 vs lib/fulmar/shell.rb in fulmar-shell-1.5.1
- old
+ new
@@ -3,11 +3,11 @@
# 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.4.0'
+ VERSION = '1.5.1'
attr_accessor :debug, :last_output, :last_error, :quiet, :strict
attr_reader :path
def initialize(path = '.', host = 'localhost')
@@ -31,20 +31,22 @@
path = options[:in][0, 1] == '/' ? options[:in] : "#{@path}/#{options[:in]}"
else
path = @path
end
+ options[:error_message] ||= 'Last shell command returned an error.'
+
command.unshift "cd #{path}"
# invoke a login shell?
- shell_command = options[:login] ? 'env -i bash -lc' : 'bash -c'
+ shell_command = options[:login] ? clean_environment : 'bash -c'
if local?
- execute("#{shell_command} '#{escape_for_sh(command.join(' && '))}'")
+ execute("#{shell_command} '#{escape_for_sh(command.join(' && '))}'", options[:error_message])
else
remote_command = escape_for_sh("#{shell_command} '#{escape_for_sh(command.join(' && '))}'")
- execute("ssh #{@host} '#{remote_command}'")
+ execute("ssh #{@host} '#{remote_command}'", options[:error_message])
end
end
def local?
@host == 'localhost'
@@ -54,12 +56,16 @@
@path = local? ? File.expand_path(path) : path
end
protected
+ def clean_environment
+ "env -i HOME=\"#{ENV['HOME']}\" bash -lc"
+ end
+
# Run the command and capture the output
- def execute(command)
+ def execute(command, error_message)
# Ladies and gentleman: More debug, please!
puts command if @debug
stdin, stdout, stderr, wait_thr = Open3.popen3(command)
@@ -73,10 +79,10 @@
@last_error.each { |line| puts line } unless @quiet
if @strict and wait_thr.value != 0
dump_error_message(command)
- fail 'Last shell command returned an error.'
+ fail error_message
end
wait_thr.value == 0
end