Class: Hub::Context::SshConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/hub/context.rb

Defined Under Namespace

Classes: HostPattern

Constant Summary collapse

CONFIG_FILES =
%w(~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config)

Instance Method Summary collapse

Constructor Details

#initialize(files = nil) ⇒ SshConfig

Returns a new instance of SshConfig.



520
521
522
523
524
525
526
# File 'lib/hub/context.rb', line 520

def initialize files = nil
  @settings = Hash.new {|h,k| h[k] = {} }
  Array(files || CONFIG_FILES).each do |path|
    file = File.expand_path path
    parse_file file if File.exist? file
  end
end

Instance Method Details

#get_value(hostname, key) ⇒ Object

yields if not found



529
530
531
532
533
534
535
536
537
# File 'lib/hub/context.rb', line 529

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



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/hub/context.rb', line 571

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



596
597
598
599
600
# File 'lib/hub/context.rb', line 596

def record_setting key, value, patterns
  patterns.each do |pattern|
    @settings[pattern][key] ||= value
  end
end