Skip to content

Commit

Permalink
✨ Added ini file extender support
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinemartin committed Jan 24, 2023
1 parent 5cf9109 commit 8c83f71
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.0 // indirect
github.com/go-ini/ini v1.67.0
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/go-git/go-git-fixtures/v4 v4.3.1 h1:y5z6dd3qi8Hl+stezc8p3JxDkoTRqMAlK
github.com/go-git/go-git-fixtures/v4 v4.3.1/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo=
github.com/go-git/go-git/v5 v5.5.2 h1:v8lgZa5k9ylUw+OR/roJHTxR4QItsNFI5nKtAXFuynw=
github.com/go-git/go-git/v5 v5.5.2/go.mod h1:BE5hUJ5yaV2YMxhmaP4l6RBQ08kMxKSPD4BlxtH7OjI=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
Expand Down
59 changes: 59 additions & 0 deletions pkg/extras/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/go-ini/ini"
"github.com/pelletier/go-toml/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -50,6 +51,7 @@ const (
RegexExtender
JsonExtender
TomlExtender
IniExtender
)

var stringToExtenderTypeMap map[string]ExtenderType
Expand Down Expand Up @@ -442,6 +444,62 @@ func NewTomlExtender() Extender {
return &tomlExtender{}
}

///////
// TOML
///////

type iniExtender struct {
file *ini.File
}

func (e *iniExtender) SetPayload(payload []byte) (err error) {

e.file, err = ini.Load(payload)
return err
}

func (e *iniExtender) GetPayload() ([]byte, error) {
var b bytes.Buffer
_, err := e.file.WriteTo(&b)
return b.Bytes(), err
}

func (e *iniExtender) keyFromPath(path []string) (*ini.Key, error) {
if len(path) < 1 || len(path) > 2 {
return nil, fmt.Errorf("invalid path length: %d", len(path))
}
section := ""
key := path[0]
if len(path) == 2 {
section = key
key = path[1]
}
return e.file.Section(section).Key(key), nil
}

func (e *iniExtender) Get(path []string) ([]byte, error) {
k, err := e.keyFromPath(path)
if err != nil {
return nil, fmt.Errorf("while getting key at path %s", strings.Join(path, "."))
}
return []byte(k.String()), nil
}

func (e *iniExtender) Set(path []string, value any) error {
k, err := e.keyFromPath(path)
if err != nil {
return fmt.Errorf("while getting key at path %s", strings.Join(path, "."))
}

k.SetValue(string(GetByteValue(value)))

return nil
}

func NewIniExtender() Extender {
return &iniExtender{}
}

////////////
// Factories
////////////
Expand All @@ -452,6 +510,7 @@ var ExtenderFactories = map[ExtenderType]func() Extender{
RegexExtender: NewRegexExtender,
JsonExtender: NewJsonExtender,
TomlExtender: NewTomlExtender,
IniExtender: NewIniExtender,
}

func (path *ExtendedSegment) Extender(payload []byte) (Extender, error) {
Expand Down
45 changes: 45 additions & 0 deletions pkg/extras/extender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,51 @@ targetRevision = 'deploy/citest'
require.Equal("deploy/citest", string(value), "error fetching changed value")
}

func (s *ExtenderTestSuite) TestIniExtender() {
require := s.Require()
source := `
uninode = true
[common]
targetRevision = main
[apps]
enabled = true
`
expected := `uninode = true
[common]
targetRevision = deploy/citest
[apps]
enabled = true
`

p := `!!ini.common.targetRevision`
path := kyaml_utils.SmarterPathSplitter(p, ".")

extensions := []*ExtendedSegment{}
prefix, err := splitExtendedPath(path, &extensions)
require.NoError(err)
require.Len(prefix, 0, "There should be no prefix")
require.Len(extensions, 1, "There should be 2 extensions")
require.Equal("ini", extensions[0].Encoding, "The first extension should be ini")

iniXP := extensions[0]
iniExt, err := iniXP.Extender([]byte(source))
require.NoError(err)
value, err := iniExt.Get(iniXP.Path)
require.NoError(err)
require.Equal("main", string(value), "error fetching value")
require.NoError(iniExt.Set(iniXP.Path, []byte("deploy/citest")))

modified, err := iniExt.GetPayload()
require.NoError(err)
require.Equal(expected, string(modified), "final ini")

value, err = iniExt.Get(iniXP.Path)
require.NoError(err)
require.Equal("deploy/citest", string(value), "error fetching changed value")
}

func TestExtender(t *testing.T) {
suite.Run(t, new(ExtenderTestSuite))
}
5 changes: 3 additions & 2 deletions pkg/extras/extendertype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8c83f71

Please sign in to comment.