Class: Morpheus::Cli::Logout

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_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, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #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, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeLogout

include Morpheus::Cli::WhoamiHelper include Morpheus::Cli::AccountsHelper



14
15
16
# File 'lib/morpheus/cli/logout.rb', line 14

def initialize()
  @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#connect(options) ⇒ Object



18
19
20
# File 'lib/morpheus/cli/logout.rb', line 18

def connect(options)
  @api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true, :skip_login => true}))
end

#handle(args) ⇒ Object



26
27
28
# File 'lib/morpheus/cli/logout.rb', line 26

def handle(args)
  logout(args)
end

#logout(args) ⇒ Object



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
# File 'lib/morpheus/cli/logout.rb', line 30

def logout(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = usage
    build_common_options(opts, options, [:remote, :quiet])
    opts.footer = <<-EOT
Logout of a remote appliance.
This clears your credentials so that you will need to login again.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args,count:0,optpare:optparse)
  connect(options)
  # establish @appliance_name, @appliance_url, 
  #@api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true, :skip_login => true}))
  begin
    if !@appliance_name
      print_error Morpheus::Terminal.angry_prompt
      puts_error "Please specify a Morpheus Appliance to logout of with -r or see the command `remote use`"
      return 1
    end
    wallet = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).load_saved_credentials
    token = wallet ? wallet['access_token'] : nil
    if !token
      if !options[:quiet]
        puts "You are not currently logged in to #{display_appliance(@appliance_name, @appliance_url)}"
      end
    else
      # todo: need to tell the server to delete the token too..
      # delete token from credentials file
      # note: this also handles updating appliance session info
      Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).logout()
      if !options[:quiet]
        puts "#{cyan}Logged out of #{@appliance_name}. Goodbye #{wallet['username']}!#{reset}"
      end
    end
    # recalcuate echo vars
    Morpheus::Cli::Echo.recalculate_variable_map()
    # recalculate shell prompt after this change
    if Morpheus::Cli::Shell.has_instance?
      Morpheus::Cli::Shell.instance.reinitialize()
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return false
  end

end

#usageObject



22
23
24
# File 'lib/morpheus/cli/logout.rb', line 22

def usage
  "Usage: morpheus logout"
end