Class: Hub::SshConfig
- Inherits:
-
Object
- Object
- Hub::SshConfig
- Defined in:
- lib/hub/ssh_config.rb
Overview
Reads ssh_config(5) files and records “Host” to “HostName” mappings to provide resolving of ssh aliases.
Constant Summary collapse
- CONFIG_FILES =
%w(~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config)
- CONFIG_RE =
/^\s*(Host|HostName)\s+/
Instance Method Summary collapse
-
#get_value(host, key, &fallback) ⇒ 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.
8 9 10 11 12 13 14 |
# File 'lib/hub/ssh_config.rb', line 8 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.readable?(file) end end |
Instance Method Details
#get_value(host, key, &fallback) ⇒ Object
Public: Get a setting as it would apply to a specific hostname.
Yields if not found.
19 20 21 22 |
# File 'lib/hub/ssh_config.rb', line 19 def get_value(host, key, &fallback) host = host.to_s.downcase @settings[host].fetch(key.to_sym, &fallback) end |
#parse_file(file) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hub/ssh_config.rb', line 24 def parse_file file host_patterns = %w[*] IO.foreach(file) do |line| next unless line =~ CONFIG_RE key, value = $1.to_sym, $'.chomp value.gsub!(/^"|"$/, '') if :Host == key host_patterns = value.downcase.split(/\s+/) else record_setting key, value, host_patterns end end end |
#record_setting(key, value, patterns) ⇒ Object
40 41 42 43 44 |
# File 'lib/hub/ssh_config.rb', line 40 def record_setting key, value, patterns patterns.each do |pattern| @settings[pattern][key] ||= value end end |