13
13
# The function parses the postgresql.conf and figures out which active settings exist in a config file and returns an array of hashes
14
14
#
15
15
def parse_config
16
- # open the config file
17
- file = File . open ( resource [ :target ] )
18
16
# regex to match active keys, values and comments
19
17
active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
20
18
# empty array to be filled with hashes
21
19
active_settings = [ ]
22
20
# iterate the file and construct a hash for every matching/active setting
23
21
# the hash is pushed to the array and the array is returned
24
- File . foreach ( file ) . with_index do |line , index |
25
- line_number = index + 1
22
+ File . foreach ( resource [ :target ] ) . with_index ( 1 ) do |line , line_number |
26
23
matches = line . match ( active_values_regex )
27
24
if matches
28
25
value = if matches [ :value ] . to_i . to_s == matches [ :value ]
@@ -36,7 +33,7 @@ def parse_config
36
33
active_settings . push ( attributes_hash )
37
34
end
38
35
end
39
- Puppet . debug ( "DEBUG: parse_config Active Settings found in Postgreql config file: #{ active_settings } " )
36
+ Puppet . debug ( "DEBUG: parse_config Active Settings found in PostgreSQL config file: #{ active_settings } " )
40
37
active_settings
41
38
end
42
39
@@ -63,12 +60,11 @@ def add_header(lines)
63
60
64
61
# This function writes the config file, it removes the old header, adds a new one and writes the file
65
62
#
66
- # @param [File] the file object of the postgresql configuration file
67
63
# @param [Array] lines of the parsed postgresql configuration file
68
- def write_config ( file , lines )
64
+ def write_config ( lines )
69
65
lines = delete_header ( lines )
70
66
lines = add_header ( lines )
71
- File . write ( file , lines . join )
67
+ File . write ( resource [ :target ] , lines . join )
72
68
end
73
69
74
70
# check, if resource exists in postgresql.conf file
@@ -85,23 +81,21 @@ def exists?
85
81
# remove resource if exists and is set to absent
86
82
def destroy
87
83
entry_regex = %r{#{ resource [ :key ] } .*=.*#{ resource [ :value ] } }
88
- file = File . open ( resource [ :target ] )
89
- lines = File . readlines ( file )
84
+ lines = File . readlines ( resource [ :target ] )
90
85
91
86
lines . delete_if do |entry |
92
87
entry . match? ( entry_regex )
93
88
end
94
- write_config ( file , lines )
89
+ write_config ( lines )
95
90
end
96
91
97
92
# create resource if it does not exists
98
93
def create
99
- file = File . open ( resource [ :target ] )
100
- lines = File . readlines ( file )
94
+ lines = File . readlines ( resource [ :target ] )
101
95
new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
102
96
103
97
lines . push ( new_line )
104
- write_config ( file , lines )
98
+ write_config ( lines )
105
99
end
106
100
107
101
# getter - get value of a resource
@@ -116,30 +110,28 @@ def comment
116
110
117
111
# setter - set value of a resource
118
112
def value = ( _value )
119
- file = File . open ( resource [ :target ] )
120
- lines = File . readlines ( file )
113
+ lines = File . readlines ( resource [ :target ] )
121
114
active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
122
115
new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
123
116
124
117
lines . each_with_index do |line , index |
125
118
matches = line . to_s . match ( active_values_regex )
126
119
lines [ index ] = new_line if matches && ( matches [ :key ] == resource [ :key ] && matches [ :value ] != resource [ :value ] )
127
120
end
128
- write_config ( file , lines )
121
+ write_config ( lines )
129
122
end
130
123
131
124
# setter - set comment of a resource
132
125
def comment = ( _comment )
133
- file = File . open ( resource [ :target ] )
134
- lines = File . readlines ( file )
126
+ lines = File . readlines ( resource [ :target ] )
135
127
active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
136
128
new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
137
129
138
130
lines . each_with_index do |line , index |
139
131
matches = line . to_s . match ( active_values_regex )
140
132
lines [ index ] = new_line if matches && ( matches [ :key ] == resource [ :key ] && matches [ :comment ] != resource [ :comment ] )
141
133
end
142
- write_config ( file , lines )
134
+ write_config ( lines )
143
135
end
144
136
145
137
private
0 commit comments