Class: Garlic::Shell

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/garlic/shell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets) ⇒ Shell

Returns a new instance of Shell.



13
14
15
16
17
# File 'lib/garlic/shell.rb', line 13

def initialize(targets)
  @current_path = '.'
  @targets = targets
  raise "Garlic::Shell requires at least one target" if @targets.empty?
end

Instance Attribute Details

#current_pathObject

Returns the value of attribute current_path.



11
12
13
# File 'lib/garlic/shell.rb', line 11

def current_path
  @current_path
end

Instance Method Details

#process(command) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/garlic/shell.rb', line 43

def process(command)
  if command =~ /^cd (.*)$/
    self.current_path = $1
  else
    @targets.each do |target|
      cd File.join(target.path, current_path) do
        STDOUT << magenta << target.name + ":\n" << clear
        system(command) || STDOUT << red << "command failed\n" << clear
      end
    end
  end
end

#runObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/garlic/shell.rb', line 32

def run
  STDOUT << green << "Garlic interactive session: type shell commands\n" << clear << prompt
  while (command = STDIN.gets) do
    command.strip!.empty? || process(command)
    STDOUT << prompt
  end
rescue Interrupt
ensure
  STDOUT << green << "Garlic interactive session ended\n" << clear
end