Skip to content

Commit

Permalink
chore(config): Deprecate --value flag in favor of --var
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 27, 2024
1 parent 8abbf6a commit c7bf218
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 157 deletions.
2 changes: 1 addition & 1 deletion docs/yampl.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ yampl [files | dirs] [-v key=value...] [flags]
-p, --prefix string Template comments must begin with this prefix. The beginning '#' is implied. (default "#yampl")
--right-delim string Override template right delimiter (default "}}")
-s, --strip Strip template comments from output
-v, --value stringToString Define a template variable. Can be used more than once. (default [])
-v, --var stringToString Define a template variable. Can be used more than once. (default [])
--version version for yampl
```

4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type Config struct {
valuesStringToString *flag.StringToString
Values Values
Vars Vars

Inplace bool
Prefix string
Expand All @@ -28,7 +28,7 @@ type Config struct {
func New() *Config {
return &Config{
valuesStringToString: &flag.StringToString{},
Values: make(Values),
Vars: make(Vars),

Prefix: "#yampl",
LeftDelim: "{{",
Expand Down
8 changes: 6 additions & 2 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

const (
InplaceFlag = "inplace"
VarFlag = "var"
ValueFlag = "value"
ValueFlagShort = "v"
RecursiveFlag = "recursive"
PrefixFlag = "prefix"
LeftDelimFlag = "left-delim"
Expand All @@ -27,7 +27,11 @@ const (

func (c *Config) RegisterFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&c.Inplace, InplaceFlag, "i", c.Inplace, "Edit files in place")
cmd.Flags().VarP(c.valuesStringToString, ValueFlag, ValueFlagShort, "Define a template variable. Can be used more than once.")
cmd.Flags().Var(c.valuesStringToString, ValueFlag, "Define a template variable. Can be used more than once.")
cmd.Flags().VarP(c.valuesStringToString, VarFlag, "v", "Define a template variable. Can be used more than once.")
if err := cmd.Flags().MarkDeprecated(ValueFlag, "use --"+VarFlag+" instead"); err != nil {
panic(err)
}
cmd.Flags().BoolP(RecursiveFlag, "r", true, "Recursively update yaml files in the given directory")
if err := cmd.Flags().MarkDeprecated(RecursiveFlag, cmd.Name()+" will always recurse if a given path is a directory"); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Config) Load(cmd *cobra.Command) error {
c.Prefix = "#" + c.Prefix
}

c.Values.Fill(c.valuesStringToString.Values())
c.Vars.Fill(c.valuesStringToString.Values())

if f := cmd.Flags().Lookup(FailFlag); f.Changed {
val, err := cmd.Flags().GetBool(FailFlag)
Expand Down
42 changes: 0 additions & 42 deletions internal/config/values.go

This file was deleted.

104 changes: 0 additions & 104 deletions internal/config/values_test.go

This file was deleted.

42 changes: 42 additions & 0 deletions internal/config/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"strings"
)

type Vars map[string]any

func (vars Vars) Fill(src map[string]string) {
for key, val := range src {
split := strings.Split(key, ".")
if len(split) == 1 {
vars[key] = val
} else {
vars.SetNested(val, split...)
}
}
}

func (vars Vars) SetNested(v any, k ...string) {
setNested(vars, v, k...)
}

func setNested(vars Vars, val any, keys ...string) {
key := keys[0]
if len(keys) == 1 {
vars[key] = val
} else {
if _, ok := vars[key]; !ok {
vars[key] = Vars{}
}
setNested(vars[key].(Vars), val, keys[1:]...)
}
}

func (vars Vars) Val() any {
return vars["Value"]
}

func (vars Vars) V() any {
return vars["Value"]
}
104 changes: 104 additions & 0 deletions internal/config/vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestVars_Fill(t *testing.T) {
type args struct {
rawValues map[string]string
}
tests := []struct {
name string
vars Vars
args args
want Vars
}{
{"simple", make(Vars), args{map[string]string{"a": "a"}}, Vars{"a": "a"}},
{"nested", make(Vars), args{map[string]string{"a.b": "a"}}, Vars{"a": Vars{"b": "a"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.vars.Fill(tt.args.rawValues)
assert.Equal(t, tt.want, tt.vars)
})
}
}

func TestVars_SetNested(t *testing.T) {
type args struct {
v any
k []string
}
tests := []struct {
name string
vars Vars
args args
want Vars
}{
{"simple", make(Vars), args{"a", []string{"a"}}, Vars{"a": "a"}},
{"nested", make(Vars), args{"a", []string{"a", "b"}}, Vars{"a": Vars{"b": "a"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.vars.SetNested(tt.args.v, tt.args.k...)
assert.Equal(t, tt.want, tt.vars)
})
}
}

func TestVars_V(t *testing.T) {
tests := []struct {
name string
vars Vars
want any
}{
{"simple", Vars{"Value": "a"}, "a"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.vars.V()
assert.Equal(t, tt.want, got)
})
}
}

func TestVars_Val(t *testing.T) {
tests := []struct {
name string
vars Vars
want any
}{
{"simple", Vars{"Value": "a"}, "a"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.vars.Val()
assert.Equal(t, tt.want, got)
})
}
}

func Test_setNested(t *testing.T) {
type args struct {
input Vars
value any
keys []string
}
tests := []struct {
name string
args args
want Vars
}{
{"simple", args{make(Vars), "a", []string{"a"}}, Vars{"a": "a"}},
{"nested", args{make(Vars), "a", []string{"a", "b"}}, Vars{"a": Vars{"b": "a"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setNested(tt.args.input, tt.args.value, tt.args.keys...)
assert.Equal(t, tt.want, tt.args.input)
})
}
}
2 changes: 1 addition & 1 deletion internal/visitor/find_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (f FindArgs) Values() []string {
result := make([]string, 0, len(f.matches))
outer:
for key, val := range f.matches {
for kconf := range f.conf.Values {
for kconf := range f.conf.Vars {
if key == kconf {
continue outer
}
Expand Down
5 changes: 4 additions & 1 deletion internal/visitor/find_args_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
)

func RegisterCompletion(cmd *cobra.Command, conf *config.Config) {
if err := cmd.RegisterFlagCompletionFunc(config.ValueFlag, valueCompletion(conf)); err != nil {
if err := errors.Join(
cmd.RegisterFlagCompletionFunc(config.VarFlag, valueCompletion(conf)),
cmd.RegisterFlagCompletionFunc(config.ValueFlag, valueCompletion(conf)),
); err != nil {
panic(err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func (t TemplateComments) Template(name string, n *yaml.Node, tmplSrc string, tm
return NewNodeError(err, name, n)
}

if t.conf.Values != nil {
t.conf.Values["Value"] = n.Value
if t.conf.Vars != nil {
t.conf.Vars["Value"] = n.Value
}

var buf bytes.Buffer
if err = tmpl.Execute(&buf, t.conf.Values); err != nil {
if err = tmpl.Execute(&buf, t.conf.Vars); err != nil {
return NewNodeError(err, name, n)
}

Expand Down

0 comments on commit c7bf218

Please sign in to comment.