Class: Morpheus::Cli::Shell
- Inherits:
-
Object
- Object
- Morpheus::Cli::Shell
show all
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/shell.rb
Constant Summary
collapse
- @@instance =
nil
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #handle_subcommand, included, #interactive?, #noninteractive, #print_usage, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
#initialize ⇒ Shell
Returns a new instance of Shell.
22
23
24
25
26
27
28
|
# File 'lib/morpheus/cli/shell.rb', line 22
def initialize()
@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
@@instance = self
recalculate_auto_complete_commands()
end
|
Class Method Details
.instance ⇒ Object
18
19
20
|
# File 'lib/morpheus/cli/shell.rb', line 18
def self.instance
@@instance ||= self.new
end
|
Instance Method Details
#execute_command(input) ⇒ Object
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'lib/morpheus/cli/shell.rb', line 127
def execute_command(input)
@command_options = {}
input = input.to_s.strip
if !input.empty?
if input == 'exit'
@history_logger.info "exit" if @history_logger
exit 0
elsif input == 'help'
puts "You are in a morpheus client shell."
puts "See the available commands below."
puts "\nCommands:"
@morpheus_commands.sort.each {|cmd|
puts "\t#{cmd.to_s}"
}
puts "\n"
puts "\nShell Commands:"
@shell_commands.each {|cmd|
puts "\t#{cmd.to_s}"
}
puts "\n"
puts "For more information."
puts "See https://github.com/gomorpheus/morpheus-cli/wiki"
print "\n"
return 0
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
return 0
elsif input == 'clear'
print "\e[H\e[2J"
return 0
elsif input == 'flush-history' || 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!"
return 0
elsif input == 'reload' || input == 'reload!'
Morpheus::Cli.load!
@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
recalculate_auto_complete_commands()
begin
load __FILE__
rescue => err
print "failed to reload #{__FILE__}. oh well"
end
print dark," #=> shell has been reloaded",reset,"\n" if Morpheus::Logging.debug?
return 0
elsif input == '!!'
cmd_number = @history.keys[-1]
input = @history[cmd_number]
if !input
puts "There is no previous command"
return 1
end
execute_commands(input)
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_number}"
return 0
end
execute_commands(input)
return 0
end
elsif input == "insecure"
Morpheus::RestClient.enable_ssl_verification = false
return 0
elsif ["hello","hi","hey","hola"].include?(input.strip.downcase)
print "#{input.capitalize}, how may I #{cyan}help#{reset} you?\n"
return
elsif input == "shell"
print "#{cyan}You are already in a shell.#{reset}\n"
return false
end
begin
argv = Shellwords.shellsplit(input)
if Morpheus::Cli::CliRegistry.has_command?(argv[0]) || Morpheus::Cli::CliRegistry.has_alias?(argv[0])
Morpheus::Cli::CliRegistry.exec(argv[0], argv[1..-1])
else
print_yellow_warning "Unrecognized Command '#{argv[0]}'. Try 'help' to see a list of available commands."
@history_logger.warn "Unrecognized Command #{argv[0]}" if @history_logger
end
rescue Interrupt
@history_logger.warn "shell interrupt" if @history_logger
print "\nInterrupt. aborting command '#{input}'\n"
rescue SystemExit
rescue => e
@history_logger.error "#{e.message}" if @history_logger
Morpheus::Cli::ErrorHandler.new.handle_error(e, @command_options)
end
if @return_to_log_level
Morpheus::Logging.set_log_level(@return_to_log_level)
::RestClient.log = Morpheus::Logging.debug? ? STDOUT : nil
@return_to_log_level = nil
end
end
end
|
#execute_commands(input) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/morpheus/cli/shell.rb', line 112
def execute_commands(input)
all_commands = input.gsub(/(\;)(?=(?:[^"']|"[^'"]*")*$)/, '__CMDDELIM__').split('__CMDDELIM__').collect {|it| it.to_s.strip }.select {|it| !it.empty? }.compact
all_commands.each do |cmd|
execute_command(cmd)
end
unless input.strip.empty? || (["exit"].include?(input.strip)) || input.strip[0].to_s.chr == "!"
log_history_command(input.strip)
end
end
|
#get_prompt ⇒ Object
330
331
332
333
334
335
336
337
338
339
340
341
342
|
# File 'lib/morpheus/cli/shell.rb', line 330
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
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
|
# File 'lib/morpheus/cli/shell.rb', line 58
def handle(args)
usage = "Usage: morpheus #{command_name}"
@command_options = {} optparse = OptionParser.new do|opts|
opts.banner = usage
opts.on('--norc','--norc', "Do not read and execute the personal initialization script .morpheusrc") do
@norc = true
end
opts.on('-I','--insecure', "Allow for insecure HTTPS communication i.e. bad SSL certificate") do |val|
Morpheus::RestClient.enable_ssl_verification = false
end
opts.on('-C','--nocolor', "Disable ANSI coloring") do
@command_options[:nocolor] = true
Term::ANSIColor::coloring = false
end
opts.on('-V','--debug', "Print extra output for debugging. ") do |json|
Morpheus::Logging.set_log_level(Morpheus::Logging::Logger::DEBUG)
::RestClient.log = Morpheus::Logging.debug? ? STDOUT : nil
@command_options[:debug] = true
end
opts.on( '-h', '--help', "Prints this help" ) do
puts opts
exit
end
end
optparse.parse!(args)
@history_logger ||= load_history_logger rescue nil
@history_logger.info "shell started" if @history_logger
load_history_from_log_file()
if !@norc
if File.exists?(Morpheus::Cli::DotFile.morpheusrc_filename)
@history_logger.info("load source #{Morpheus::Cli::DotFile.morpheusrc_filename}") if @history_logger
Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheusrc_filename).execute()
end
end
exit = false
while !exit do
Readline.completion_append_character = " "
Readline.completion_proc = @auto_complete
Readline.basic_word_break_characters = ""
input = Readline.readline("#{cyan}morpheus> #{reset}", true).to_s
input = input.strip
execute_commands(input)
end
end
|
#history_file_path ⇒ Object
344
345
346
|
# File 'lib/morpheus/cli/shell.rb', line 344
def history_file_path
File.join(Morpheus::Cli.home_directory, "shell_history")
end
|
#load_history_from_log_file(n_commands = 1000) ⇒ Object
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
# File 'lib/morpheus/cli/shell.rb', line 364
def load_history_from_log_file(n_commands=1000)
@history ||= {}
@last_command_number ||= 0
begin
if Gem.win_platform?
return @history
end
file_path = history_file_path
FileUtils.mkdir_p(File.dirname(file_path))
n_lines = n_commands + 500
history_lines = `tail -n #{n_lines} #{file_path}`.split(/\n/)
command_lines = history_lines.select do |line|
line.match(/\(cmd (\d+)\) (.+)/)
end
command_lines = command_lines.last(n_commands)
command_lines.each do |line|
matches = line.match(/\(cmd (\d+)\) (.+)/)
if matches && matches.size == 3
cmd_number = matches[1].to_i
cmd = matches[2]
@last_command_number = cmd_number
@history[@last_command_number] = cmd
Readline::HISTORY << cmd
end
end
rescue => e
@history = {}
end
return @history
end
|
#load_history_logger ⇒ Object
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
# File 'lib/morpheus/cli/shell.rb', line 348
def load_history_logger
file_path = history_file_path
if !Dir.exists?(File.dirname(file_path))
FileUtils.mkdir_p(File.dirname(file_path))
end
if !File.exists?(file_path)
FileUtils.touch(file_path)
FileUtils.chmod(0600, file_path)
end
logger = Logger.new(file_path)
return logger
end
|
#log_history_command(cmd) ⇒ Object
402
403
404
405
406
407
408
409
410
|
# File 'lib/morpheus/cli/shell.rb', line 402
def log_history_command(cmd)
@history ||= {}
@last_command_number ||= 0
@last_command_number += 1
@history[@last_command_number] = cmd
if @history_logger
@history_logger.info "(cmd #{@last_command_number}) #{cmd}"
end
end
|
#recalculate_auto_complete_commands ⇒ Object
def connect(opts)
@api_client = establish_remote_appliance_connection(opts)
end
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/morpheus/cli/shell.rb', line 34
def recalculate_auto_complete_commands
@morpheus_commands = Morpheus::Cli::CliRegistry.all.keys.reject {|k| [:shell].include?(k) }
@shell_commands = [:clear, :history, :'flush-history', :reload!, :help, :exit]
@alias_commands = Morpheus::Cli::CliRegistry.all_aliases.keys
@exploded_commands = []
Morpheus::Cli::CliRegistry.all.each do |cmd, klass|
@exploded_commands << cmd.to_s
subcommands = klass.subcommands rescue []
subcommands.keys.each do |sub_cmd|
@exploded_commands << "#{cmd} #{sub_cmd}"
end
end
@auto_complete_commands = (@exploded_commands + @shell_commands + @alias_commands).collect {|it| it.to_s }
@auto_complete = proc do |s|
command_list = @auto_complete_commands
result = command_list.grep(/^#{Regexp.escape(s)}/)
if result.nil? || result.empty?
Readline::FILENAME_COMPLETION_PROC.call(s)
else
result
end
end
end
|