Class: MrMurano::InteractiveSolutionFinder
- Inherits:
-
Object
- Object
- MrMurano::InteractiveSolutionFinder
- Defined in:
- lib/MrMurano/commands/solution_picker.rb
Overview
A class for finding solutions, either automatically or interactively.
Instance Method Summary collapse
- #find_or_create(model) ⇒ Object
-
#initialize(skip_verify: false, create_ok: false, update_cfg: false, ignore_cfg: false, verbose: false, match_enable: false, match_api_id: nil, match_name: nil, match_fuzzy: nil) ⇒ InteractiveSolutionFinder
constructor
A new instance of InteractiveSolutionFinder.
- #solution_choose_solution(solz, type_name) ⇒ Object
- #solution_create_new_solution(sol) ⇒ Object
- #solution_find_by_api_id(sol) ⇒ Object
- #solution_lookup_or_ask(sol) ⇒ Object
- #solution_search_by_term(sol) ⇒ Object
Constructor Details
#initialize(skip_verify: false, create_ok: false, update_cfg: false, ignore_cfg: false, verbose: false, match_enable: false, match_api_id: nil, match_name: nil, match_fuzzy: nil) ⇒ InteractiveSolutionFinder
Returns a new instance of InteractiveSolutionFinder.
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 381 def initialize( skip_verify: false, create_ok: false, update_cfg: false, ignore_cfg: false, verbose: false, match_enable: false, match_api_id: nil, match_name: nil, match_fuzzy: nil ) @skip_verify = skip_verify @create_ok = create_ok @update_cfg = update_cfg @ignore_cfg = ignore_cfg @verbose = verbose @match_enable = match_enable @match_api_id = match_api_id @match_name = match_name @match_fuzzy = match_fuzzy @match_api_id = nil if @match_api_id.to_s.empty? @match_name = nil if @match_name.to_s.empty? @match_fuzzy = nil if @match_fuzzy.to_s.empty? @searching = @match_enable && (@match_api_id || @match_name || @match_fuzzy) end |
Instance Method Details
#find_or_create(model) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 407 def find_or_create(model) # First, try to find the solution by solution ID. sol = solution_find_by_api_id(model) # If not found, search existing solutions, and maybe ask user. if sol.nil? if @searching sol = solution_search_by_term(model) sol = solution_create_new_solution(model) if sol.nil? && @create_ok && @match_api_id.nil? else sol = solution_lookup_or_ask(model) end end # Finally, if asked, update the config. if @update_cfg && !sol.nil? # Update the config in memory and on disk/file. $cfg.set(sol.cfg_key_id, sol.api_id, :project) $cfg.set(sol.cfg_key_name, sol.name, :project) end sol end |
#solution_choose_solution(solz, type_name) ⇒ Object
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 527 def solution_choose_solution(solz, type_name) sol = nil choose do || .prompt = "Select which #{type_name} to use:" .flow = :columns_across .list_option = list_option_override(solz) # NOTE: There are 2 human friendly identifiers, :name and :domain. solz.sort_by(&:domain).each do |option| .choice(option.name) do sol = option end end end sol end |
#solution_create_new_solution(sol) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 492 def solution_create_new_solution(sol) # See if user specified name using a switch option. solname = nil solname = @match_name if solname.nil? solname = @match_fuzzy if solname.nil? if solname.nil? #say "You do not have any #{type}s. Let's create one." if @verbose plural = Inflecto.pluralize(sol.type.to_s) say("This business does not have any #{plural}. Let's create one") puts '' end solname = solution_ask_for_name(sol) end return nil if solname.nil? # The solution name, if we asked for it, will have been verified; # otherwise, if supplied as --option but an invalid solution name, # raise a fuss. begin sol.set_name!(solname) rescue MrMurano::ConfigError => _err MrMurano::Verbose.error(sol.name_validate_help) exit 1 end # MAYBE/2017-07-20: Detect if Business is ADC enabled. If not, # creating a solution fails, e.g., # Request Failed: 409: [409] upgrade sol = sol.biz.new_solution!(sol.name, sol.type) unless sol.name.to_s.empty? say "Created new #{sol.pretty_desc(add_type: true)}" if @verbose puts '' if @verbose sol end |
#solution_find_by_api_id(sol) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 428 def solution_find_by_api_id(sol) exists = false if @searching || @ignore_cfg sol.api_id = @match_api_id || @match_fuzzy else # Note that we verify the solution ID we find in the config, # since the user could've, e.g., deleted it via the web UI. # LATER: This only works so long as there's only one Product # or one Application. Eventually we'll add support for more. sol.api_id = $cfg[sol.cfg_key_id].to_s return sol if @skip_verify end if sol.api_id? tried_api_id = sol.api_id if @searching whirly_msg = "Searching #{sol.type_name} by ID..." else whirly_msg = "Verifying #{sol.type_name}..." end MrMurano::Verbose.whirly_start(whirly_msg) sol.info_safe if sol.valid_api_id exists = true # Convert from Solution to proper subclass, perhaps. sol = solution_factory_reset(sol) else sol.api_id = nil end MrMurano::Verbose.whirly_stop # Spit out some messages, maybe. if @verbose if exists say "Found #{sol.type_name} #{sol.pretty_desc}" elsif !@searching # The solution ID in the config was not found for this business. tried_api_id = MrMurano::Verbose.fancy_ticks(tried_api_id) say "The #{sol.type_name} ID #{tried_api_id} from the config was not found" end puts '' end end (exists && sol) || nil end |
#solution_lookup_or_ask(sol) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 472 def solution_lookup_or_ask(sol) solz = solution_get_solutions(sol.biz, sol.type) if solz.count == 1 sol = solz.first #say "This business has one #{sol.type_name}. Using #{Rainbow(sol.domain).underline}" say "This business has one #{sol.type_name}. Using #{sol.pretty_desc}" if @verbose puts '' if @verbose elsif solz.count.zero? if @create_ok sol = solution_create_new_solution(sol) else sol.error("No #{Inflecto.pluralize(sol.type.to_s)} found") sol = nil end else sol = solution_choose_solution(solz, sol.type_name) end sol end |
#solution_search_by_term(sol) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/MrMurano/commands/solution_picker.rb', line 543 def solution_search_by_term(sol) solz = solution_get_solutions( sol.biz, sol.type, api_id: @match_api_id, name: @match_name, fuzzy: @match_fuzzy ) if solz.length > 1 sol.error("More than one matching #{sol.type_name} found. Please be more specific") sol = nil # MAYBE/2017-07-01: Show interactive menu. # For now, if we didn't exit, weird behavior might occur, e.g., if # user calls `murano init --application foo` and 2 matches are found, # if we returned nil, the code would create a new application. exit 1 elsif solz.length.zero? inflection = MrMurano::Verbose.pluralize?(sol.type_name, 0) # Only blather an error if we're not about to create a new solution. sol.error("No matching #{inflection} found.") unless @create_ok sol = nil # It's okay not to exit. If `murano init` was called, a new # solution will be created; otherwise, the command will exit. else sol = solz.first say "Found one matching #{sol.type_name}. Using #{sol.pretty_desc}" if @verbose puts '' if @verbose end sol end |