Class: Uberpass::CLI

Inherits:
Object
  • Object
show all
Extended by:
Actions
Defined in:
lib/uberpass/cli.rb

Defined Under Namespace

Modules: Actions Classes: DumpDecorator, HelpDecorator, InvalidActionError, ListDecorator, MissingArgumentError, ShowDecorator

Instance Attribute Summary

Attributes included from Actions

#actions

Instance Method Summary collapse

Methods included from Actions

register_action

Constructor Details

#initialize(namespace, input = $stdin, output = $stdout, run_loop = true) ⇒ CLI

Returns a new instance of CLI.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/uberpass/cli.rb', line 145

def initialize(namespace, input = $stdin, output = $stdout, run_loop = true)
  @input    = input
  @output   = output
  @run_loop = run_loop
  @terminal = HighLine.new @input, @output

  pass = @terminal.ask("Enter PEM pass phrase: ") { |q| q.echo = '*' }
  @output.print "\n"

  FileHandler.configure do |handler|
    handler.namespace = namespace
    handler.pass_phrase = pass
  end
  do_action if @run_loop
end

Instance Method Details

#confirm_actionObject



161
162
163
# File 'lib/uberpass/cli.rb', line 161

def confirm_action
  @terminal.agree("<%= color('are you sure?', :confirm) %> ") { |q| q.default = "n" }
end

#do_actionObject



165
166
167
168
169
170
171
172
# File 'lib/uberpass/cli.rb', line 165

def do_action
  input = @terminal.ask "uberpass:#{VERSION}> "
  return if input.strip == 'exit'
  @output.print "\n"
  do_action_with_rescue input
  @output.print "\n"
  do_action if @run_loop
end

#do_action_with_rescue(input) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/uberpass/cli.rb', line 174

def do_action_with_rescue(input)
  args = input.split(/ /).compact
  if args[1] == '<'
    action = fetch_action 'encrypt'
    args.slice!(1)
  else
    action = fetch_action args.slice!(0)
  end

  if action.confirm && @run_loop
    action.call(@terminal, *args) if confirm_action
  else
    action.call(@terminal, *args)
  end
rescue MissingArgumentError, InvalidActionError, OpenSSL::PKey::RSAError => e
  @terminal.say "<%= color('#{e}', :error) %>"
rescue FileHandler::ExistingEntryError => key
  @terminal.say "<%= color('#{key} is already defined, try removing it first', :error) %>"
end

#fetch_action(key) ⇒ Object

Raises:



194
195
196
197
198
199
# File 'lib/uberpass/cli.rb', line 194

def fetch_action(key)
  self.class.actions.each do |action|
    return action if action.name == key || action.short == key
  end
  raise InvalidActionError, key
end