Sha256: c2dc485c6305dfe8a031727fbe48d6520ac1351c1ebe1a8dbb1610cf86ac1d74

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

module Shhh
  module App
    module PrivateKey
      class Detector < Struct.new(:opts, :input_handler) # :nodoc:s
        @mapping = Hash.new
        class << self
          attr_reader :mapping

          def register(argument, proc)
            self.mapping[argument] = proc
          end
        end

        def key
          self.class.mapping.each_pair do |options_key, key_proc|
            return key_proc.call(opts[options_key], self) if opts[options_key]
          end
          nil
        end
      end

      Detector.register :private_key,
                        ->(key, *) { key }

      Detector.register :interactive,
                        ->(*, detector) { detector.input_handler.prompt('Please paste your private key: ', :magenta) }

      Detector.register :keychain,
                        ->(key_name, * ) { KeyChain.new(key_name).find }

      Detector.register :keyfile,
                        ->(file, *) {
        begin
          ::File.read(file)
        rescue Errno::ENOENT
          raise Shhh::Errors::FileNotFound.new("Encryption key file #{file} was not found.")
          nil
        end
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shhh-1.6.5 lib/shhh/app/private_key/detector.rb
shhh-1.6.4 lib/shhh/app/private_key/detector.rb
shhh-1.6.3 lib/shhh/app/private_key/detector.rb
shhh-1.6.2 lib/shhh/app/private_key/detector.rb
shhh-1.6.1 lib/shhh/app/private_key/detector.rb
shhh-1.5.4 lib/shhh/app/private_key/detector.rb
shhh-1.4.1 lib/shhh/app/private_key/detector.rb
shhh-1.4.0 lib/shhh/app/private_key/detector.rb