Class: GrateHandle::CLIProcessor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, manager = GoGridManager.new, out = STDOUT) ⇒ CLIProcessor

Returns a new instance of CLIProcessor.



8
9
10
11
12
13
# File 'lib/cli_processor.rb', line 8

def initialize(args, manager=GoGridManager.new, out=STDOUT)
  @args = args
  @extra_params = {}
  @out = out
  @manager = manager
end

Instance Attribute Details

#manager=(value) ⇒ Object (writeonly)

{{{1 Initialization and accessors



6
7
8
# File 'lib/cli_processor.rb', line 6

def manager=(value)
  @manager = value
end

Instance Method Details

#list_actionObject



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

def list_action
  case @args.shift(1).first
  when 'servers'    then print_servers
  when 'images'     then print_images
  when 'ips'        then print_ips
  when 'passwords'  then print_passwords
  when 'help', nil  then print_usage
  else                   print_list_error
  end
end

}}} {{{1 listings print



60
61
62
63
64
# File 'lib/cli_processor.rb', line 60

def print_images
  @manager.list_images.each do |image|
    @out.puts(sprintf("%-5s\t%s", image.id, image.name))
  end
end


72
73
74
75
76
# File 'lib/cli_processor.rb', line 72

def print_ips
  @manager.list_ips.each do |ip|
    @out.puts(sprintf("%-15s\t%-7s\t%s", ip.ip, (ip.public ? "Public" : "Private"), ip.state.name))
  end
end

}}} {{{1 Help Messages



36
37
38
# File 'lib/cli_processor.rb', line 36

def print_list_error
  @out.puts "Unknown command. Please check gh help for the list of available commands"
end


78
79
80
81
82
# File 'lib/cli_processor.rb', line 78

def print_passwords
  @manager.list_passwords.each do |passwd|
    @out.puts(sprintf("%-20s\t%s@%-15s\t%s", passwd.server.name, passwd.username, passwd.server.ip.ip, passwd.password)) if passwd.has_key?('server') # hack against CloudStorage
  end
end


66
67
68
69
70
# File 'lib/cli_processor.rb', line 66

def print_servers
  @manager.list_servers.each do |server|
    @out.puts(sprintf("%-6s\t%-20s\t%-15s\t%s", server.id, server.name, server.ip.ip, server.state.name))
  end
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cli_processor.rb', line 40

def print_usage
  # don't like multilines strings, sorry
  usage = []
  usage << 'usage: gh ACTION ENTITY [ARGS]'
  usage << ''
  usage << 'Note that not all ACTIONs applicable to every ENTITY.'
  usage << 'Available combinations are:'

  actions = []
  actions << ['list servers',   'Shows all servers in all states']
  actions << ['list images',    'Shows all available server images (both legacy and MyGSI)']
  actions << ['list passwords', 'Shows root login credentials for all servers']
  actions << ['list ips',       'Shows all all ips in all states of all types.']

  actions.map! { |action| sprintf("\t%-20s%s", action[0], action[1]) }

  (usage + actions).each { |line| @out.puts line }
end

#processObject

}}} {{{1 Ugly Options processing



16
17
18
19
20
21
22
# File 'lib/cli_processor.rb', line 16

def process
  case @args.shift(1).first
  when 'list'      then list_action
  when 'help', nil then print_usage
  else             @out.puts "unknown action"
  end
end