From f2c0348bd3315d928aa6d6548a5c8452f4040a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20van=20L=C3=BCck?= Date: Sat, 24 Jan 2026 23:39:01 +0100 Subject: [PATCH] Add TOML file parsing functionality to ConfigurationFile --- go.mod | 4 ++-- parser/parser.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 55510973..60d91f83 100644 --- a/go.mod +++ b/go.mod @@ -131,7 +131,7 @@ require ( github.com/nwaples/rardecode/v2 v2.1.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0 // indirect - github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 github.com/pierrec/lz4/v4 v4.1.21 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -161,7 +161,7 @@ require ( golang.org/x/mod v0.26.0 // indirect golang.org/x/net v0.42.0 // indirect golang.org/x/text v0.28.0 // indirect - golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect + golang.org/x/time v0.0.0-20220922220347-f3bd1da661af golang.org/x/tools v0.35.0 // indirect golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect diff --git a/parser/parser.go b/parser/parser.go index 45aaa67f..19b9e57f 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -14,6 +14,7 @@ import ( "github.com/goccy/go-json" "github.com/icza/dyno" "github.com/magiconair/properties" + "github.com/pelletier/go-toml/v2" "gopkg.in/ini.v1" "gopkg.in/yaml.v3" @@ -29,6 +30,7 @@ const ( Ini = "ini" Json = "json" Xml = "xml" + Toml = "toml" ) type ReplaceValue struct { @@ -227,6 +229,8 @@ func (f *ConfigurationFile) Parse(file ufs.File) error { err = f.parseIniFile(file) case Xml: err = f.parseXmlFile(file) + case Toml: + err = f.parseTomlFile(file) } return err } @@ -469,6 +473,49 @@ func (f *ConfigurationFile) parseYamlFile(file ufs.File) error { return nil } +// Parses a toml file and updates any matching key/value pairs before persisting +// it back to the disk. +func (f *ConfigurationFile) parseTomlFile(file ufs.File) error { + b, err := io.ReadAll(file) + if err != nil { + return err + } + + i := make(map[string]interface{}) + if err := toml.Unmarshal(b, &i); err != nil { + return err + } + + // Convert to JSON to reuse IterateOverJson for value replacement. + jsonBytes, err := json.Marshal(dyno.ConvertMapI2MapS(i)) + if err != nil { + return err + } + + data, err := f.IterateOverJson(jsonBytes) + if err != nil { + return err + } + + // Remarshal back to TOML format. + marshaled, err := toml.Marshal(data.Data()) + if err != nil { + return err + } + + if _, err := file.Seek(0, io.SeekStart); err != nil { + return err + } + if err := file.Truncate(0); err != nil { + return err + } + + if _, err := io.Copy(file, bytes.NewReader(marshaled)); err != nil { + return errors.Wrap(err, "parser: failed to write toml file to disk") + } + return nil +} + // Parses a text file using basic find and replace. This is a highly inefficient method of // scanning a file and performing a replacement. You should attempt to use anything other // than this function where possible.