class Ec2ssh::SshConfig

Constants

Attributes

path[R]
sections[R]

Public Class Methods

new(path=nil) click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 11
def initialize(path=nil)
  @path     = path || "#{ENV['HOME']}/.ssh/config"
  @sections = {}
end

Public Instance Methods

append_mark!() click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 35
def append_mark!
  replace! ""
  File.open(@path, "a") do |f|
    f.puts wrap("")
  end
end
config_src() click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 50
def config_src
  unless File.exist?(@path)
    File.open(@path, "w", 0600).close()
  end

  @config_src ||= File.open(@path, "r") do |f|
    f.read
  end
end
mark_exist?() click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 42
def mark_exist?
  config_src =~ /#{HEADER}\n.*#{FOOTER}\n/m
end
parse!() click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 16
def parse!
  return unless mark_exist?
  ec2_config = config_src.match(/#{HEADER}\n(.*)#{FOOTER}/m).to_s

  current_section = 'default'
  @sections[current_section] = Section.new('default')

  ec2_config.split(/\n+/).each do |line|
    if line =~ /#{Section::HEADER} (.+)/
      current_section = $1
      @sections[current_section] ||= Section.new(current_section)
    elsif line =~ /^#/ # ignore
    elsif line =~ /^$/ # ignore
    else
      @sections[current_section].append("#{line}\n")
    end
  end
end
replace!(str) click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 46
def replace!(str)
  save! config_src.gsub(/#{HEADER}\n.*#{FOOTER}\n/m, str)
end
save!(str) click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 60
def save!(str)
  File.open(@path, "w") do |f|
    f.puts str
  end
end
wrap(text) click to toggle source
# File lib/ec2ssh/ssh_config.rb, line 66
    def wrap(text)
      return <<-END
#{HEADER}
# Generated by ec2ssh http://github.com/mirakui/ec2ssh
# DO NOT edit this block!
# Updated #{Time.now.iso8601}
#{text}
#{FOOTER}
      END
    end