From be543ce3e0135c2a2090c21d2f0e9a747d063fa1 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 4 Nov 2021 13:24:12 -0600 Subject: [PATCH] parser(ini): allow setting the section name In an egg replacer putting `[]` will cause it to not be split at the first dot. Before this change putting `"find": { "/Script/Engine.GameSession.MaxPlayers": "" }` would make the section name `/Script/Engine` and the key `GameSession.MaxPlayers`. After this change, the same behavior occurs, but if you wrap the key in `[]` it will set the section name properly, for example `"find": { "[/Script/Engine.GameSession].MaxPlayers": "" }` would make the sesion name `/Script/Engine.GameSession` and the key `MaxPlayers`. Closes https://github.com/pterodactyl/panel/issues/2533 --- parser/parser.go | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 78c7608..44ee7d4 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -212,7 +212,7 @@ func (f *ConfigurationFile) Parse(path string, internal bool) error { } b := strings.TrimSuffix(path, filepath.Base(path)) - if err := os.MkdirAll(b, 0755); err != nil { + if err := os.MkdirAll(b, 0o755); err != nil { return errors.WithMessage(err, "failed to create base directory for missing configuration file") } else { if _, err := os.Create(path); err != nil { @@ -229,7 +229,7 @@ func (f *ConfigurationFile) Parse(path string, internal bool) error { // Parses an xml file. func (f *ConfigurationFile) parseXmlFile(path string) error { doc := etree.NewDocument() - file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) + file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644) if err != nil { return err } @@ -322,7 +322,7 @@ func (f *ConfigurationFile) parseIniFile(path string) error { // Ini package can't handle a non-existent file, so handle that automatically here // by creating it if not exists. Then, immediately close the file since we will use // other methods to write the new contents. - file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) + file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644) if err != nil { return err } @@ -334,7 +334,30 @@ func (f *ConfigurationFile) parseIniFile(path string) error { } for _, replacement := range f.Replace { - path := strings.SplitN(replacement.Match, ".", 2) + var ( + path []string + bracketDepth int + v []int32 + ) + for _, c := range replacement.Match { + switch c { + case '[': + bracketDepth++ + case ']': + bracketDepth-- + case '.': + if bracketDepth > 0 || len(path) == 1 { + v = append(v, c) + continue + } + path = append(path, string(v)) + v = v[:0] + default: + v = append(v, c) + } + } + path = append(path, string(v)) + // path := strings.SplitN(replacement.Match, ".", 2) value, err := f.LookupConfigurationValue(replacement) if err != nil { @@ -387,7 +410,7 @@ func (f *ConfigurationFile) parseJsonFile(path string) error { } output := []byte(data.StringIndent("", " ")) - return ioutil.WriteFile(path, output, 0644) + return ioutil.WriteFile(path, output, 0o644) } // Parses a yaml file and updates any matching key/value pairs before persisting @@ -424,7 +447,7 @@ func (f *ConfigurationFile) parseYamlFile(path string) error { return err } - return ioutil.WriteFile(path, marshaled, 0644) + return ioutil.WriteFile(path, marshaled, 0o644) } // Parses a text file using basic find and replace. This is a highly inefficient method of @@ -449,7 +472,7 @@ func (f *ConfigurationFile) parseTextFile(path string) error { } } - if err := ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644); err != nil { + if err := ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), 0o644); err != nil { return err } @@ -545,7 +568,7 @@ func (f *ConfigurationFile) parsePropertiesFile(path string) error { } // Open the file for writing. - w, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + w, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644) if err != nil { return err }