Skip to content

Commit

Permalink
feat: Add fromIni template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 14, 2022
1 parent ffdc746 commit 02b8954
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `fromIni` *initext*

`fromIni` returns the parsed value of *initext*.

!!! example

```
{{ (fromIni "[section]\nkey = value").section.key }}
```
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ nav:
- comment: reference/templates/functions/comment.md
- decrypt: reference/templates/functions/decrypt.md
- encrypt: reference/templates/functions/encrypt.md
- fromIni: reference/templates/functions/fromIni.md
- fromToml: reference/templates/functions/fromToml.md
- fromYaml: reference/templates/functions/fromYaml.md
- glob: reference/templates/functions/glob.md
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ func newConfig(options ...configOption) (*Config, error) {
"comment": c.commentTemplateFunc,
"decrypt": c.decryptTemplateFunc,
"encrypt": c.encryptTemplateFunc,
"fromIni": c.fromIniTemplateFunc,
"fromToml": c.fromTomlTemplateFunc,
"fromYaml": c.fromYamlTemplateFunc,
"gitHubKeys": c.gitHubKeysTemplateFunc,
Expand Down
28 changes: 28 additions & 0 deletions pkg/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/bmatcuk/doublestar/v4"
"github.com/bradenhilton/mozillainstallhash"
"gopkg.in/ini.v1"
"howett.net/plist"

"github.com/twpayne/chezmoi/v2/pkg/chezmoi"
Expand All @@ -29,6 +30,14 @@ func (c *Config) commentTemplateFunc(prefix, s string) string {
return startOfLineRx.ReplaceAllString(s, prefix)
}

func (c *Config) fromIniTemplateFunc(s string) map[string]any {
file, err := ini.Load([]byte(s))
if err != nil {
panic(err)
}
return iniFileToMap(file)
}

func (c *Config) fromTomlTemplateFunc(s string) any {
var data any
if err := chezmoi.FormatTOML.Unmarshal([]byte(s), &data); err != nil {
Expand Down Expand Up @@ -211,3 +220,22 @@ func (c *Config) toYamlTemplateFunc(data any) string {
}
return string(yaml)
}

func iniFileToMap(file *ini.File) map[string]any {
m := make(map[string]any)
for _, section := range file.Sections() {
m[section.Name()] = iniSectionToMap(section)
}
return m
}

func iniSectionToMap(section *ini.Section) map[string]any {
m := make(map[string]any)
for _, s := range section.ChildSections() {
m[s.Name()] = iniSectionToMap(s)
}
for _, k := range section.Keys() {
m[k.Name()] = k.Value()
}
return m
}
39 changes: 39 additions & 0 deletions pkg/cmd/templatefuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,51 @@ package cmd

import (
"errors"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/ini.v1"

"github.com/twpayne/chezmoi/v2/pkg/chezmoitest"
)

func TestFromIniTemplateFunc(t *testing.T) {
for i, tc := range []struct {
text string
expected map[string]any
}{
{
text: chezmoitest.JoinLines(
`key = value`,
),
expected: map[string]any{
ini.DefaultSection: map[string]any{
"key": "value",
},
},
},
{
text: chezmoitest.JoinLines(
`[section]`,
`sectionKey = sectionValue`,
),
expected: map[string]any{
ini.DefaultSection: map[string]any{},
"section": map[string]any{
"sectionKey": "sectionValue",
},
},
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
c := &Config{}
assert.Equal(t, tc.expected, c.fromIniTemplateFunc(tc.text))
})
}
}

func TestQuoteListTemplateFunc(t *testing.T) {
c, err := newConfig()
require.NoError(t, err)
Expand Down

0 comments on commit 02b8954

Please sign in to comment.