MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / UpdateConfigurationContents

Function UpdateConfigurationContents

pkg/configfile/configfile.go:80–117  ·  view source on GitHub ↗

UpdateConfigurationContents search and replace options in a configuration file whose content is passed

(lines []string, options map[string]string)

Source from the content-addressed store, hash-verified

78// UpdateConfigurationContents search and replace options in a configuration file whose
79// content is passed
80func UpdateConfigurationContents(lines []string, options map[string]string) []string {
81 foundKeys := stringset.New()
82 index := 0
83 for _, line := range lines {
84 kv := strings.SplitN(strings.TrimSpace(line), "=", 2)
85 key := strings.TrimSpace(kv[0])
86
87 // If we find a line containing one of the option we have to manage,
88 // we replace it with the provided content
89 if value, has := options[key]; has {
90 // We output only the first occurrence of the option,
91 // discarding further occurrences
92 if foundKeys.Has(key) {
93 continue
94 }
95
96 foundKeys.Put(key)
97 lines[index] = fmt.Sprintf("%s = %s", key, escapePostgresConfLiteral(value))
98 index++
99 continue
100 }
101
102 lines[index] = line
103 index++
104 }
105 lines = lines[:index]
106
107 // Append missing options to the end of the file
108 keysList := stringset.FromKeys(options).ToSortedList()
109 for _, key := range keysList {
110 if !foundKeys.Has(key) {
111 value := options[key]
112 lines = append(lines, fmt.Sprintf("%s = %s", key, escapePostgresConfLiteral(value)))
113 }
114 }
115
116 return lines
117}
118
119// RenderPostgresConfiguration returns a PostgreSQL configuration fragment as
120// a single string: one `key = 'escaped value'\n` line per entry in options,

Callers 4

configfile_test.goFile · 0.85

Calls 2

HasMethod · 0.80

Tested by

no test coverage detected