-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathparsed.rb
45 lines (40 loc) · 1.72 KB
/
parsed.rb
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
# frozen_string_literal: true
require 'puppet/provider/parsedfile'
Puppet::Type.type(:postgresql_conf).provide(
:parsed,
parent: Puppet::Provider::ParsedFile,
default_target: '/etc/postgresql.conf',
filetype: :flat,
) do
desc 'Set key/values in postgresql.conf.'
text_line :comment, match: %r{^\s*#}
text_line :blank, match: %r{^\s*$}
record_line :parsed,
fields: ['key', 'value', 'comment'],
optional: ['comment'],
match: %r{^\s*([\w.]+)\s*=?\s*(.*?)(?:\s*#\s*(.*))?\s*$},
to_line: proc { |h|
# simple string and numeric values don't need to be enclosed in quotes
val = if h[:value].is_a?(Numeric)
h[:value].to_s
elsif h[:value].is_a?(Array)
# multiple listen_addresses specified as a string containing a comma-speparated list
h[:value].join(', ')
else
h[:value]
end
dontneedquote = val.match(%r{^(\d+.?\d+|\w+)$})
dontneedequal = h[:key].match(%r{^(include|include_if_exists)$}i)
str = h[:key].downcase # normalize case
str += dontneedequal ? ' ' : ' = '
str += "'" unless dontneedquote && !dontneedequal
str += val
str += "'" unless dontneedquote && !dontneedequal
str += " # #{h[:comment]}" unless h[:comment].nil? || h[:comment] == :absent
str
},
post_parse: proc { |h|
h[:key].downcase! # normalize case
h[:value].gsub!(%r{(^'|'$)}, '') # strip out quotes
}
end