Class: TFA::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tfa/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(name, secret) ⇒ Object



11
12
13
14
# File 'lib/tfa/cli.rb', line 11

def add(name, secret)
  storage.save(name, clean(secret))
  "Added #{name}"
end

#decryptObject



66
67
68
69
70
# File 'lib/tfa/cli.rb', line 66

def decrypt
  return unless ensure_upgraded!

  secure_storage.decrypt!
end

#destroy(name) ⇒ Object



17
18
19
# File 'lib/tfa/cli.rb', line 17

def destroy(name)
  storage.delete(name)
end

#encryptObject



59
60
61
62
63
# File 'lib/tfa/cli.rb', line 59

def encrypt
  return unless ensure_upgraded!

  secure_storage.encrypt!
end

#now(secret) ⇒ Object



32
33
34
# File 'lib/tfa/cli.rb', line 32

def now(secret)
  TotpCommand.new(storage).run('', secret)
end

#show(name = nil) ⇒ Object



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

def show(name = nil)
  name ? storage.secret_for(name) : storage.all
end

#totp(name = nil) ⇒ Object



27
28
29
# File 'lib/tfa/cli.rb', line 27

def totp(name = nil)
  TotpCommand.new(storage).run(name)
end

#upgradeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tfa/cli.rb', line 37

def upgrade
  if !File.exist?(pstore_path)
    say_status :error, "Unable to detect #{pstore_path}", :red
    return
  end
  if File.exist?(secure_path)
    say_status :error, "The new database format was detected.", :red
    return
  end

  if yes? "Upgrade to #{secure_path}?"
    secure_storage
    pstore_storage.each do |row|
      row.each do |name, secret|
        secure_storage.save(name, secret) if yes?("Migrate `#{name}`?")
      end
    end
    File.delete(pstore_path) if yes?("Delete `#{pstore_path}`?")
  end
end