Class: Zillabyte::Command::Keys

Inherits:
Base
  • Object
show all
Defined in:
lib/zillabyte/cli/keys.rb

Overview

manage authentication keys

Constant Summary collapse

SHORT_KEY_SPLIT =
18
SHORT_KEY_MAX =
64

Constants inherited from Base

Base::META_COLUMNS

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

alias_command, #api, extract_banner, extract_description, extract_help, extract_help_from_caller, extract_options, extract_summary, inherited, #initialize, method_added, namespace

Methods included from Helpers

#add_git_remote, #app, #ask, #create_git_remote, #display, #error, #extract_app_from_git_config, #extract_app_in_dir, #format_with_bang, #get_flow_name, #git, #handle_downloading_manifest, #has_git?, #longest, #read_multiline, #with_tty

Constructor Details

This class inherits a constructor from Zillabyte::Command::Base

Instance Method Details

#add ⇒ Object

keys:add NAME [KEY]

Add a key for the current user. If no KEY is specified, will try to find ~/.ssh/id_rsa.pub.

--output_type OUTPUT_TYPE # Specify an output format type i.e. json #HIDDEN



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zillabyte/cli/keys.rb', line 23

def add()
  type = options[:output_type] || nil
  name = shift_argument()
  if name.nil?
    error("no name given", type)
  end
  keypath = shift_argument() || "~/.ssh/id_rsa.pub"
  begin
    key = File.binread(File.expand_path(keypath))
  rescue => e
    error(e.message, type)
  end
  message = api.keys.add(name, key)
  if type == "json"
    display("{}")
  else
    display(message)
  end
end

#clear ⇒ Object

keys:clear

Remove all authentication keys from the current user.

--output_type OUTPUT_TYPE # Specify an output format type i.e. json #HIDDEN



151
152
153
154
155
156
157
158
159
160
# File 'lib/zillabyte/cli/keys.rb', line 151

def clear()
  type = options[:output_type] || nil

  message = api.keys.clear()
  if type == "json"
    display("{}")
  else
    display(message)
  end
end

#index ⇒ Object

keys

Display keys for the current user.

-l, --long # Display extended information for each key --output_type OUTPUT_TYPE # Specify an output format type i.e. json #HIDDEN



134
135
136
137
138
139
140
141
142
143
# File 'lib/zillabyte/cli/keys.rb', line 134

def index()
  type = options[:output_type] || nil
  long = options[:long]

  keys = api.keys.list()
  if type.nil?()
    display("=== Keys")
  end
  display(format_keys(keys, long, type))
end

#remove ⇒ Object

keys:remove [NAME]

Remove a key from the current user.

-l, --long # Display extended information for each key -f, --force # Force -- remove key without prompting --output_type OUTPUT_TYPE # Specify an output format type i.e. json #HIDDEN



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
# File 'lib/zillabyte/cli/keys.rb', line 72

def remove()
  type = options[:output_type] || nil
  long = options[:long]
  force = options[:force]
  name = shift_argument()
  key_data = if name.nil?()
    if force
      error("no name given", type)
    end
    keys = api.keys.list()
    display("=== Keys")
    display(format_keys(keys, long, nil))
    # NOTE: it may not be the most efficient thing to use a hash here, unless
    # we expect users to routinely try to remove invalid keys.
    keymap = Hash[*keys.flatten()]
    loop do
      display("Please enter the name of the key you wish to remove (case-sensitive):")
      name = ask().chomp()
      if name.empty?()
        error("Aborting.", type)
      end
      if keymap.has_key?(name)
        break(keymap[name])
      end
      display("Key not found: `#{name}`")
    end
  else
    key = api.keys.show(name)
    if force
      key
    else
      display(format_keys([[name, key]], long, nil))
      loop do
        display("Proceed with removal? (yes or no)")
        choice = ask().chomp()
        if choice == "y" || choice == "yes"
          break(key)
        elsif choice == "n" || choice == "no"
          error("Aborting.", type)
        else
          display("Invalid response.")
        end
      end
    end
    key
  end

  message = api.keys.remove(name, key_data)
  if type == "json"
    display("{}")
  else
    display(message)
  end
end

#show ⇒ Object

keys:show NAME

Show a key for the current user.

--output_type OUTPUT_TYPE # Specify an output format type i.e. json #HIDDEN



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zillabyte/cli/keys.rb', line 49

def show()
  type = options[:output_type] || nil
  name = shift_argument()
  if name.nil?()
    error("no name given", type)
  end

  key = api.keys.show(name)
  if type == "json"
    display({"key" => key}.to_json())
  else
    display(key)
  end
end