Sha256: 21ff6c22c241447a61cb1792b9410be90eb991f6dc87cf7f321245ce401b4eff

Contents?: true

Size: 1.14 KB

Versions: 10

Compression:

Stored size: 1.14 KB

Contents

module Sym
  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 Sym::Errors::FileNotFound.new("Encryption key file #{file} was not found.")
          nil
        end
      }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sym-2.3.0 lib/sym/app/private_key/detector.rb
sym-2.2.1 lib/sym/app/private_key/detector.rb
sym-2.2.0 lib/sym/app/private_key/detector.rb
sym-2.1.2 lib/sym/app/private_key/detector.rb
sym-2.1.1 lib/sym/app/private_key/detector.rb
sym-2.1.0 lib/sym/app/private_key/detector.rb
sym-2.0.3 lib/sym/app/private_key/detector.rb
sym-2.0.2 lib/sym/app/private_key/detector.rb
sym-2.0.1 lib/sym/app/private_key/detector.rb
sym-2.0.0 lib/sym/app/private_key/detector.rb