Class: ConfigFile

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

Instance Method Summary collapse

Constructor Details

#initialize(file_location = '~/.ssh/config', make_backups = true) ⇒ ConfigFile

Returns a new instance of ConfigFile.



4
5
6
7
8
9
10
11
# File 'lib/config_file.rb', line 4

def initialize(file_location = '~/.ssh/config', make_backups = true)
  @config_file_location = file_location
  @make_backups = make_backups
  @header_lines = []
  @sections = []
  @sections_by_name = {}
  read_config
end

Instance Method Details

#add_alias(host_nick, new_alias) ⇒ Object



134
135
136
137
# File 'lib/config_file.rb', line 134

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



13
14
15
16
17
18
# File 'lib/config_file.rb', line 13

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

#alias!(host_nick, *args) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/config_file.rb', line 124

def alias!(host_nick, *args)
  backup if @make_backups
  while args.length > 0
    new_alias = args.shift
    section = add_alias(host_nick, new_alias)
  end
  save
  section
end

#backupObject



162
163
164
165
# File 'lib/config_file.rb', line 162

def backup
  filename = File.expand_path(@config_file_location)
  FileUtils.copy(filename, filename + "~") if File.exist?(filename)
end

#copy(old_host_nick, new_host_nick, *args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/config_file.rb', line 107

def copy(old_host_nick, new_host_nick, *args)
  if @sections_by_name.key?(old_host_nick)
    old_section = @sections_by_name[old_host_nick]
    new_section = @sections_by_name[new_host_nick] || add_section(new_host_nick)
    new_section.lines = old_section.lines.dup

    if old_section["Hostname"]
      new_section["Hostname"] = old_section["Hostname"].gsub(old_host_nick, new_host_nick)
    end

    while args.length > 0
      key, value = args.shift, args.shift
      section = set(new_host_nick, key, value)
    end
  end
end

#copy!(old_host_nick, new_host_nick, *args) ⇒ Object



101
102
103
104
105
# File 'lib/config_file.rb', line 101

def copy!(old_host_nick, new_host_nick, *args)
  backup if @make_backups
  copy(old_host_nick, new_host_nick, *args)
  save
end

#dumpObject



84
85
86
# File 'lib/config_file.rb', line 84

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

#listObject



46
47
48
# File 'lib/config_file.rb', line 46

def list()
  to_text(@sections_by_name.keys.sort.map {|name| @sections_by_name[name].header})
end

#read_configObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/config_file.rb', line 20

def read_config
  current_section = nil
  filename = File.expand_path(@config_file_location)
  return unless File.exist?(filename)
  IO.readlines(filename).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



94
95
96
97
98
99
# File 'lib/config_file.rb', line 94

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

#rm!(host_nick) ⇒ Object



88
89
90
91
92
# File 'lib/config_file.rb', line 88

def rm!(host_nick)
  backup if @make_backups
  rm(host_nick)
  save
end

#saveObject



156
157
158
159
160
# File 'lib/config_file.rb', line 156

def save
  File.open(File.expand_path(@config_file_location), "w") do |file|
    file.puts dump
  end
end

#search(text) ⇒ Object



50
51
52
# File 'lib/config_file.rb', line 50

def search(text)
  to_text(@sections.find_all {|section| section.matches?(text)}.sort_by {|section| section.name})
end

#set(host_nick, key, value) ⇒ Object



64
65
66
67
# File 'lib/config_file.rb', line 64

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

#set!(host_nick, *args) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/config_file.rb', line 54

def set!(host_nick, *args)
  backup if @make_backups
  while args.length > 0
    key, value = args.shift, args.shift
    section = set(host_nick, key, value)
  end
  save
  section
end

#show(*host_nicks) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/config_file.rb', line 38

def show(*host_nicks)
  items =  host_nicks.map {|nick|
    sections = @sections.select {|section| section.matches_exactly?(nick)}
    sections.empty? ? "# No entry found for: #{nick}" : sections
  }
  items.flatten.uniq * "\n"
end

#unalias!(*args) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/config_file.rb', line 139

def unalias!(*args)
  if(args.size >= 2)
    name = args.shift
    section = @sections_by_name[name]
  else
    section = @sections.select {|section| section.has_alias?(args[0])}.first
  end

  if(section) then
    section.aliases -= args
  else
    $stderr.puts "Failed to find section named #{name || args[0]}"
  end
  save
  section
end

#unset(host_nick, key) ⇒ Object



78
79
80
81
82
# File 'lib/config_file.rb', line 78

def unset(host_nick, key)
  if section = @sections_by_name[host_nick]
    section.unset(key)
  end
end

#unset!(host_nick, *keys) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/config_file.rb', line 69

def unset!(host_nick, *keys)
  backup if @make_backups
  while keys.length > 0
    section = unset(host_nick, keys.shift)
  end
  save
  section
end