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 |
22
+ File . foreach ( resource [ :target ] ) . with_index do |line , index |
25
23
line_number = index + 1
26
24
matches = line . match ( active_values_regex )
27
25
if matches
@@ -63,12 +61,11 @@ def add_header(lines)
63
61
64
62
# This function writes the config file, it removes the old header, adds a new one and writes the file
65
63
#
66
- # @param [File] the file object of the postgresql configuration file
67
64
# @param [Array] lines of the parsed postgresql configuration file
68
- def write_config ( file , lines )
65
+ def write_config ( lines )
69
66
lines = delete_header ( lines )
70
67
lines = add_header ( lines )
71
- File . write ( file , lines . join )
68
+ File . write ( resource [ :target ] , lines . join )
72
69
end
73
70
74
71
# check, if resource exists in postgresql.conf file
@@ -85,23 +82,21 @@ def exists?
85
82
# remove resource if exists and is set to absent
86
83
def destroy
87
84
entry_regex = %r{#{ resource [ :key ] } .*=.*#{ resource [ :value ] } }
88
- file = File . open ( resource [ :target ] )
89
- lines = File . readlines ( file )
85
+ lines = File . readlines ( resource [ :target ] )
90
86
91
87
lines . delete_if do |entry |
92
88
entry . match? ( entry_regex )
93
89
end
94
- write_config ( file , lines )
90
+ write_config ( lines )
95
91
end
96
92
97
93
# create resource if it does not exists
98
94
def create
99
- file = File . open ( resource [ :target ] )
100
- lines = File . readlines ( file )
95
+ lines = File . readlines ( resource [ :target ] )
101
96
new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
102
97
103
98
lines . push ( new_line )
104
- write_config ( file , lines )
99
+ write_config ( lines )
105
100
end
106
101
107
102
# getter - get value of a resource
@@ -116,30 +111,28 @@ def comment
116
111
117
112
# setter - set value of a resource
118
113
def value = ( _value )
119
- file = File . open ( resource [ :target ] )
120
- lines = File . readlines ( file )
114
+ lines = File . readlines ( resource [ :target ] )
121
115
active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
122
116
new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
123
117
124
118
lines . each_with_index do |line , index |
125
119
matches = line . to_s . match ( active_values_regex )
126
120
lines [ index ] = new_line if matches && ( matches [ :key ] == resource [ :key ] && matches [ :value ] != resource [ :value ] )
127
121
end
128
- write_config ( file , lines )
122
+ write_config ( lines )
129
123
end
130
124
131
125
# setter - set comment of a resource
132
126
def comment = ( _comment )
133
- file = File . open ( resource [ :target ] )
134
- lines = File . readlines ( file )
127
+ lines = File . readlines ( resource [ :target ] )
135
128
active_values_regex = %r{^\s *(?<key>[\w .]+)\s *=?\s *(?<value>.*?)(?:\s *#\s *(?<comment>.*))?\s *$}
136
129
new_line = line ( key : resource [ :key ] , value : resource [ :value ] , comment : resource [ :comment ] )
137
130
138
131
lines . each_with_index do |line , index |
139
132
matches = line . to_s . match ( active_values_regex )
140
133
lines [ index ] = new_line if matches && ( matches [ :key ] == resource [ :key ] && matches [ :comment ] != resource [ :comment ] )
141
134
end
142
- write_config ( file , lines )
135
+ write_config ( lines )
143
136
end
144
137
145
138
private
0 commit comments