Class: Morpheus::Cli::Shell

Inherits:
Object
  • Object
show all
Includes:
CliCommand, Term::ANSIColor
Defined in:
lib/morpheus/cli/shell.rb

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initializeShell

Returns a new instance of Shell.



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

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance	
end

Instance Method Details

#get_promptObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/morpheus/cli/shell.rb', line 95

def get_prompt
	input = ''
	while char=STDIN.getch do
		if char == '\n'
			print "\r\n"
			puts "executing..."
			break
		end
		print char
		input << char
	end
	return input
end

#handle(args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/morpheus/cli/shell.rb', line 18

def handle(args)
	usage = "Usage: morpheus shell"
	@command_options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = usage
		opts.on('-a','--account ACCOUNT', "Account Name") do |val|
			@command_options[:account_name] = val
     end
     opts.on('-A','--account-id ID', "Account ID") do |val|
			@command_options[:account_id] = val
     end
		opts.on('-C','--nocolor', "ANSI") do
			@command_options[:nocolor] = true
       Term::ANSIColor::coloring = false
     end
     opts.on( '-h', '--help', "Prints this help" ) do
       puts opts
       exit
     end
	end
	optparse.parse(args)

	history = []
	exit = false
	while !exit do
		print cyan,"morpheus > ",reset
		input = $stdin.gets.chomp!
		if !input.empty?

			if input == 'exit'
				break
			elsif input == 'history'
				commands = history.last(100)
				puts "Last #{commands.size} commands"
				commands.reverse.each do |cmd|
					puts "#{cmd}"
				end
				next
			elsif input == 'clear'
				print "\e[H\e[2J"
				next
			elsif input == '!'
				input = history[-1]
			end

				begin
					history << input
					argv = Shellwords.shellsplit(input)
					if @command_options[:account_name]
						argv.push "--account", @command_options[:account_name]
					end
					if @command_options[:account_id]
						argv.push "--account-id", @command_options[:account_id]
					end
					if @command_options[:nocolor]
						argv.push "--nocolor"
					end
					#puts "cmd: #{argv.join(' ')}"
					if Morpheus::Cli::CliRegistry.has_command?(argv[0])
						Morpheus::Cli::CliRegistry.exec(argv[0], argv[1..-1])
					else
						puts "Unrecognized Command."
					end
				rescue ArgumentError
					puts "Argument Syntax Error..."
				rescue SystemExit, Interrupt
						# nothing to do
				rescue => e
					print red, "\n", e.message, "\n", reset
					print e.backtrace.join("\n"), "\n"
				end
			
		end
	end
end