Class: Fulmar::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/fulmar/shell.rb

Overview

Implements simple access to shell commands

Constant Summary collapse

VERSION =
'1.8.3'
DEFAULT_BUFFER_SIZE =
1000
DEFAULT_OPTIONS =
{
  login: false,
  escape_bundler: false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = '.', host = 'localhost') ⇒ Shell

Returns a new instance of Shell.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fulmar/shell.rb', line 24

def initialize(path = '.', host = 'localhost')
  @host = host.nil? ? 'no_hostname_set' : host
  @path = (path.nil? || path.empty?) ? '.' : path
  @path = File.expand_path(@path) if local?
  reset_output
  @debug = false
  @quiet = true
  @strict = false
  @interactive = false
  @clean_environment = [] # list of things to clean from environment variables
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



14
15
16
# File 'lib/fulmar/shell.rb', line 14

def debug
  @debug
end

#interactiveObject

Returns the value of attribute interactive.



14
15
16
# File 'lib/fulmar/shell.rb', line 14

def interactive
  @interactive
end

#last_errorObject

Returns the value of attribute last_error.



14
15
16
# File 'lib/fulmar/shell.rb', line 14

def last_error
  @last_error
end

#last_outputObject

Returns the value of attribute last_output.



14
15
16
# File 'lib/fulmar/shell.rb', line 14

def last_output
  @last_output
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/fulmar/shell.rb', line 15

def path
  @path
end

#quietObject

Returns the value of attribute quiet.



14
15
16
# File 'lib/fulmar/shell.rb', line 14

def quiet
  @quiet
end

#strictObject

Returns the value of attribute strict.



14
15
16
# File 'lib/fulmar/shell.rb', line 14

def strict
  @strict
end

Instance Method Details

#buffer_size(size) ⇒ Object



78
79
80
# File 'lib/fulmar/shell.rb', line 78

def buffer_size(size)
  reset_output(size)
end

#local?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/fulmar/shell.rb', line 70

def local?
  @host == 'localhost'
end

#run(command, options = DEFAULT_OPTIONS) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fulmar/shell.rb', line 41

def run(command, options = DEFAULT_OPTIONS)
  reset_output(@last_output.max_size)
  command = [command] if command.class == String

  # is a custom path given?
  path = if options[:in]
           # is it absolute?
           (Pathname.new options[:in]).absolute? ? options[:in] : "#{@path}/#{options[:in]}"
         else
           @path
         end

  options[:error_message] ||= 'Last shell command returned an error.'

  command.unshift "cd \"#{path}\""

  # invoke a login shell?
  shell_command = shell_command(options[:login])

  @clean_environment << 'bundler' if options[:escape_bundler]

  if local?
    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}'", options[:error_message])
  end
end