Class: Mytotp::Commands::Services::Remove

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

Overview

Class command for remove a service. the service can be search by service’s name and username for remove.

Instance Method Summary collapse

Instance Method Details

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

Execute the command

Parameters:

  • service (String) (defaults to: nil)

    service name to remove.

  • username (String) (defaults to: nil)

    username in the service.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mytotp/commands/services/remove.rb', line 15

def call(service: nil, username: nil, **)
  services = Mytotp::Models::Service.where(
    Sequel.ilike(:service, "%#{service}%")
  ).where(
    Sequel.ilike(:username, "%#{username}%")
  )
  case services.count
  when 0
    puts CLI::UI.fmt '{{yellow:No service found}}'
  when 1
    CLI::UI::Frame.open('We found one service!') do
      # ask to remove
      remove(services.first.id)
    end
  else
    # select one
    multiple_options(services)
  end
end

#multiple_options(services) ⇒ Object

Ask for which remove

Parameters:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mytotp/commands/services/remove.rb', line 38

def multiple_options(services)
  CLI::UI::Frame.open('We found more than one!') do
    # puts services.map { |s| s.service + " " + s.username }
    id = CLI::UI::Prompt.ask('Which service do you want to remove?') do |handler|
      services.each do |s|
        handler.option("#{s.service} : #{s.username}") { |_selection| s.id }
      end
    end
    remove(id)
  end
end

#remove(id) ⇒ Object

Remove a service by id

Parameters:

  • id (Integer)

    Unic ID of the service to remove.



53
54
55
56
57
58
59
60
61
62
# File 'lib/mytotp/commands/services/remove.rb', line 53

def remove(id)
  service_obj = Mytotp::Models::Service[id]
  answer = CLI::UI.ask('Are you sure?', options: %w[yes no])
  if answer == 'yes'
    service_obj.delete
    puts CLI::UI.fmt '{{red:Service successfull removed!}}'
  else
    puts CLI::UI.fmt '{{green:No service removed.}}'
  end
end