Sha256: a51754310fe7431b6d5a73e99a1645bbbe9793f43f522535a9f73246edc3b079

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 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 rescue nil }

      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

1 entries across 1 versions & 1 rubygems

Version Path
shhh-1.7.0 lib/shhh/app/private_key/detector.rb