Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -29,6 +30,7 @@ const (
Ini = "ini"
Json = "json"
Xml = "xml"
Toml = "toml"
)

type ReplaceValue struct {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
Loading