Class: Morpheus::Cli::SearchCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/search_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#connect(opts) ⇒ Object



9
10
11
12
# File 'lib/morpheus/cli/commands/search_command.rb', line 9

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @search_interface = @api_client.search
end

#handle(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/morpheus/cli/commands/search_command.rb', line 14

def handle(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = "Usage: #{prog_name} #{command_name} [phrase]"
    opts.on("-g", "--go", "Go get details for the top search result instead of printing the list.") do
      options[:go] = true
    end
    build_standard_list_options(opts, options)
    opts.footer = <<-EOT
Global search for finding all types of objects
[phrase] is required. This is the phrase to search for.
Prints the list of search results with the most relevant first.
or use the --go option to get details about the top result instead.
EOT
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, min:1)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  if options[:phrase].to_s.empty?
    raise_command_error "[phrase] is required.", args, optparse
  end
  params.merge!(parse_list_options(options))
  @search_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @search_interface.dry.list(params)
    return
  end
  json_response = @search_interface.list(params)
  search_results = json_response["hits"] || json_response["results"] || []
  top_result = search_results[0]
  if options[:go]
    if top_result
      print cyan,"Loading top search result: #{format_morpheus_type(top_result['type'])} #{top_result['name'] || top_result['id']} (score: #{top_result['score']})",reset,"\n"
      return go_to_search_result(top_result, options)
    else
      # print cyan,"No results found.",reset,"\n"
      raise_command_error "No search results for phrase '#{options[:phrase]}'"
    end
  end
  render_response(json_response, options, "hits") do
    print_h1 "Morpheus Search", parse_list_subtitles(options), options
    if search_results.empty?
      print cyan,"No results found.",reset,"\n"
    else
      columns = {
        "Type" => lambda {|it| format_morpheus_type(it['type']) },
        "ID" => 'id',
        # "UUID" => 'uuid',
        "Name" => 'name',
        "Decription" => 'description',
        #"Score" => 'score',
        "Date Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      }
      print as_pretty_table(search_results, columns.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  if search_results.empty?
    return 1, "no results found"
  else
    return 0, nil
  end
end