class OtpCli::Config

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

add(secret) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/otp-cli/config.rb, line 33
def otp(id)
        otp = @otps[id]
        raise 'No such OTP' unless otp
        otp
end
select(filter) click to toggle source
# 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