Class: Sambot::Domain::Workstations::SshConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sambot/domain/workstations/ssh_config_file.rb

Instance Method Summary collapse

Constructor Details

#initializeSshConfigFile

Returns a new instance of SshConfigFile.



6
7
8
9
10
11
12
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 6

def initialize
  @make_backups = true
  @header_lines = []
  @sections = []
  @sections_by_name = {}
  read_config
end

Instance Method Details

#add_alias(host_nick, new_alias) ⇒ Object



61
62
63
64
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 61

def add_alias(host_nick, new_alias)
  section = @sections_by_name[host_nick] || add_section(host_nick)
  section.aliases.push(new_alias) unless section.aliases.member?(new_alias)
end

#add_section(name) ⇒ Object



22
23
24
25
26
27
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 22

def add_section(name)
  section = SshConfigSection.new(name)
  @sections << section
  @sections_by_name[section.name] = section
  section
end

#dumpObject



50
51
52
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 50

def dump
  to_text([@header_lines, @sections].flatten)
end

#read_configObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 29

def read_config
  current_section = nil
  IO.readlines(File.expand_path("~/.ssh/config")).each_with_index do |line, i|
    line.rstrip!
    if line =~ /\bHost\s+(.+)/
      current_section = add_section($1)
    else
      if current_section
        current_section.lines << line
      else
        @header_lines << line
      end
    end
  end
end

#rm(host_nick) ⇒ Object



54
55
56
57
58
59
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 54

def rm(host_nick)
  if @sections_by_name.key?(host_nick)
    @sections_by_name.delete host_nick
    @sections.delete_at(@sections.index{|s| s.name == host_nick})
  end
end

#saveObject



66
67
68
69
70
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 66

def save
  File.open(File.expand_path("~/.ssh/config"), "w") do |file|
    file.puts dump
  end
end

#sectionsObject



14
15
16
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 14

def sections
  @sections
end

#sections_by_nameObject



18
19
20
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 18

def sections_by_name
  @sections_by_name
end

#set(host_nick, key, value) ⇒ Object



45
46
47
48
# File 'lib/sambot/domain/workstations/ssh_config_file.rb', line 45

def set(host_nick, key, value)
  section = @sections_by_name[host_nick] || add_section(host_nick)
  section[key] = value
end