Class: OtpCli::Config
- Inherits:
-
Object
- Object
- OtpCli::Config
- Defined in:
- lib/otp-cli/config.rb
Instance Method Summary collapse
- #add(secret) ⇒ Object
- #add_qrcode(path) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #otp(id) ⇒ Object
- #select(filter) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
6 7 8 9 10 11 |
# File 'lib/otp-cli/config.rb', line 6 def initialize @file = ENV.fetch('OTP_CONFIG') { File.join Dir.home, '.otp' } FileUtils.touch @file unless File.exists? @file @otps = IO.readlines(@file).collect { |line| OTP.get line.chomp } end |
Instance Method Details
#add(secret) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/otp-cli/config.rb', line 13 def add(secret) begin otp = OTP.get secret rescue raise "Invalid OTP secret #{secret}" end puts "Adding secret #{secret}" File.open(@file, 'a') do |f| f.puts secret end @otps << otp otp end |
#add_qrcode(path) ⇒ Object
28 29 30 31 |
# File 'lib/otp-cli/config.rb', line 28 def add_qrcode(path) data, = Open3.capture2 'zbarimg', '--raw', '-q', path self.add data.chomp end |
#otp(id) ⇒ Object
33 34 35 36 37 |
# File 'lib/otp-cli/config.rb', line 33 def otp(id) otp = @otps[id] raise 'No such OTP' unless otp otp end |
#select(filter) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/otp-cli/config.rb', line 39 def select(filter) otps = if filter @otps.select { |o| o.to_s.downcase.include? filter } else @otps end case otps.size when 0 raise 'No such OTP' when 1 otp = otps.first puts otp otp else len = otps.size.to_s.size otps.each_with_index do |otp, n| puts "[#{(n + 1).to_s.rjust len}] #{otp}" end number = STDIN.gets.to_i - 1 otps[number] end end |