You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.rb 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'clipboard'
  2. require 'open3'
  3. module OtpCli
  4. class Config
  5. def initialize
  6. @file = ENV.fetch('OTP_CONFIG') { File.join Dir.home, '.otp' }
  7. FileUtils.touch @file unless File.exists? @file
  8. @otps = IO.readlines(@file).collect { |line| OTP.get line.chomp }
  9. end
  10. def add(secret)
  11. begin
  12. otp = OTP.get secret
  13. rescue
  14. raise "Invalid OTP secret #{secret}"
  15. end
  16. puts "Adding secret #{secret}"
  17. File.open(@file, 'a') do |f|
  18. f.puts secret
  19. end
  20. @otps << otp
  21. otp
  22. end
  23. def add_qrcode(path)
  24. data, = Open3.capture2 'zbarimg', '--raw', '-q', path
  25. self.add data.chomp
  26. end
  27. def otp(id)
  28. otp = @otps[id]
  29. raise 'No such OTP' unless otp
  30. otp
  31. end
  32. def select(filter)
  33. otps = if filter
  34. @otps.select { |o| o.to_s.downcase.include? filter }
  35. else
  36. @otps
  37. end
  38. case otps.size
  39. when 0
  40. raise 'No such OTP'
  41. when 1
  42. otp = otps.first
  43. puts otp
  44. otp
  45. else
  46. len = otps.size.to_s.size
  47. otps.each_with_index do |otp, n|
  48. puts "[#{(n + 1).to_s.rjust len}] #{otp}"
  49. end
  50. number = STDIN.gets.to_i - 1
  51. otps[number]
  52. end
  53. end
  54. end
  55. end