Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
johnSchnake committed Apr 10, 2022
1 parent ea529ed commit 0e3b994
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 11 additions & 6 deletions flag_groups.go
Expand Up @@ -35,7 +35,10 @@ func (c *Command) MarkFlagsRequiredTogether(flagNames ...string) {
if f == nil {
panic(fmt.Sprintf("Failed to find flag %q and mark it as being required in a flag group", v))
}
c.Flags().SetAnnotation(v, requiredAsGroup, append(f.Annotations[requiredAsGroup], strings.Join(flagNames, " ")))
if err := c.Flags().SetAnnotation(v, requiredAsGroup, append(f.Annotations[requiredAsGroup], strings.Join(flagNames, " "))); err != nil {
// Only errs if the flag isn't found.
panic(err)
}
}
}

Expand All @@ -49,7 +52,9 @@ func (c *Command) MarkFlagsMutuallyExclusive(flagNames ...string) {
panic(fmt.Sprintf("Failed to find flag %q and mark it as being in a mutually exclusive flag group", v))
}
// Each time this is called is a single new entry; this allows it to be a member of multiple groups if needed.
c.Flags().SetAnnotation(v, mutuallyExclusive, append(f.Annotations[mutuallyExclusive], strings.Join(flagNames, " ")))
if err := c.Flags().SetAnnotation(v, mutuallyExclusive, append(f.Annotations[mutuallyExclusive], strings.Join(flagNames, " "))); err != nil {
panic(err)
}
}
}

Expand Down Expand Up @@ -100,7 +105,7 @@ func processFlagForGroupAnnotation(pflag *flag.Flag, annotation string, groupSta

func validateRequiredFlagGroups(data map[string]map[string]bool) error {
keys := sortedKeys(data)
for _, flagList := range keys{
for _, flagList := range keys {
flagnameAndStatus := data[flagList]

unset := []string{}
Expand All @@ -123,7 +128,7 @@ func validateRequiredFlagGroups(data map[string]map[string]bool) error {

func validateExclusiveFlagGroups(data map[string]map[string]bool) error {
keys := sortedKeys(data)
for _, flagList := range keys{
for _, flagList := range keys {
flagnameAndStatus := data[flagList]
var set []string
for flagname, isSet := range flagnameAndStatus {
Expand All @@ -142,7 +147,7 @@ func validateExclusiveFlagGroups(data map[string]map[string]bool) error {
return nil
}

func sortedKeys(m map[string]map[string]bool) ([]string) {
func sortedKeys(m map[string]map[string]bool) []string {
keys := make([]string, len(m))
i := 0
for k := range m {
Expand All @@ -151,4 +156,4 @@ func sortedKeys(m map[string]map[string]bool) ([]string) {
}
sort.Strings(keys)
return keys
}
}
12 changes: 6 additions & 6 deletions flag_groups_test.go
Expand Up @@ -69,16 +69,16 @@ func TestValidateFlagGroups(t *testing.T) {
args: []string{"testcmd", "--a=foo", "--c=foo", "--d=foo"},
expectErr: `if any flags in the group [a b c] are set none of the others can be; [a c] were all set`,
}, {
desc: "Validation of required groups occurs on groups in sorted order",
desc: "Validation of required groups occurs on groups in sorted order",
flagGroupsRequired: []string{"a d", "a b", "a c"},
args: []string{"testcmd", "--a=foo"},
expectErr: `if any flags in the group [a b] are set they must all be set; missing [b]`,
},{
args: []string{"testcmd", "--a=foo"},
expectErr: `if any flags in the group [a b] are set they must all be set; missing [b]`,
}, {
desc: "Validation of exclusive groups occurs on groups in sorted order",
flagGroupsExclusive: []string{"a d", "a b", "a c"},
args: []string{"testcmd", "--a=foo", "--b=foo", "--c=foo"},
expectErr: `if any flags in the group [a b] are set none of the others can be; [a b] were all set`,
},{
}, {
desc: "Persistent flags utilize both features and can fail required groups",
flagGroupsRequired: []string{"a e", "e f"},
flagGroupsExclusive: []string{"f g"},
Expand All @@ -88,7 +88,7 @@ func TestValidateFlagGroups(t *testing.T) {
desc: "Persistent flags utilize both features and can fail mutually exclusive groups",
flagGroupsRequired: []string{"a e", "e f"},
flagGroupsExclusive: []string{"f g"},
args: []string{"testcmd", "--a=foo", "--e=foo","--f=foo", "--g=foo"},
args: []string{"testcmd", "--a=foo", "--e=foo", "--f=foo", "--g=foo"},
expectErr: `if any flags in the group [f g] are set none of the others can be; [f g] were all set`,
}, {
desc: "Persistent flags utilize both features and can pass",
Expand Down

0 comments on commit 0e3b994

Please sign in to comment.