Module: Morpheus::Cli::OptionTypes
- Includes:
- Term::ANSIColor
- Defined in:
- lib/morpheus/cli/option_types.rb
Class Method Summary collapse
-
.checkbox_prompt(option_type) ⇒ Object
this is a funky one, the user is prompted for yes/no but the return value is ‘on’,‘off’,nil todo: maybe make this easier to use, and have the api’s be flexible too..
- .confirm(message, options = {}) ⇒ Object
- .display_option_types_help(option_types) ⇒ Object
- .display_select_options(opt, select_options = []) ⇒ Object
- .file_prompt(option_type) ⇒ Object
- .format_option_types_help(option_types) ⇒ Object
- .generic_prompt(option_type) ⇒ Object
- .get_last_select ⇒ Object
- .grails_params(data, context = nil) ⇒ Object
- .help_prompt(option_type) ⇒ Object
- .load_source_options(source, api_client, params) ⇒ Object
- .multiline_prompt(option_type) ⇒ Object
- .no_prompt(option_types, options = {}, api_client = nil, api_params = {}) ⇒ Object
- .number_prompt(option_type) ⇒ Object
- .optional_label(option_type) ⇒ Object
- .password_prompt(option_type) ⇒ Object
- .prompt(option_types, options = {}, api_client = nil, api_params = {}, no_prompt = false) ⇒ Object
- .radio_prompt(option_type) ⇒ Object
- .select_prompt(option_type, api_client, api_params = {}, no_prompt = false, use_value = nil) ⇒ Object
- .set_last_select(obj) ⇒ Object
Class Method Details
.checkbox_prompt(option_type) ⇒ Object
this is a funky one, the user is prompted for yes/no but the return value is ‘on’,‘off’,nil todo: maybe make this easier to use, and have the api’s be flexible too..
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 |
# File 'lib/morpheus/cli/option_types.rb', line 367 def self.checkbox_prompt(option_type) value_found = false value = nil has_default = option_type['defaultValue'] != nil default_yes = has_default ? ['on', 'true', 'yes', '1'].include?(option_type['defaultValue'].to_s.downcase) : false while !value_found do print "#{option_type['fieldLabel']} (yes/no)#{has_default ? ' ['+(default_yes ? 'yes' : 'no')+']' : ''}: " input = $stdin.gets.chomp! if input == '?' help_prompt(option_type) next end if input.downcase == 'yes' value_found = true value = 'on' elsif input.downcase == 'no' value_found = true value = 'off' elsif input == '' && has_default value_found = true value = default_yes ? 'on' : 'off' end if value.nil? && option_type['required'] puts "Invalid Option... Please try again." next end if value.nil? && !option_type['required'] value_found = true end end return value end |
.confirm(message, options = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/morpheus/cli/option_types.rb', line 8 def self.confirm(,={}) if [:yes] == true return true end default_value = [:default] value_found = false while value_found == false do if default_value.nil? print "#{} (yes/no): " else print "#{} (yes/no) [#{!!default_value ? 'yes' : 'no'}]: " end input = $stdin.gets.chomp! if input.empty? && !default_value.nil? return !!default_value end if input.downcase == 'yes' || input.downcase == 'y' return true elsif input.downcase == 'no' || input.downcase == 'n' return false else puts "Invalid Option... Please try again." end end end |
.display_option_types_help(option_types) ⇒ Object
526 527 528 |
# File 'lib/morpheus/cli/option_types.rb', line 526 def self.display_option_types_help(option_types) puts self.format_option_types_help(option_types) end |
.display_select_options(opt, select_options = []) ⇒ Object
508 509 510 511 512 513 514 515 |
# File 'lib/morpheus/cli/option_types.rb', line 508 def self.(opt, = []) header = opt['fieldLabel'] ? "#{opt['fieldLabel']} Options" : "Options" puts "\n#{header}" puts "===============" .each do |option| puts " * #{option['name']} [#{option['value']}]" end end |
.file_prompt(option_type) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/morpheus/cli/option_types.rb', line 458 def self.file_prompt(option_type) value_found = false value = nil while !value_found do #print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{optional_label(option_type)}#{option_type['defaultValue'] ? ' ['+option_type['defaultValue'].to_s+']' : ''}: " Readline.completion_append_character = "" Readline.basic_word_break_characters = '' Readline.completion_proc = proc {|s| Readline::FILENAME_COMPLETION_PROC.call(s) } input = Readline.readline("#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{optional_label(option_type)}#{option_type['defaultValue'] ? ' ['+option_type['defaultValue'].to_s+']' : ''}: ", false).to_s input = input.chomp.strip #input = $stdin.gets.chomp! value = input.empty? ? option_type['defaultValue'] : input.to_s if input == '?' help_prompt(option_type) elsif value.empty? && option_type['required'] != true value = nil value_found = true elsif value filename = File.(value) if !File.exists?(filename) # print_red_alert "File not found: #{filename}" # exit 1 print Term::ANSIColor.red," File not found: #{filename}",Term::ANSIColor.reset, "\n" elsif !File.file?(filename) print Term::ANSIColor.red," Argument is not a file: #{filename}",Term::ANSIColor.reset, "\n" else value = filename value_found = true end end end return value end |
.format_option_types_help(option_types) ⇒ Object
517 518 519 520 521 522 523 524 |
# File 'lib/morpheus/cli/option_types.rb', line 517 def self.format_option_types_help(option_types) if option_types.empty? "Available Options:\nNone\n\n" else option_lines = option_types.collect {|it| " -O #{it['fieldName']}=\"value\"" }.join("\n") "Available Options:\n#{option_lines}\n\n" end end |
.generic_prompt(option_type) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/morpheus/cli/option_types.rb', line 400 def self.generic_prompt(option_type) value_found = false value = nil while !value_found do print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''}#{!option_type['defaultValue'].to_s.empty? ? ' ['+option_type['defaultValue'].to_s+']' : ''}: " input = $stdin.gets.chomp! value = input.empty? ? option_type['defaultValue'] : input if input == '?' help_prompt(option_type) elsif !value.nil? || option_type['required'] != true value_found = true end end return value end |
.get_last_select ⇒ Object
244 245 246 |
# File 'lib/morpheus/cli/option_types.rb', line 244 def self.get_last_select() Thread.current[:_last_select] end |
.grails_params(data, context = nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/morpheus/cli/option_types.rb', line 172 def self.grails_params(data, context=nil) params = {} data.each do |k,v| if v.is_a?(Hash) params.merge!(grails_params(v, context ? "#{context}.#{k.to_s}" : k)) else if context params["#{context}.#{k.to_s}"] = v else params[k.to_s] = v end end end return params end |
.help_prompt(option_type) ⇒ Object
492 493 494 495 496 497 498 499 500 501 |
# File 'lib/morpheus/cli/option_types.rb', line 492 def self.help_prompt(option_type) full_field_name = option_type['fieldContext'] ? (option_type['fieldContext']+'.') : '' full_field_name << option_type['fieldName'].to_s # an attempt at prompting help for natural options without the -O switch if option_type[:fmt] == :natural print Term::ANSIColor.green," * #{option_type['fieldLabel']} [--#{full_field_name}=] ", Term::ANSIColor.reset , "#{option_type['description']}\n" else print Term::ANSIColor.green," * #{option_type['fieldLabel']} [-O #{full_field_name}=] - ", Term::ANSIColor.reset , "#{option_type['description']}\n" end end |
.load_source_options(source, api_client, params) ⇒ Object
504 505 506 |
# File 'lib/morpheus/cli/option_types.rb', line 504 def self.(source,api_client,params) api_client..(source,params)['data'] end |
.multiline_prompt(option_type) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/morpheus/cli/option_types.rb', line 416 def self.multiline_prompt(option_type) value_found = false value = nil while !value_found do if value.nil? print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{optional_label(option_type)} [Type 'EOF' to stop input]: \n" end input = $stdin.gets.chomp! # value = input.empty? ? option_type['defaultValue'] : input if input == '?' && value.nil? help_prompt(option_type) elsif input.chomp == 'EOF' value_found = true else if value.nil? value = '' end value << input + "\n" end end return value ? value.strip : value end |
.no_prompt(option_types, options = {}, api_client = nil, api_params = {}) ⇒ Object
34 35 36 |
# File 'lib/morpheus/cli/option_types.rb', line 34 def self.no_prompt(option_types, ={}, api_client=nil,api_params={}) prompt(option_types, , api_client, api_params, true) end |
.number_prompt(option_type) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/morpheus/cli/option_types.rb', line 222 def self.number_prompt(option_type) value_found = false value = nil while !value_found do print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''}#{!option_type['defaultValue'].to_s.empty? ? ' ['+option_type['defaultValue'].to_s+']' : ''}: " input = $stdin.gets.chomp! value = input.empty? ? option_type['defaultValue'] : input value = value.to_s.include?('.') ? value.to_f : value.to_i if input == '?' help_prompt(option_type) elsif !value.nil? || option_type['required'] != true value_found = true end end return value end |
.optional_label(option_type) ⇒ Object
530 531 532 533 534 535 536 537 |
# File 'lib/morpheus/cli/option_types.rb', line 530 def self.optional_label(option_type) # removing this for now, for the sake of providing less to look at if option_type[:fmt] == :natural # || true return "" else return option_type['required'] ? '' : ' (optional)' end end |
.password_prompt(option_type) ⇒ Object
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/morpheus/cli/option_types.rb', line 439 def self.password_prompt(option_type) value_found = false while !value_found do print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{optional_label(option_type)}#{option_type['defaultValue'] ? ' ['+option_type['defaultValue'].to_s+']' : ''}: " input = $stdin.noecho(&:gets).chomp! value = input print "\n" if input == '?' help_prompt(option_type) elsif input == "" && option_type['defaultValue'] != nil value = option_type['defaultValue'].to_s value_found = true elsif !value.empty? || option_type['required'] != true value_found = true end end return value end |
.prompt(option_types, options = {}, api_client = nil, api_params = {}, no_prompt = false) ⇒ Object
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/option_types.rb', line 38 def self.prompt(option_types, ={}, api_client=nil,api_params={}, no_prompt=false) results = {} = || {} # puts "Options Prompt #{options}" option_types.sort { |x,y| x['displayOrder'].to_i <=> y['displayOrder'].to_i }.each do |option_type| context_map = results value = nil value_found=false # How about this instead? # option_type = option_type.clone # field_key = [option_type['fieldContext'], option_type['fieldName']].select {|it| it && it != '' }.join('.') # if field_key != '' # value = get_object_value(options, field_key) # if value != nil && options[:always_prompt] != true # value_found = true # end # end field_key = [option_type['fieldContext'], option_type['fieldName']].select {|it| it && it != '' }.join('.') namespaces = field_key.split(".") field_name = namespaces.pop if field_key.include?(".") cur_namespace = namespaces.each do |ns| next if ns.empty? cur_namespace[ns.to_s] ||= {} cur_namespace = cur_namespace[ns.to_s] context_map[ns.to_s] ||= {} context_map = context_map[ns.to_s] end # use the value passed in the options map if cur_namespace.key?(field_name) value = cur_namespace[field_name] if option_type['type'] == 'number' value = value.to_s.include?('.') ? value.to_f : value.to_i elsif option_type['type'] == 'select' # this should just fall down through below, with the extra params no_prompt, use_value value = select_prompt(option_type, api_client, (api_params || {}).merge(results), true, value) end if [:always_prompt] != true value_found = true end end else # no fieldContext if value_found == false && .key?(field_key) value = [field_key] if option_type['type'] == 'number' value = value.to_s.include?('.') ? value.to_f : value.to_i end # still prompt if [:always_prompt] != true value_found = true end end end # set the value that has been passed to the option type default value: options[fieldContext.fieldName] if value != nil # && value != '' option_type = option_type.clone option_type['defaultValue'] = value end # no_prompt means skip prompting and instead # use default value or error if a required option is not present no_prompt = no_prompt || [:no_prompt] if no_prompt if !value_found if option_type['defaultValue'] != nil value = option_type['defaultValue'] value_found = true end if !value_found # select type is special because it supports skipSingleOption # and prints the available options on error if option_type['type'] == 'select' value = select_prompt(option_type, api_client, (api_params || {}).merge(results), true) value_found = !!value end if !value_found if option_type['required'] print Term::ANSIColor.red, "\nMissing Required Option\n\n", Term::ANSIColor.reset print Term::ANSIColor.red, " * #{option_type['fieldLabel']} [-O #{field_key}=] - #{option_type['description']}\n", Term::ANSIColor.reset print "\n" exit 1 else next end end end end end if !value_found if option_type['type'] == 'number' value = number_prompt(option_type) elsif option_type['type'] == 'password' value = password_prompt(option_type) elsif option_type['type'] == 'checkbox' value = checkbox_prompt(option_type) elsif option_type['type'] == 'radio' value = radio_prompt(option_type) elsif option_type['type'] == 'textarea' value = multiline_prompt(option_type) elsif option_type['type'] == 'code-editor' value = multiline_prompt(option_type) elsif option_type['type'] == 'select' # so, the /api/options/source is may need ALL the previously # selected values that are being accumulated in options # api_params is just extra params to always send # I suppose the entered value should take precedence # api_params = api_params.merge(options) # this might be good enough # dup it value = select_prompt(option_type,api_client, (api_params || {}).merge(results)) elsif option_type['type'] == 'hidden' value = option_type['defaultValue'] input = value elsif option_type['type'] == 'file' value = file_prompt(option_type) else value = generic_prompt(option_type) end end context_map[field_name] = value end return results end |
.radio_prompt(option_type) ⇒ Object
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 |
# File 'lib/morpheus/cli/option_types.rb', line 188 def self.radio_prompt(option_type) value_found = false value = nil = [] if option_type['config'] and option_type['config']['radioOptions'] option_type['config']['radioOptions'].each do |radio_option| << {key: radio_option['key'], checked: radio_option['checked']} end end optionString = .collect{ |b| b[:checked] ? "(#{b[:key]})" : b[:key]}.join(', ') while !value_found do print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }[#{optionString}]: " input = $stdin.gets.chomp! if input == '?' help_prompt(option_type) else if input.nil? || input.empty? selectedOption = .find{|o| o[:checked] == true} else selectedOption = .find{|o| o[:key].downcase == input.downcase} end if selectedOption value = selectedOption[:key] else puts "Invalid Option. Please select from #{optionString}." end if !value.nil? || option_type['required'] != true value_found = true end end end return value end |
.select_prompt(option_type, api_client, api_params = {}, no_prompt = false, use_value = nil) ⇒ Object
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/morpheus/cli/option_types.rb', line 248 def self.select_prompt(option_type,api_client, api_params={}, no_prompt=false, use_value=nil) value_found = false value = nil default_value = option_type['defaultValue'] # local array of options if option_type['selectOptions'] = option_type['selectOptions'] # remote optionSource aka /api/options/$optionSource? elsif option_type['optionSource'] # /api/options/list is a special action for custom OptionTypeLists, just need to pass the optionTypeId parameter if option_type['optionSource'] == 'list' = (option_type['optionSource'], api_client, {'optionTypeId' => option_type['id']}) else = (option_type['optionSource'], api_client, grails_params(api_params || {})) end else raise "select_prompt() requires selectOptions or optionSource!" end # ensure the preselected value (passed as an option) is in the dropdown if !use_value.nil? matched_value = .find {|opt| opt['value'].to_s == use_value.to_s } if !matched_value.nil? value = use_value value_found = true else print Term::ANSIColor.red, "\nInvalid Option #{option_type['fieldLabel']}: [#{use_value}]\n\n", Term::ANSIColor.reset print Term::ANSIColor.red, " * #{option_type['fieldLabel']} [-O #{option_type['fieldContext'] ? (option_type['fieldContext']+'.') : ''}#{option_type['fieldName']}=] - #{option_type['description']}\n", Term::ANSIColor.reset if && .size > 10 (option_type, .first(10)) puts " (#{.size-1} more)" else (option_type, ) end print "\n" exit 1 end # skipSingleOption is no longer supported # elsif !select_options.nil? && select_options.count == 1 && option_type['skipSingleOption'] == true # value_found = true # value = select_options[0]['value'] # if there is just one option, use it as the defaultValue elsif !.nil? && .count == 1 if option_type['required'] && default_value.nil? default_value = [0]['name'] # name is prettier than value end elsif !.nil? if default_value.nil? found_default_option = .find {|opt| opt['isDefault'] == true } if found_default_option default_value = found_default_option['name'] # name is prettier than value end end end if no_prompt if !value_found if !.nil? && .count > 1 && option_type['autoPickOption'] == true value_found = true value = [0]['value'] elsif option_type['required'] print Term::ANSIColor.red, "\nMissing Required Option\n\n", Term::ANSIColor.reset print Term::ANSIColor.red, " * #{option_type['fieldLabel']} [-O #{option_type['fieldContext'] ? (option_type['fieldContext']+'.') : ''}#{option_type['fieldName']}=] - #{option_type['description']}\n", Term::ANSIColor.reset if && .size > 10 (option_type, .first(10)) puts " (#{.size-1} more)" else (option_type, ) end print "\n" exit 1 else return nil end end end while !value_found do Readline.completion_append_character = "" Readline.basic_word_break_characters = '' Readline.completion_proc = proc {|s| matches = [] = ( || []) .each{|option| if option['name'] && option['name'] =~ /^#{Regexp.escape(s)}/ matches << option['name'] # elsif option['id'] && option['id'].to_s =~ /^#{Regexp.escape(s)}/ elsif option['value'] && option['value'].to_s == s matches << option['name'] end } matches } input = Readline.readline("#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''}#{!default_value.to_s.empty? ? ' ['+default_value.to_s+']' : ''} ['?' for options]: ", false).to_s input = input.chomp.strip if input.empty? && default_value input = default_value end select_option = .find{|b| b['name'] == input || (!b['value'].nil? && b['value'].to_s == input) || (b['value'].nil? && input.empty?)} if select_option value = select_option['value'] set_last_select(select_option) elsif !input.nil? && !input.empty? input = '?' end if input == '?' help_prompt(option_type) (option_type, ) elsif !value.nil? || option_type['required'] != true value_found = true end end return value end |
.set_last_select(obj) ⇒ Object
240 241 242 |
# File 'lib/morpheus/cli/option_types.rb', line 240 def self.set_last_select(obj) Thread.current[:_last_select] = obj end |