Sha256: 6e559edede2a740343a80c110e9b177e5712734d5822d66c39163ed947ee4ce5

Contents?: true

Size: 908 Bytes

Versions: 3

Compression:

Stored size: 908 Bytes

Contents

require 'pathname'

module Ec2ssh
  class Config
    HEADER = "### EC2SSH BEGIN ###"
    FOOTER = "### EC2SSH END ###"

    attr_reader :path

    def initialize(path=nil)
      @path = Pathname(path || "#{ENV['HOME']}/.ssh/config")
    end

    def append_mark!
      replace! ""
      open(@path, "a") do |f|
        f.puts wrap("")
      end
    end

    def mark_exist?
      config_src =~ /#{HEADER}\n.*#{FOOTER}\n/m
    end

    def replace!(str)
      save! config_src.gsub(/#{HEADER}\n.*#{FOOTER}\n/m, str)
    end

    def config_src
      @config_src ||= open(@path, "r") do |f|
        f.read
      end
    end

    def save!(str)
      open(@path, "w") do |f|
        f.puts str
      end
    end

    def wrap(text)
      return <<-END
#{HEADER}
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
# DO NOT edit this block!
# Updated #{Time.now}
#{text}
#{FOOTER}
      END
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ec2ssh-1.0.3 lib/ec2ssh/config.rb
ec2ssh-1.0.2 lib/ec2ssh/config.rb
ec2ssh-1.0.1 lib/ec2ssh/config.rb