Sha256: 92c67c65256d755fb3eb6929b4befc6d1a716ed3567328ea2a202284bccda767

Contents?: true

Size: 1014 Bytes

Versions: 1

Compression:

Stored size: 1014 Bytes

Contents

require 'yaml'

module Ec2ssh
  class AwsKeyNotFound < StandardError; end
  class Dotfile
    def initialize(config={})
      @config = {
        'path' => "~/.ssh/config",
        'aws_keys' => {
          'default' => {
            'access_key_id' => ENV['AMAZON_ACCESS_KEY_ID'],
            'secret_access_key' => ENV['AMAZON_SECRET_ACCESS_KEY']
          }
        },
        'regions' => %w(ap-northeast-1),
      }.merge(config)
    end

    def self.load(path)
      new YAML.load(open(path).read)
    end

    def save(path)
      open(path, 'w') {|f| f.write @config.to_yaml }
    end

    def self.update_or_create(path, config={})
      dotfile = if File.exist?(path)
        Dotfile.load(path)
      else
        new
      end
      dotfile.update(config)
      dotfile.save(path)
    end

    def [](key)
      @config[key]
    end

    def aws_key(keyname)
      self['aws_keys'][keyname] or raise AwsKeyNotFound
    end

    def update(config)
      @config = @config.merge config
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ec2ssh-2.0.0 lib/ec2ssh/dotfile.rb