Class: Hub::SshConfig
- Inherits:
-
Object
- Object
- Hub::SshConfig
- Defined in:
- lib/hub/ssh_config.rb
Overview
Reads ssh configuration files and records each setting under its host pattern so it can be looked up by hostname.
Defined Under Namespace
Classes: HostPattern
Constant Summary collapse
- CONFIG_FILES =
%w(~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config)
Instance Method Summary collapse
-
#get_value(hostname, key) ⇒ Object
Public: Get a setting as it would apply to a specific hostname.
-
#initialize(files = nil) ⇒ SshConfig
constructor
A new instance of SshConfig.
- #parse_file(file) ⇒ Object
- #record_setting(key, value, patterns) ⇒ Object
Constructor Details
#initialize(files = nil) ⇒ SshConfig
Returns a new instance of SshConfig.
7 8 9 10 11 12 13 |
# File 'lib/hub/ssh_config.rb', line 7 def initialize files = nil @settings = Hash.new {|h,k| h[k] = {} } Array(files || CONFIG_FILES).each do |path| file = File. path parse_file file if File.exist? file end end |
Instance Method Details
#get_value(hostname, key) ⇒ Object
Public: Get a setting as it would apply to a specific hostname.
Yields if not found.
18 19 20 21 22 23 24 25 26 |
# File 'lib/hub/ssh_config.rb', line 18 def get_value hostname, key key = key.to_s.downcase @settings.each do |pattern, settings| if pattern.match? hostname and found = settings[key] return found end end yield end |
#parse_file(file) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/hub/ssh_config.rb', line 60 def parse_file file host_patterns = [HostPattern.new('*')] IO.foreach(file) do |line| case line when /^\s*(#|$)/ then next when /^\s*(\S+)\s*=/ key, value = $1, $' else key, value = line.strip.split(/\s+/, 2) end next if value.nil? key.downcase! value = $1 if value =~ /^"(.*)"$/ value.chomp! if 'host' == key host_patterns = value.split(/\s+/).map {|p| HostPattern.new p } else record_setting key, value, host_patterns end end end |
#record_setting(key, value, patterns) ⇒ Object
85 86 87 88 89 |
# File 'lib/hub/ssh_config.rb', line 85 def record_setting key, value, patterns patterns.each do |pattern| @settings[pattern][key] ||= value end end |