|
| 1 | +/* |
| 2 | +Copyright 2025 Red Hat |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | +) |
| 24 | + |
| 25 | +type IniOption struct { |
| 26 | + Section string |
| 27 | + Key string |
| 28 | + Value string |
| 29 | +} |
| 30 | + |
| 31 | +// Define static errors at package level |
| 32 | +var ( |
| 33 | + ErrKeyAlreadyExists = errors.New("key already exists in section") |
| 34 | + ErrCouldNotPatchSection = errors.New("could not patch target section") |
| 35 | +) |
| 36 | + |
| 37 | +// repr - print key: value in .ini format |
| 38 | +func (i *IniOption) repr() string { |
| 39 | + return fmt.Sprintf("%s = %s", i.Key, i.Value) |
| 40 | +} |
| 41 | + |
| 42 | +// ExtendCustomServiceConfig - customServiceConfig is tokenized and parsed in a |
| 43 | +// loop where we keep track of two indexes: |
| 44 | +// - index: keep track of the current extracted token |
| 45 | +// - sectionIndex: when we detect a [<section>] within a token, we save the index |
| 46 | +// and we update it with the next section when is detected. This way we can |
| 47 | +// make sure to evaluate only the keys of the target section |
| 48 | +// |
| 49 | +// when an invalid case is detected, we return the customServiceConfig string |
| 50 | +// unchanged, otherwise the new key=value is appended as per the IniOption struct |
| 51 | +func ExtendCustomServiceConfig( |
| 52 | + iniString string, |
| 53 | + customServiceConfigExtend IniOption, |
| 54 | +) (string, error) { |
| 55 | + // customServiceConfig is empty |
| 56 | + if len(iniString) == 0 { |
| 57 | + return iniString, nil |
| 58 | + } |
| 59 | + // Position where insert new option (-1 = target section not found) |
| 60 | + index := -1 |
| 61 | + // Current section header position (-1 = no section found) |
| 62 | + sectionIndex := -1 |
| 63 | + svcConfigLines := strings.Split(iniString, "\n") |
| 64 | + sectionName := "" |
| 65 | + for idx, rawLine := range svcConfigLines { |
| 66 | + line := strings.TrimSpace(rawLine) |
| 67 | + token := strings.TrimSpace(strings.SplitN(line, "=", 2)[0]) |
| 68 | + |
| 69 | + if token == "" || strings.HasPrefix(token, "#") { |
| 70 | + // Skip blank lines and comments |
| 71 | + continue |
| 72 | + } |
| 73 | + if strings.HasPrefix(token, "[") && strings.HasSuffix(token, "]") { |
| 74 | + // Note the section name before looking for a backend_name |
| 75 | + sectionName = strings.Trim(token, "[]") |
| 76 | + sectionIndex = idx |
| 77 | + // increment the index (as an offset) only when a section is found |
| 78 | + if sectionName == customServiceConfigExtend.Section { |
| 79 | + index = idx + 1 |
| 80 | + } |
| 81 | + } |
| 82 | + // Check if key already exists in target section |
| 83 | + if token == customServiceConfigExtend.Key && sectionIndex > -1 && |
| 84 | + sectionName == customServiceConfigExtend.Section { |
| 85 | + errMsg := fmt.Errorf("%w: key %s in section %s", ErrKeyAlreadyExists, token, sectionName) |
| 86 | + return iniString, errMsg |
| 87 | + } |
| 88 | + } |
| 89 | + // index didn't progress during the customServiceConfig scan: |
| 90 | + // return unchanged, but no error |
| 91 | + if index == -1 { |
| 92 | + errMsg := fmt.Errorf("%w: %s", ErrCouldNotPatchSection, customServiceConfigExtend.Section) |
| 93 | + return iniString, errMsg |
| 94 | + } |
| 95 | + // index has a valid value and it is used as a pivot to inject the new ini |
| 96 | + // option right after the section |
| 97 | + var svcExtended []string |
| 98 | + svcExtended = append(svcExtended, svcConfigLines[:index]...) |
| 99 | + svcExtended = append(svcExtended, []string{customServiceConfigExtend.repr()}...) |
| 100 | + svcExtended = append(svcExtended, svcConfigLines[index:]...) |
| 101 | + return strings.Join(svcExtended, "\n"), nil |
| 102 | +} |
0 commit comments