Verify that the configuration keys in the file are valid.

- Return an error if any of the keys don't match valid flags.
- Fix an issue ignoring merged values as named values.
- Fix tlsverify configuration key.
- Fix bug in mflag to avoid panics when one of the flag set doesn't have any flag.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2016-01-20 17:16:49 -05:00
parent 1fd7b5e933
commit d0585b6411
2 changed files with 30 additions and 0 deletions

View file

@ -1223,11 +1223,27 @@ func (v mergeVal) IsBoolFlag() bool {
return false
}
// Name returns the name of a mergeVal.
// If the original value had a name, return the original name,
// otherwise, return the key asinged to this mergeVal.
func (v mergeVal) Name() string {
type namedValue interface {
Name() string
}
if nVal, ok := v.Value.(namedValue); ok {
return nVal.Name()
}
return v.key
}
// Merge is an helper function that merges n FlagSets into a single dest FlagSet
// In case of name collision between the flagsets it will apply
// the destination FlagSet's errorHandling behavior.
func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
for _, fset := range flagsets {
if fset.formal == nil {
continue
}
for k, f := range fset.formal {
if _, ok := dest.formal[k]; ok {
var err error
@ -1249,6 +1265,9 @@ func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
}
newF := *f
newF.Value = mergeVal{f.Value, k, fset}
if dest.formal == nil {
dest.formal = make(map[string]*Flag)
}
dest.formal[k] = &newF
}
}

View file

@ -514,3 +514,14 @@ func TestSortFlags(t *testing.T) {
t.Fatalf("NFlag (%d) != fs.NFlag() (%d) of elements visited", nflag, fs.NFlag())
}
}
func TestMergeFlags(t *testing.T) {
base := NewFlagSet("base", ContinueOnError)
base.String([]string{"f"}, "", "")
fs := NewFlagSet("test", ContinueOnError)
Merge(fs, base)
if len(fs.formal) != 1 {
t.Fatalf("FlagCount (%d) != number (1) of elements merged", len(fs.formal))
}
}