morpheus-cli 3.6.8 → 3.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/lib/morpheus/api/account_groups_interface.rb +2 -2
  3. data/lib/morpheus/api/accounts_interface.rb +4 -7
  4. data/lib/morpheus/api/api_client.rb +207 -70
  5. data/lib/morpheus/api/app_templates_interface.rb +7 -28
  6. data/lib/morpheus/api/apps_interface.rb +14 -21
  7. data/lib/morpheus/api/archive_buckets_interface.rb +2 -2
  8. data/lib/morpheus/api/archive_files_interface.rb +6 -6
  9. data/lib/morpheus/api/auth_interface.rb +14 -1
  10. data/lib/morpheus/api/blueprints_interface.rb +9 -16
  11. data/lib/morpheus/api/cloud_datastores_interface.rb +1 -1
  12. data/lib/morpheus/api/cloud_policies_interface.rb +1 -1
  13. data/lib/morpheus/api/clouds_interface.rb +18 -21
  14. data/lib/morpheus/api/cypher_interface.rb +19 -28
  15. data/lib/morpheus/api/file_copy_request_interface.rb +1 -1
  16. data/lib/morpheus/api/group_policies_interface.rb +1 -1
  17. data/lib/morpheus/api/groups_interface.rb +4 -4
  18. data/lib/morpheus/api/image_builder_boot_scripts_interface.rb +1 -1
  19. data/lib/morpheus/api/image_builder_image_builds_interface.rb +2 -2
  20. data/lib/morpheus/api/image_builder_preseed_scripts_interface.rb +1 -1
  21. data/lib/morpheus/api/instances_interface.rb +17 -23
  22. data/lib/morpheus/api/logs_interface.rb +7 -10
  23. data/lib/morpheus/api/network_domains_interface.rb +1 -1
  24. data/lib/morpheus/api/network_groups_interface.rb +1 -1
  25. data/lib/morpheus/api/network_pool_servers_interface.rb +1 -1
  26. data/lib/morpheus/api/network_pools_interface.rb +1 -1
  27. data/lib/morpheus/api/network_proxies_interface.rb +1 -1
  28. data/lib/morpheus/api/network_services_interface.rb +1 -1
  29. data/lib/morpheus/api/networks_interface.rb +1 -1
  30. data/lib/morpheus/api/old_cypher_interface.rb +55 -0
  31. data/lib/morpheus/api/packages_interface.rb +1 -1
  32. data/lib/morpheus/api/policies_interface.rb +1 -1
  33. data/lib/morpheus/api/setup_interface.rb +1 -1
  34. data/lib/morpheus/api/storage_providers_interface.rb +1 -1
  35. data/lib/morpheus/api/whoami_interface.rb +1 -1
  36. data/lib/morpheus/benchmarking.rb +277 -0
  37. data/lib/morpheus/cli.rb +6 -22
  38. data/lib/morpheus/cli/access_token_command.rb +172 -0
  39. data/lib/morpheus/cli/accounts.rb +5 -0
  40. data/lib/morpheus/cli/apps.rb +93 -37
  41. data/lib/morpheus/cli/archives_command.rb +0 -2
  42. data/lib/morpheus/cli/auth_command.rb +112 -0
  43. data/lib/morpheus/cli/blueprints_command.rb +50 -13
  44. data/lib/morpheus/cli/change_password_command.rb +148 -0
  45. data/lib/morpheus/cli/cli_command.rb +173 -49
  46. data/lib/morpheus/cli/clouds.rb +15 -5
  47. data/lib/morpheus/cli/command_error.rb +7 -1
  48. data/lib/morpheus/cli/{alias_command.rb → commands/standard/alias_command.rb} +79 -51
  49. data/lib/morpheus/cli/commands/standard/benchmark_command.rb +399 -0
  50. data/lib/morpheus/cli/commands/standard/coloring_command.rb +60 -0
  51. data/lib/morpheus/cli/{curl_command.rb → commands/standard/curl_command.rb} +0 -7
  52. data/lib/morpheus/cli/commands/standard/debug_command.rb +61 -0
  53. data/lib/morpheus/cli/{echo_command.rb → commands/standard/echo_command.rb} +1 -1
  54. data/lib/morpheus/cli/{edit_profile_command.rb → commands/standard/edit_profile_command.rb} +0 -0
  55. data/lib/morpheus/cli/{edit_rc_command.rb → commands/standard/edit_rc_command.rb} +0 -0
  56. data/lib/morpheus/cli/commands/standard/get_prompt_command.rb +39 -0
  57. data/lib/morpheus/cli/commands/standard/history_command.rb +76 -0
  58. data/lib/morpheus/cli/{log_level_command.rb → commands/standard/log_level_command.rb} +1 -1
  59. data/lib/morpheus/cli/{man_command.rb → commands/standard/man_command.rb} +2 -2
  60. data/lib/morpheus/cli/commands/standard/rm_command.rb +14 -0
  61. data/lib/morpheus/cli/commands/standard/set_prompt_command.rb +54 -0
  62. data/lib/morpheus/cli/{sleep_command.rb → commands/standard/sleep_command.rb} +0 -0
  63. data/lib/morpheus/cli/{source_command.rb → commands/standard/source_command.rb} +0 -0
  64. data/lib/morpheus/cli/{ssl_verification_command.rb → commands/standard/ssl_verification_command.rb} +1 -1
  65. data/lib/morpheus/cli/commands/standard/tee_command.rb +14 -0
  66. data/lib/morpheus/cli/{version_command.rb → commands/standard/version_command.rb} +0 -0
  67. data/lib/morpheus/cli/credentials.rb +276 -87
  68. data/lib/morpheus/cli/cypher_command.rb +333 -214
  69. data/lib/morpheus/cli/error_handler.rb +12 -2
  70. data/lib/morpheus/cli/groups.rb +44 -20
  71. data/lib/morpheus/cli/hosts.rb +39 -16
  72. data/lib/morpheus/cli/instances.rb +114 -62
  73. data/lib/morpheus/cli/login.rb +74 -21
  74. data/lib/morpheus/cli/logout.rb +3 -4
  75. data/lib/morpheus/cli/mixins/accounts_helper.rb +50 -18
  76. data/lib/morpheus/cli/mixins/print_helper.rb +207 -42
  77. data/lib/morpheus/cli/old_cypher_command.rb +414 -0
  78. data/lib/morpheus/cli/option_parser.rb +6 -1
  79. data/lib/morpheus/cli/processes_command.rb +3 -0
  80. data/lib/morpheus/cli/remote.rb +11 -17
  81. data/lib/morpheus/cli/roles.rb +17 -17
  82. data/lib/morpheus/cli/security_groups.rb +47 -17
  83. data/lib/morpheus/cli/shell.rb +139 -79
  84. data/lib/morpheus/cli/tenants_command.rb +353 -0
  85. data/lib/morpheus/cli/users.rb +26 -18
  86. data/lib/morpheus/cli/version.rb +1 -1
  87. data/lib/morpheus/cli/whoami.rb +14 -10
  88. data/lib/morpheus/formatters.rb +4 -4
  89. data/lib/morpheus/logging.rb +16 -8
  90. data/lib/morpheus/terminal.rb +63 -34
  91. metadata +28 -15
  92. data/lib/morpheus/cli/coloring_command.rb +0 -45
  93. data/lib/morpheus/cli/set_prompt_command.rb +0 -51
@@ -10,24 +10,22 @@ require 'json'
10
10
  class Morpheus::Cli::AliasCommand
11
11
  include Morpheus::Cli::CliCommand
12
12
  set_command_name :alias
13
+ set_command_description "Aliases for commands"
13
14
  register_subcommands :add, :export, :remove, :list
14
15
  #set_default_subcommand :add
15
16
 
16
17
  def initialize()
17
18
  end
18
19
 
19
- def usage
20
- out = "Usage: morpheus #{command_name} [name]='[command]'"
21
- out
22
- end
23
-
24
20
  def handle(args)
25
21
  if args.empty?
26
- puts usage
27
- exit 127
22
+ list(args)
23
+ #add(args)
28
24
  elsif self.class.has_subcommand?(args[0])
29
25
  handle_subcommand(args)
30
- elsif args.count == 1 || (args.count == 2 && args.include?('-e'))
26
+ elsif (args.count == 1) && (args[0] == '-h' || args[0] == '--help')
27
+ handle_subcommand(args)
28
+ elsif (args.count == 1) || (args.count == 2 && args.include?('-e'))
31
29
  add(args)
32
30
  else
33
31
  handle_subcommand(args)
@@ -39,35 +37,58 @@ class Morpheus::Cli::AliasCommand
39
37
  options = {}
40
38
  do_export = false
41
39
  optparse = Morpheus::Cli::OptionParser.new do|opts|
42
- opts.banner = usage
40
+ opts.banner = subcommand_usage("[name]='[command]'")
43
41
  #build_common_options(opts, options, [])
44
42
  opts.on( '-e', '--export', "Export this alias to your .morpheus_profile for future use" ) do
45
43
  do_export = true
46
44
  end
47
- opts.on('-h', '--help', "Prints this help" ) do
45
+ opts.on('-h', '--help', "Print this help" ) do
48
46
  puts opts
49
- puts "Commands:"
50
- subcommands.sort.each {|cmd, method|
51
- puts "\t#{cmd.to_s}"
52
- }
53
- puts "" +
54
- "This defines an alias of a command.\n" +
55
- "The [name] should be a one word .\n" +
56
- "The [command] must be quoted if it is more than one word.\n" +
57
- "The [command] may include multiple commands, semicolon delimited.\n" +
58
- #"Example: alias cloud='clouds' .\n" +
59
- "Aliases can be exported for future use with the -e option.\n" +
60
- "You can use just `alias` instead of `alias add`.\n" +
61
- "For more information, see https://github.com/gomorpheus/morpheus-cli/wiki/Alias"
62
47
  exit
63
48
  end
49
+ formatted_commands_map = " " + subcommands.sort.collect {|cmd| "\t#{cmd}" }.join("\n")
50
+ opts.footer = <<-EOT
51
+ Define a new alias.
52
+ [name] is required. This is the alias name. It should be one word.
53
+ [command] is required. This is the full command or or expression wrapped in quotes.
54
+ Aliases can be exported for future use with the -e option.
55
+ The `alias add` command can be invoked with `alias [name]=[command]`
56
+
57
+ Examples:
58
+ alias cloud=clouds
59
+ alias ij='instances get -j'
60
+ alias new-hosts='hosts list -S id -D'
61
+ alias infra='clouds list -m 5; networks list -m 5; hosts list -m 5'
62
+ alias find-answer='(instances get 42 || instances add) || echo "oh dear..."' -e
63
+
64
+ For more information, see https://github.com/gomorpheus/morpheus-cli/wiki/Alias
65
+ EOT
66
+
64
67
  end
65
68
  optparse.parse!(args)
66
- if args.count != 1
67
- puts optparse
68
- exit 1
69
+ if args.count < 1
70
+ print_error Morpheus::Terminal.angry_prompt
71
+ puts_error "wrong number of arguments, expected 1-N and got #{args.count}\n#{optparse}"
72
+ return 1
73
+ end
74
+
75
+ # make this super forgiving.. my name = command -j -O value=woot
76
+ # the old way, as one argument "name='command'"
77
+ # no spaces.. name="command -j -O value=woot"
78
+ if (args.count == 1)
79
+ alias_definition = args[0]
80
+ elsif (args.count == 2)
81
+ alias_definition = "#{args[0]}='#{args[1]}'"
82
+ else
83
+ # meh, never gets here now anyway..
84
+ # alias_definition = args.join(' ')
85
+ args_alias_str = ""
86
+ alias_parts = args.join(' ').split('=')
87
+ left_side = alias_parts[0]
88
+ right_side = alias_parts[1..-1].join(' ')
89
+ args_alias_str = ""
90
+ alias_definition = "#{left_side}='#{right_side}'"
69
91
  end
70
- alias_definition = args[0]
71
92
 
72
93
 
73
94
  begin
@@ -168,15 +189,24 @@ class Morpheus::Cli::AliasCommand
168
189
  end
169
190
 
170
191
  def list(args)
171
- options = {format:'friendly', sort:'name'}
172
- do_remove = false
192
+ options = {format:'table', sort:'name'}
193
+ do_export = false
173
194
  optparse = Morpheus::Cli::OptionParser.new do|opts|
174
195
  opts.banner = subcommand_usage()
175
- opts.on( '-f', '--format FORMAT', "The format for the output: export, json, friendly (default)." ) do |val|
196
+ opts.on( '-f', '--format FORMAT', "The format for the output: export, json, list, table (default)." ) do |val|
176
197
  options[:format] = val
177
198
  end
199
+ opts.on( '-e', '--export', "Include the '-e' switch after each alias in the output. This implies --format export." ) do
200
+ options[:format] = 'export'
201
+ do_export = true
202
+ end
178
203
  build_common_options(opts, options, [:list, :json])
179
- opts.footer = "This outputs a list of your defined aliases."
204
+ opts.footer = <<-EOT
205
+ Print list of defined aliases.
206
+ Use the --format option to vary output.
207
+ The `alias list` command can be abbreviated as just `alias`.
208
+ For more information, see https://github.com/gomorpheus/morpheus-cli/wiki/Alias
209
+ EOT
180
210
  end
181
211
  optparse.parse!(args)
182
212
 
@@ -229,36 +259,34 @@ class Morpheus::Cli::AliasCommand
229
259
  end
230
260
  out << JSON.pretty_generate({aliases: alias_json})
231
261
  out << "\n"
232
- elsif options[:format] == 'export' || options[:format] == 'config'
262
+ elsif options[:format] == 'export' || options[:format] == 'e' || options[:format] == 'config'
233
263
  # out << "# morpheus aliases for #{`whoami`}\n" # windows!
234
264
  #out << "# morpheus aliases\n"
235
265
  my_aliases.each do |it|
236
266
  out << "alias #{it[:name]}='#{it[:command_string]}'"
267
+ if do_export
268
+ out << " -e"
269
+ end
237
270
  out << "\n"
238
271
  end
239
- else
240
- # friendly
241
- #out << cyan
242
- if num_aliases == 0
243
- #print "You have #{num_aliases} aliases defined."
244
- out << "Found #{num_aliases} aliases"
245
- elsif num_aliases == 1
246
- #print "You have just one alias defined."
247
- out << "Found #{num_aliases} alias"
248
- else
249
- #print "You have #{num_aliases} aliases defined."
250
- out << "Found #{num_aliases} aliases"
251
- end
252
- if options[:phrase]
253
- out << " matching '#{options[:phrase]}'"
254
- end
255
- out << "\n"
256
- out << reset
272
+ elsif options[:format] == 'list'
257
273
  my_aliases.each do |it|
258
- out << " #{cyan}#{it[:name]}#{reset}='#{it[:command_string]}'"
274
+ out << "#{cyan}#{it[:name]}#{reset}='#{it[:command_string]}'"
259
275
  out << "\n"
260
276
  end
261
277
  out << reset
278
+ else
279
+ # table (default)
280
+ alias_columns = {
281
+ "ALIAS" => lambda {|it| it[:name] },
282
+ "COMMAND" => lambda {|it| it[:command_string] }
283
+ }
284
+ out << "\n"
285
+ out << cyan
286
+ out << as_pretty_table(my_aliases, alias_columns, {:border_style => :thin}.merge(options))
287
+ out << format_results_pagination({size:my_aliases.size,total:my_aliases.size.to_i})
288
+ out << reset
289
+ out << "\n"
262
290
  end
263
291
  print out
264
292
  end
@@ -0,0 +1,399 @@
1
+ require 'morpheus/logging'
2
+ require 'morpheus/benchmarking'
3
+ require 'morpheus/cli/cli_command'
4
+
5
+ class Morpheus::Cli::BenchmarkCommand
6
+ include Morpheus::Cli::CliCommand
7
+ set_command_name :'benchmark'
8
+
9
+ # control global benchmark toggle
10
+ register_subcommands :on, :off, :on?, :off?
11
+ # record your own benchmarks
12
+ register_subcommands :start, :stop, :status, :exec => :execute
13
+
14
+
15
+ # this would be cool, we should store all benchmarking results in memory or on disk =o
16
+ # register_subcommands :list, :get, :put, :remove
17
+
18
+ def initialize()
19
+ # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
20
+ end
21
+
22
+ def connect(opts)
23
+ # no connection needed
24
+ end
25
+
26
+ def handle(args)
27
+ handle_subcommand(args)
28
+ end
29
+
30
+ def on(args)
31
+ options = {}
32
+ params = {}
33
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
34
+ opts.banner = subcommand_usage("")
35
+ build_common_options(opts, options, [:quiet])
36
+ opts.footer = <<-EOT
37
+ Enable global benchmarking.
38
+ This behaves the same as if you were to add the -B switch to every command.
39
+ EOT
40
+ end
41
+ optparse.parse!(args)
42
+ connect(options)
43
+ if args.count != 0
44
+ print_error Morpheus::Terminal.angry_prompt
45
+ puts_error "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
46
+ return 1
47
+ end
48
+ Morpheus::Benchmarking.enabled = true
49
+ my_terminal.benchmarking = Morpheus::Benchmarking.enabled
50
+ puts "#{cyan}benchmark: #{green}on#{reset}" unless options[:quiet]
51
+ return 0
52
+ end
53
+
54
+ def off(args)
55
+ options = {}
56
+ params = {}
57
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
58
+ opts.banner = subcommand_usage("")
59
+ build_common_options(opts, options, [:quiet])
60
+ opts.footer = <<-EOT
61
+ Disable global benchmarking.
62
+ The default state for this setting is off.
63
+ EOT
64
+ end
65
+ optparse.parse!(args)
66
+ connect(options)
67
+ if args.count != 0
68
+ print_error Morpheus::Terminal.angry_prompt
69
+ puts_error "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
70
+ return 1
71
+ end
72
+ Morpheus::Benchmarking.enabled = false
73
+ my_terminal.benchmarking = Morpheus::Benchmarking.enabled
74
+ puts "#{cyan}benchmark: #{dark}off#{reset}" unless options[:quiet]
75
+ return 0
76
+ end
77
+
78
+ def on?(args)
79
+ options = {}
80
+ params = {}
81
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
82
+ opts.banner = subcommand_usage("")
83
+ build_common_options(opts, options, [:quiet])
84
+ opts.footer = <<-EOT
85
+ Print the value of the global benchmark setting.
86
+ Exit 0 if on.
87
+ EOT
88
+ end
89
+ optparse.parse!(args)
90
+ connect(options)
91
+ if args.count != 0
92
+ print_error Morpheus::Terminal.angry_prompt
93
+ puts_error "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
94
+ return 1
95
+ end
96
+ if Morpheus::Benchmarking.enabled?
97
+ puts "#{cyan}benchmark: #{green}on#{reset}" unless options[:quiet]
98
+ else
99
+ puts "#{cyan}benchmark: #{dark}off#{reset}" unless options[:quiet]
100
+ end
101
+ return Morpheus::Benchmarking.enabled? ? 0 : 1
102
+ end
103
+
104
+ def off?(args)
105
+ options = {}
106
+ params = {}
107
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
108
+ opts.banner = subcommand_usage("")
109
+ build_common_options(opts, options, [:quiet])
110
+ opts.footer = <<-EOT
111
+ Print the value of the global benchmark setting.
112
+ Exit 0 if off.
113
+ EOT
114
+ end
115
+ optparse.parse!(args)
116
+ connect(options)
117
+ if args.count != 0
118
+ print_error Morpheus::Terminal.angry_prompt
119
+ puts_error "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
120
+ return 1
121
+ end
122
+ unless options[:quiet]
123
+ if Morpheus::Benchmarking.enabled?
124
+ puts "#{cyan}benchmark: #{green}on#{reset}"
125
+ else
126
+ puts "#{cyan}benchmark: #{dark}off#{reset}"
127
+ end
128
+ end
129
+ return Morpheus::Benchmarking.enabled? ? 1 : 0
130
+ end
131
+
132
+ def start(args)
133
+ options = {}
134
+ params = {}
135
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
136
+ opts.banner = subcommand_usage("[name]")
137
+ build_common_options(opts, options, [:quiet])
138
+ opts.footer = <<-EOT
139
+ Start recording a benchmark.
140
+ [name] is required. This is just a name for the routine.
141
+ This allows you to record how long it takes to run a series of commands.
142
+ Just run `benchmark stop` when you are finished.
143
+ EOT
144
+ end
145
+ optparse.parse!(args)
146
+ connect(options)
147
+ # if args.count < 1
148
+ # print_error Morpheus::Terminal.angry_prompt
149
+ # puts_error "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
150
+ # return 1
151
+ # end
152
+ benchmark_name = nil
153
+ if !args.empty?
154
+ benchmark_name = args.join(' ')
155
+ end
156
+ benchmark_record = Morpheus::Benchmarking.start(benchmark_name)
157
+ unless options[:quiet]
158
+ if benchmark_record.name
159
+ print_green_success "Started benchmark '#{benchmark_record.name}'"
160
+ else
161
+ print_green_success "Started benchmark"
162
+ end
163
+ end
164
+ # record this record so it can be stopped later without knowing (or remembering) the name
165
+
166
+ return 0
167
+ end
168
+
169
+ def stop(args)
170
+ options = {}
171
+ params = {}
172
+ benchmark_exit_code = nil
173
+ benchmark_err = nil
174
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
175
+ opts.banner = subcommand_usage("[name]")
176
+ opts.on('--exit CODE', String, "Exit code to end benchmark with. Default is 0 to indicate success.") do |val| # default should my_terminal.last_exit_code
177
+ benchmark_exit_code = val.to_i
178
+ end
179
+ opts.on('--error ERROR', String, "Error message to include with a benchmark that failed.") do |val|
180
+ benchmark_err = val
181
+ end
182
+ build_common_options(opts, options, [:quiet])
183
+ opts.footer = <<-EOT
184
+ Stop recording a benchmark.
185
+ [name] is optional. This is the name of the benchmark to stop.
186
+ The last benchmark is used by default.
187
+ EOT
188
+ end
189
+ optparse.parse!(args)
190
+ connect(options)
191
+ # if args.count < 1
192
+ # print_error Morpheus::Terminal.angry_prompt
193
+ # puts_error "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
194
+ # return 1
195
+ # end
196
+ # benchmark_name = args.join(' ')
197
+ # benchmark_record = Morpheus::Benchmarking.stop(benchmark_name)
198
+
199
+ benchmark_name = nil
200
+ benchmark_record = nil
201
+ if args.empty?
202
+ # stop the last one..
203
+ benchmark_record = Morpheus::Benchmarking.last
204
+ if benchmark_record.nil?
205
+ print_error red,"Benchmark not found",reset,"\n"
206
+ return 1
207
+ end
208
+ else
209
+ benchmark_name = args.join(' ')
210
+ benchmark_record = Morpheus::Benchmarking.lookup(benchmark_name)
211
+ if benchmark_record.nil?
212
+ print_error red,"Benchmark not found by name '#{benchmark_name}'",reset,"\n"
213
+ return 1
214
+ end
215
+ end
216
+ if benchmark_record.start_time == nil || benchmark_record.end_time != nil
217
+ if benchmark_record.name
218
+ print_error red,"Benchmark '#{benchmark_record.name}' is not running",reset,"\n"
219
+ else
220
+ print_error red,"Benchmark is not running",reset,"\n"
221
+ end
222
+ return 1
223
+ end
224
+ benchmark_record.stop(benchmark_exit_code || 0, benchmark_err)
225
+ unless options[:quiet]
226
+ if benchmark_record.name
227
+ print_green_success "Stopped benchmark '#{benchmark_record.name}'"
228
+ else
229
+ print_green_success "Stopped benchmark"
230
+ end
231
+ end
232
+ # always print benchmark info
233
+ Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg)
234
+ return 0
235
+ end
236
+
237
+ def status(args)
238
+ options = {}
239
+ params = {}
240
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
241
+ opts.banner = subcommand_usage("[name]")
242
+ build_common_options(opts, options, [:quiet])
243
+ opts.footer = <<-EOT
244
+ Print status of benchmark.
245
+ [name] is optional. This is the name of the benchmark to inspect.
246
+ The last benchmark is used by default.
247
+ EOT
248
+ end
249
+ optparse.parse!(args)
250
+ connect(options)
251
+ # if args.count < 1
252
+ # print_error Morpheus::Terminal.angry_prompt
253
+ # puts_error "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
254
+ # return 1
255
+ # end
256
+ # benchmark_name = args.join(' ')
257
+ # benchmark_record = Morpheus::Benchmarking.stop(benchmark_name)
258
+
259
+ benchmark_name = nil
260
+ benchmark_record = nil
261
+ if args.empty?
262
+ # stop the last one..
263
+ benchmark_record = Morpheus::Benchmarking.last
264
+ if benchmark_record.nil?
265
+ print_error red,"No benchmark is running",reset,"\n"
266
+ return 1
267
+ end
268
+ else
269
+ benchmark_name = args.join(' ')
270
+ benchmark_record = Morpheus::Benchmarking.lookup(benchmark_name)
271
+ if benchmark_record.nil?
272
+ print_error red,"Benchmark not found by name '#{benchmark_name}'",reset,"\n"
273
+ return 1
274
+ end
275
+ end
276
+
277
+ unless options[:quiet]
278
+ Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg)
279
+ end
280
+ return 0
281
+ end
282
+
283
+ def execute(args)
284
+ benchmark_name = nil
285
+ options = {}
286
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
287
+ opts.banner = subcommand_usage("[command...]")
288
+ opts.on('--name NAME', String, "Name for the benchmark. Default is the command itself.") do |val|
289
+ benchmark_name = val
290
+ end
291
+ build_common_options(opts, options, [:quiet])
292
+ opts.footer = <<-EOT
293
+ Benchmark a specified command.
294
+ [command] is required. This is the command to execute
295
+ EOT
296
+ end
297
+ optparse.parse!(args)
298
+ connect(options)
299
+ if args.count < 1
300
+ print_error Morpheus::Terminal.angry_prompt
301
+ puts_error "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
302
+ return 1
303
+ end
304
+
305
+ cmd = args.join(' ')
306
+
307
+ #previous_terminal_benchmarking = my_terminal.benchmarking
308
+ start_benchmark(benchmark_name || cmd)
309
+
310
+ # exit_code, err = my_terminal.execute(cmd)
311
+ # do this way until terminal supports expressions
312
+ cmd_result = execute_commands_as_expression(cmd)
313
+ exit_code, err = parse_command_result(cmd_result)
314
+
315
+ benchmark_record = stop_benchmark(exit_code, err)
316
+ Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg) if benchmark_record
317
+
318
+ #my_terminal.benchmarking = previous_terminal_benchmarking
319
+ return 0
320
+ end
321
+
322
+ protected
323
+
324
+ # copied these over from shell, consolidate to terminal plz
325
+
326
+ def execute_command(cmd)
327
+ my_terminal.execute(cmd)
328
+ end
329
+
330
+ def execute_commands_as_expression(input)
331
+ flow = input
332
+ if input.is_a?(String)
333
+ begin
334
+ flow = Morpheus::Cli::ExpressionParser.parse(input)
335
+ rescue Morpheus::Cli::ExpressionParser::InvalidExpression => e
336
+ @history_logger.error "#{e.message}" if @history_logger
337
+ return Morpheus::Cli::ErrorHandler.new(my_terminal.stderr).handle_error(e) # lol
338
+ end
339
+ end
340
+ final_command_result = nil
341
+ if flow.size == 0
342
+ # no input eh?
343
+ else
344
+ last_command_result = nil
345
+ if ['&&','||', '|'].include?(flow.first)
346
+ puts_error "#{Morpheus::Terminal.angry_prompt}invalid command format, begins with an operator: #{input}"
347
+ return 99
348
+ elsif ['&&','||', '|'].include?(flow.last)
349
+ puts_error "#{Morpheus::Terminal.angry_prompt}invalid command format, ends with an operator: #{input}"
350
+ return 99
351
+ # elsif ['&&','||', '|'].include?(flow.last)
352
+ # puts_error "invalid command format, consecutive operators: #{cmd}"
353
+ else
354
+ #Morpheus::Logging::DarkPrinter.puts "Executing command flow: #{flow.inspect}" if Morpheus::Logging.debug?
355
+ previous_command = nil
356
+ previous_command_result = nil
357
+ current_operator = nil
358
+ still_executing = true
359
+ flow.each do |flow_cmd|
360
+ if still_executing
361
+ if flow_cmd == '&&'
362
+ # AND operator
363
+ current_operator = flow_cmd
364
+ exit_code, cmd_err = parse_command_result(previous_command_result)
365
+ if exit_code != 0
366
+ still_executing = false
367
+ end
368
+ elsif flow_cmd == '||' # or with previous command
369
+ current_operator = flow_cmd
370
+ exit_code, err = parse_command_result(previous_command_result)
371
+ if exit_code == 0
372
+ still_executing = false
373
+ end
374
+ elsif flow_cmd == '|' # or with previous command
375
+ puts_error "The PIPE (|) operator is not yet supported =["
376
+ previous_command_result = nil
377
+ still_executing = false
378
+ # or just continue?
379
+ elsif flow_cmd.is_a?(Array)
380
+ # this is a subexpression, execute it as such
381
+ current_operator = nil
382
+ previous_command_result = execute_commands_as_expression(flow_cmd)
383
+ else # it's a command, not an operator
384
+ current_operator = nil
385
+ previous_command_result = execute_command(flow_cmd)
386
+ end
387
+ previous_command = flow_cmd
388
+ else
389
+ #Morpheus::Logging::DarkPrinter.puts "operator skipped command: #{flow_cmd}" if Morpheus::Logging.debug?
390
+ end
391
+ # previous_command = flow_cmd
392
+ end
393
+ final_command_result = previous_command_result
394
+ end
395
+ end
396
+ return final_command_result
397
+ end
398
+
399
+ end