1313 # The function parses the postgresql.conf and figures out which active settings exist in a config file and returns an array of hashes
1414 #
1515 def parse_config
16- # open the config file
17- file = File . open ( resource [ :target ] )
1816 # regex to match active keys, values and comments
1917 active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
2018 # empty array to be filled with hashes
2119 active_settings = [ ]
2220 # iterate the file and construct a hash for every matching/active setting
2321 # 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 |
2623 matches = line . match ( active_values_regex )
2724 if matches
2825 value = if matches [ :value ] . to_i . to_s == matches [ :value ]
@@ -36,7 +33,7 @@ def parse_config
3633 active_settings . push ( attributes_hash )
3734 end
3835 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 } " )
4037 active_settings
4138 end
4239
@@ -63,12 +60,11 @@ def add_header(lines)
6360
6461 # This function writes the config file, it removes the old header, adds a new one and writes the file
6562 #
66- # @param [File] the file object of the postgresql configuration file
6763 # @param [Array] lines of the parsed postgresql configuration file
68- def write_config ( file , lines )
64+ def write_config ( lines )
6965 lines = delete_header ( lines )
7066 lines = add_header ( lines )
71- File . write ( file , lines . join )
67+ File . write ( resource [ :target ] , lines . join )
7268 end
7369
7470 # check, if resource exists in postgresql.conf file
@@ -85,23 +81,21 @@ def exists?
8581 # remove resource if exists and is set to absent
8682 def destroy
8783 entry_regex = %r{#{ resource [ :key ] } .*=.*#{ resource [ :value ] } }
88- file = File . open ( resource [ :target ] )
89- lines = File . readlines ( file )
84+ lines = File . readlines ( resource [ :target ] )
9085
9186 lines . delete_if do |entry |
9287 entry . match? ( entry_regex )
9388 end
94- write_config ( file , lines )
89+ write_config ( lines )
9590 end
9691
9792 # create resource if it does not exists
9893 def create
99- file = File . open ( resource [ :target ] )
100- lines = File . readlines ( file )
94+ lines = File . readlines ( resource [ :target ] )
10195 new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
10296
10397 lines . push ( new_line )
104- write_config ( file , lines )
98+ write_config ( lines )
10599 end
106100
107101 # getter - get value of a resource
@@ -116,30 +110,28 @@ def comment
116110
117111 # setter - set value of a resource
118112 def value = ( _value )
119- file = File . open ( resource [ :target ] )
120- lines = File . readlines ( file )
113+ lines = File . readlines ( resource [ :target ] )
121114 active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
122115 new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
123116
124117 lines . each_with_index do |line , index |
125118 matches = line . to_s . match ( active_values_regex )
126119 lines [ index ] = new_line if matches && ( matches [ :key ] == resource [ :key ] && matches [ :value ] != resource [ :value ] )
127120 end
128- write_config ( file , lines )
121+ write_config ( lines )
129122 end
130123
131124 # setter - set comment of a resource
132125 def comment = ( _comment )
133- file = File . open ( resource [ :target ] )
134- lines = File . readlines ( file )
126+ lines = File . readlines ( resource [ :target ] )
135127 active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
136128 new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
137129
138130 lines . each_with_index do |line , index |
139131 matches = line . to_s . match ( active_values_regex )
140132 lines [ index ] = new_line if matches && ( matches [ :key ] == resource [ :key ] && matches [ :comment ] != resource [ :comment ] )
141133 end
142- write_config ( file , lines )
134+ write_config ( lines )
143135 end
144136
145137 private
0 commit comments