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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/morpheus/cli/shell.rb', line 31
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_logger ||= load_history_logger
@history_logger.info "shell started" if @history_logger
load_history_from_log_file()
exit = false
while !exit do
Readline.completion_append_character = " "
Readline.completion_proc = @auto_complete
Readline.basic_word_break_characters = "\t\n\"\‘`@$><=;|&{( "
input = Readline.readline("#{cyan}morpheus> #{reset}", true).to_s
input = input.strip
if !input.empty?
if input == 'exit'
@history_logger.info "exit" if @history_logger
break
elsif input =~ /^history/
n_commands = input.sub(/^history\s?/, '').sub(/\-n\s?/, '')
n_commands = n_commands.empty? ? 25 : n_commands.to_i
cmd_numbers = @history.keys.last(n_commands)
puts "Last #{cmd_numbers.size} commands"
cmd_numbers.each do |cmd_number|
cmd = @history[cmd_number]
puts "#{cmd_number.to_s.rjust(3, ' ')} #{cmd}"
end
next
elsif input == 'clear'
print "\e[H\e[2J"
next
elsif input == 'flush_history'
file_path = history_file_path
if File.exists?(file_path)
File.truncate(file_path, 0)
end
@history = {}
@last_command_number = 0
@history_logger = load_history_logger
puts "history cleared!"
next
elsif input == '!!'
cmd_number = @history.keys[-1]
input = @history[cmd_number]
if !input
puts "There is no previous command"
next
end
elsif input =~ /^\!.+/
cmd_number = input.sub("!", "").to_i
if cmd_number != 0
input = @history[cmd_number]
if !input
puts "Command not found by number #{cmd_index}"
next
end
end
end
begin
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
if argv.find {|arg| arg == '-V' || arg == '--debug'}
@return_to_log_level = Morpheus::Logging.log_level
Morpheus::Logging.set_log_level(Morpheus::Logging::Logger::DEBUG)
end
argv = argv.find_all {|arg| arg != '-V' && arg != '--debug'}
if argv[0] == 'shell'
puts "You are already in a shell."
elsif Morpheus::Cli::CliRegistry.has_command?(argv[0])
log_history_command(input)
Morpheus::Cli::CliRegistry.exec(argv[0], argv[1..-1])
else
@history_logger.warn "Unrecognized Command: #{input}" if @history_logger
puts "Unrecognized Command."
end
rescue ArgumentError
puts "Argument Syntax Error..."
rescue Interrupt
@history_logger.warn "shell interrupt" if @history_logger
print "\n"
rescue SystemExit
print "\n"
rescue OptionParser::InvalidOption => e
print Term::ANSIColor.red, "\n", "#{e.message}", "", Term::ANSIColor.reset
print "\n", "Try -h for help with this command.", "\n\n"
rescue => e
@history_logger.error "#{e.message}" if @history_logger
print Term::ANSIColor.red, "\n", "Unexpected Error", "\n\n", Term::ANSIColor.reset
if Morpheus::Logging.print_stacktrace?
print Term::ANSIColor.red, "\n", "#{e.class}: #{e.message}", "\n", Term::ANSIColor.reset
print e.backtrace.join("\n"), "\n\n"
end
end
if @return_to_log_level
Morpheus::Logging.set_log_level(@return_to_log_level)
@return_to_log_level = nil
end
end
end
end
|