File: ini_file.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (226 lines) | stat: -rw-r--r-- 5,091 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# @api private
class Puppet::Settings::IniFile
  DEFAULT_SECTION_NAME = "main"

  def self.update(config_fh, &block)
    config = parse(config_fh)
    manipulator = Manipulator.new(config)
    yield manipulator
    config.write(config_fh)
  end

  def self.parse(config_fh)
    config = new([DefaultSection.new])
    config_fh.each_line do |line|
      case line.chomp
      when /^(\s*)\[([[:word:]]+)\](\s*)$/
        config.append(SectionLine.new($1, $2, $3))
      when /^(\s*)([[:word:]]+)(\s*=\s*)(.*?)(\s*)$/
        config.append(SettingLine.new($1, $2, $3, $4, $5))
      else
        config.append(Line.new(line))
      end
    end

    config
  end

  def initialize(lines = [])
    @lines = lines
  end

  def append(line)
    line.previous = @lines.last
    @lines << line
  end

  def delete(section, name)
    delete_offset = @lines.index(setting(section, name))
    next_offset = delete_offset + 1
    if next_offset < @lines.length
      @lines[next_offset].previous = @lines[delete_offset].previous
    end
    @lines.delete_at(delete_offset)
  end

  def insert_after(line, new_line)
    new_line.previous = line

    insertion_point = @lines.index(line)
    @lines.insert(insertion_point + 1, new_line)
    if @lines.length > insertion_point + 2
      @lines[insertion_point + 2].previous = new_line
    end
  end

  def section_lines
    @lines.select { |line| line.is_a?(SectionLine) }
  end

  def section_line(name)
    section_lines.find { |section| section.name == name }
  end

  def setting(section, name)
    settings_in(lines_in(section)).find do |line|
      line.name == name
    end
  end

  def lines_in(section_name)
    section_lines = []
    current_section_name = DEFAULT_SECTION_NAME
    @lines.each do |line|
      if line.is_a?(SectionLine)
        current_section_name = line.name
      elsif current_section_name == section_name
        section_lines << line
      end
    end

    section_lines
  end

  def settings_in(lines)
    lines.select { |line| line.is_a?(SettingLine) }
  end

  def settings_exist_in_default_section?
    lines_in(DEFAULT_SECTION_NAME).any? { |line| line.is_a?(SettingLine) }
  end

  def section_exists_with_default_section_name?
    section_lines.any? do |section|
      !section.is_a?(DefaultSection) && section.name == DEFAULT_SECTION_NAME
    end
  end

  def set_default_section_write_sectionline(value)
    index = @lines.find_index { |line| line.is_a?(DefaultSection) }
    if index
      @lines[index].write_sectionline = true
    end
  end

  def write(fh)
    # If no real section line for the default section exists, configure the
    # DefaultSection object to write its section line. (DefaultSection objects
    # don't write the section line unless explicitly configured to do so)
    if settings_exist_in_default_section? && !section_exists_with_default_section_name?
      set_default_section_write_sectionline(true)
    end

    fh.truncate(0)
    fh.rewind
    @lines.each do |line|
      line.write(fh)
    end
    fh.flush
  end

  class Manipulator
    def initialize(config)
      @config = config
    end

    def set(section, name, value)
      setting = @config.setting(section, name)
      if setting
        setting.value = value
      else
        add_setting(section, name, value)
      end
    end

    def delete(section_name, name)
      setting = @config.setting(section_name, name)
      if setting
        @config.delete(section_name, name)
        setting.to_s.chomp
      end
    end

    private

    def add_setting(section_name, name, value)
      section = @config.section_line(section_name)
      if section.nil?
        previous_line = SectionLine.new("", section_name, "")
        @config.append(previous_line)
      else
        previous_line = @config.settings_in(@config.lines_in(section_name)).last || section
      end

      @config.insert_after(previous_line, SettingLine.new("", name, " = ", value, ""))
    end
  end

  module LineNumber
    attr_accessor :previous

    def line_number
      line = 0
      previous_line = previous
      while previous_line
        line += 1
        previous_line = previous_line.previous
      end
      line
    end
  end

  Line = Struct.new(:text) do
    include LineNumber

    def to_s
      text
    end

    def write(fh)
      fh.puts(to_s)
    end
  end

  SettingLine = Struct.new(:prefix, :name, :infix, :value, :suffix) do
    include LineNumber

    def to_s
      "#{prefix}#{name}#{infix}#{value}#{suffix}"
    end

    def write(fh)
      fh.puts(to_s)
    end

    def ==(other)
      super(other) && self.line_number == other.line_number
    end
  end

  SectionLine = Struct.new(:prefix, :name, :suffix) do
    include LineNumber

    def to_s
      "#{prefix}[#{name}]#{suffix}"
    end

    def write(fh)
      fh.puts(to_s)
    end
  end

  class DefaultSection < SectionLine
    attr_accessor :write_sectionline

    def initialize
      @write_sectionline = false
      super("", DEFAULT_SECTION_NAME, "")
    end

    def write(fh)
      if @write_sectionline
        super(fh)
      end
    end
  end
end