Class: Mytotp::Commands::Services::Add

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/mytotp/commands/services/add.rb

Overview

Class command for add a new service.

Instance Method Summary collapse

Instance Method Details

#ask_for_incomplete(service_obj) ⇒ Mytotp::Models::Service

Ask incomplete information for a service_obj

Parameters:

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mytotp/commands/services/add.rb', line 34

def ask_for_incomplete(service_obj)
  # Check service if was input by argument or interactive ask
  if service_obj.service.nil?
    service_obj.service = CLI::UI.ask(
      'Which service do you want to add?', allow_empty: false
    )
  end
  # Check service if was input by argument or interactive ask
  service_obj.username = CLI::UI.ask('Username?', allow_empty: false) if service_obj.username.nil?
  # Check service if was input by argument or interactive ask
  service_obj.key = CLI::UI.ask('Key?', allow_empty: false) if service_obj.key.nil?
  service_obj
end

#call(service: nil, username: nil, key: nil) ⇒ Object

execute the command

Parameters:

  • service (String) (defaults to: nil)

    service name to add

  • username (String) (defaults to: nil)

    username used in the service

  • key (String) (defaults to: nil)

    totp key shared for generate the otp codes



19
20
21
22
23
24
25
26
27
28
# File 'lib/mytotp/commands/services/add.rb', line 19

def call(service: nil, username: nil, key: nil, **)
  service_obj = Mytotp::Models::Service.new(service: service, username: username,
                                            key: key)
  service_obj = ask_for_incomplete(service_obj)
  service_obj.save
  puts CLI::UI.fmt '{{green:Correct saved!}}'
rescue StandardError => e
  print(e.to_s)
  puts CLI::UI.fmt '{{red:Something is not fine, try again!}}'
end