# Class: ROTP::CLI

Inherits:
  Object
  - Object
  - ROTP::CLI
  show all

Defined in:
  lib/rotp/cli.rb

## Instance Attribute Summary collapse

- #argv ⇒ Object readonly

  Returns the value of attribute argv.

- #filename ⇒ Object readonly

  Returns the value of attribute filename.

## Instance Method Summary collapse

- #arguments ⇒ Object
- #errors ⇒ Object

  :nocov:.

- #initialize(filename, argv) ⇒ CLI constructor

  A new instance of CLI.

- #options ⇒ Object
- #output ⇒ Object
- #red(string) ⇒ Object
- #run ⇒ Object

  :nocov:.

## Constructor Details

### #initialize(filename, argv) ⇒ `CLI`

Returns a new instance of CLI.

    7
    8
    9
    10

    # File 'lib/rotp/cli.rb', line 7

    def initialize(filename, argv)
      @filename = filename
      @argv = argv
    end

## Instance Attribute Details

### #argv ⇒ `Object` (readonly)

Returns the value of attribute argv.

    5
    6
    7

    # File 'lib/rotp/cli.rb', line 5

    def argv
      @argv
    end

### #filename ⇒ `Object` (readonly)

Returns the value of attribute filename.

    5
    6
    7

    # File 'lib/rotp/cli.rb', line 5

    def filename
      @filename
    end

## Instance Method Details

### #arguments ⇒ `Object`

    40
    41
    42

    # File 'lib/rotp/cli.rb', line 40

    def arguments
      @arguments ||= ROTP::Arguments.new(filename, argv)
    end

### #errors ⇒ `Object`

:nocov:

    18
    19
    20
    21
    22
    23
    24
    25
    26

    # File 'lib/rotp/cli.rb', line 18

    def errors
      if %i[time hmac].include?(options.mode)
        if options.secret.to_s == ''
          red 'You must also specify a --secret. Try --help for help.'
        elsif options.secret.to_s.chars.any? { |c| ROTP::Base32::CHARS.index(c.upcase).nil? }
          red 'Secret must be in RFC4648 Base32 format - http://en.wikipedia.org/wiki/Base32#RFC_4648_Base32_alphabet'
        end
      end
    end

### #options ⇒ `Object`

    44
    45
    46

    # File 'lib/rotp/cli.rb', line 44

    def options
      arguments.options
    end

### #output ⇒ `Object`

    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38

    # File 'lib/rotp/cli.rb', line 28

    def output
      return options.warnings if options.warnings
      return errors if errors
      return arguments.to_s if options.mode == :help

      if options.mode == :time
        ROTP::TOTP.new(options.secret, options).now
      elsif options.mode == :hmac
        ROTP::HOTP.new(options.secret, options).at options.counter
      end
    end

### #red(string) ⇒ `Object`

    48
    49
    50

    # File 'lib/rotp/cli.rb', line 48

    def red(string)
      "\033[31m#{string}\033[0m"
    end

### #run ⇒ `Object`

:nocov:

    13
    14
    15

    # File 'lib/rotp/cli.rb', line 13

    def run
      puts output
    end