Apply the case of the CLI flags for the configuration
This commit is contained in:
parent
cd164de776
commit
91e63dea47
5 changed files with 174 additions and 43 deletions
|
@ -136,6 +136,20 @@ func TestDecode(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map string case sensitive",
|
||||
args: []string{"--foo.caseSensitiveName=barBoo"},
|
||||
element: &struct {
|
||||
Foo map[string]string
|
||||
}{},
|
||||
expected: &struct {
|
||||
Foo map[string]string
|
||||
}{
|
||||
Foo: map[string]string{
|
||||
"caseSensitiveName": "barBoo",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map struct",
|
||||
args: []string{"--foo.name.value=bar"},
|
||||
|
|
|
@ -16,6 +16,7 @@ func Parse(args []string, element interface{}) (map[string]string, error) {
|
|||
flagTypes: getFlagTypes(element),
|
||||
args: args,
|
||||
values: make(map[string]string),
|
||||
keys: make(map[string]string),
|
||||
}
|
||||
|
||||
for {
|
||||
|
@ -35,6 +36,7 @@ type flagSet struct {
|
|||
flagTypes map[string]reflect.Kind
|
||||
args []string
|
||||
values map[string]string
|
||||
keys map[string]string
|
||||
}
|
||||
|
||||
func (f *flagSet) parseOne() (bool, error) {
|
||||
|
@ -78,7 +80,8 @@ func (f *flagSet) parseOne() (bool, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
if f.flagTypes[name] == reflect.Bool || f.flagTypes[name] == reflect.Ptr {
|
||||
n := strings.ToLower(name)
|
||||
if f.flagTypes[n] == reflect.Bool || f.flagTypes[n] == reflect.Ptr {
|
||||
f.setValue(name, "true")
|
||||
return true, nil
|
||||
}
|
||||
|
@ -98,13 +101,20 @@ func (f *flagSet) parseOne() (bool, error) {
|
|||
}
|
||||
|
||||
func (f *flagSet) setValue(name string, value string) {
|
||||
n := strings.ToLower(parser.DefaultRootName + "." + name)
|
||||
v, ok := f.values[n]
|
||||
srcKey := parser.DefaultRootName + "." + name
|
||||
neutralKey := strings.ToLower(srcKey)
|
||||
|
||||
if ok && f.flagTypes[name] == reflect.Slice {
|
||||
f.values[n] = v + "," + value
|
||||
key, ok := f.keys[neutralKey]
|
||||
if !ok {
|
||||
f.keys[neutralKey] = srcKey
|
||||
key = srcKey
|
||||
}
|
||||
|
||||
v, ok := f.values[key]
|
||||
if ok && f.flagTypes[strings.ToLower(name)] == reflect.Slice {
|
||||
f.values[key] = v + "," + value
|
||||
return
|
||||
}
|
||||
|
||||
f.values[n] = value
|
||||
f.values[key] = value
|
||||
}
|
||||
|
|
|
@ -29,6 +29,16 @@ func TestParse(t *testing.T) {
|
|||
"traefik.foo": "true",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "bool value capitalized",
|
||||
args: []string{"--Foo"},
|
||||
element: &struct {
|
||||
Foo bool
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.Foo": "true",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "equal",
|
||||
args: []string{"--foo=bar"},
|
||||
|
@ -39,6 +49,16 @@ func TestParse(t *testing.T) {
|
|||
"traefik.foo": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "equal",
|
||||
args: []string{"--Foo=Bar"},
|
||||
element: &struct {
|
||||
Foo string
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.Foo": "Bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "space separated",
|
||||
args: []string{"--foo", "bar"},
|
||||
|
@ -49,6 +69,16 @@ func TestParse(t *testing.T) {
|
|||
"traefik.foo": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "space separated capitalized",
|
||||
args: []string{"--Foo", "Bar"},
|
||||
element: &struct {
|
||||
Foo string
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.Foo": "Bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "space separated with end of parameter",
|
||||
args: []string{"--foo=bir", "--", "--bar"},
|
||||
|
@ -91,6 +121,16 @@ func TestParse(t *testing.T) {
|
|||
"traefik.foo.name": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map string capitalized",
|
||||
args: []string{"--foo.Name=Bar"},
|
||||
element: &struct {
|
||||
Foo map[string]string
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.Name": "Bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map struct",
|
||||
args: []string{"--foo.name.value=bar"},
|
||||
|
@ -199,6 +239,50 @@ func TestParse(t *testing.T) {
|
|||
"traefik.foo": "true",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map string case sensitive",
|
||||
args: []string{"--foo.caseSensitiveName=barBoo"},
|
||||
element: &struct {
|
||||
Foo map[string]string
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.caseSensitiveName": "barBoo",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map struct with sub-map case senstitive",
|
||||
args: []string{"--foo.Name1.bar.name2.value=firstValue", "--foo.naMe1.bar.name2.value=secondValue"},
|
||||
element: &struct {
|
||||
Foo map[string]struct {
|
||||
Bar map[string]struct{ Value string }
|
||||
}
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.Name1.bar.name2.value": "secondValue",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "map struct with sub-map and different case",
|
||||
args: []string{"--foo.Name1.bar.name2.value=firstValue", "--foo.naMe1.bar.name2.value=secondValue"},
|
||||
element: &struct {
|
||||
Foo map[string]struct {
|
||||
Bar map[string]struct{ Value string }
|
||||
}
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.foo.Name1.bar.name2.value": "secondValue",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "slice with several flags 2 and different cases.",
|
||||
args: []string{"--foo", "bar", "--Foo", "baz"},
|
||||
element: &struct {
|
||||
Foo []string
|
||||
}{},
|
||||
expected: map[string]string{
|
||||
"traefik.foo": "bar,baz",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue