Better parsing of regex replacements

This commit is contained in:
Dane Everitt
2020-04-12 15:57:07 -07:00
parent 9de094f078
commit 137b6cddae
2 changed files with 51 additions and 17 deletions

View File

@@ -43,26 +43,34 @@ type ConfigurationFile struct {
// Defines a single find/replace instance for a given server configuration file.
type ConfigurationFileReplacement struct {
Match string `json:"match"`
IfValue *string `json:"if_value"`
IfValue string `json:"if_value"`
ReplaceWith ReplaceValue `json:"replace_with"`
}
// Handles unmarshaling the JSON representation into a struct that provides more useful
// data to this functionality.
func (cfr *ConfigurationFileReplacement) UnmarshalJSON(data []byte) error {
if m, err := jsonparser.GetString(data, "match"); err != nil {
return err
} else {
cfr.Match = m
m, err := jsonparser.GetString(data, "match")
if err != nil {
return errors.WithStack(err)
}
cfr.Match = m
if v, dt, _, err := jsonparser.Get(data, "replace_with"); err != nil {
return err
} else {
cfr.ReplaceWith = ReplaceValue{
value: v,
valueType: dt,
}
iv, err := jsonparser.GetString(data, "if_value")
// We only check keypath here since match & replace_with should be present on all of
// them, however if_value is optional.
if err != nil && err != jsonparser.KeyPathNotFoundError {
return errors.WithStack(err)
}
cfr.IfValue = iv
rw, dt, _, err := jsonparser.Get(data, "replace_with")
if err != nil {
return errors.WithStack(err)
}
cfr.ReplaceWith = ReplaceValue{
value: rw,
valueType: dt,
}
return nil
@@ -370,7 +378,7 @@ func (f *ConfigurationFile) parsePropertiesFile(path string) error {
// Don't attempt to replace the value if we're looking for a specific value and
// it does not match. If there was no match at all in the file for this key but
// we're doing an IfValue match, do nothing.
if replace.IfValue != nil && (!ok || (ok && v != *replace.IfValue)) {
if replace.IfValue != "" && (!ok || (ok && v != replace.IfValue)) {
continue
}