From 0e3b994e84d73bf837ac2be8a642c6181c7b9cb0 Mon Sep 17 00:00:00 2001 From: John Schnake Date: Sun, 10 Apr 2022 15:27:41 -0500 Subject: [PATCH] Fix linting --- flag_groups.go | 17 +++++++++++------ flag_groups_test.go | 12 ++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/flag_groups.go b/flag_groups.go index be8ff66a5..1e9d45691 100644 --- a/flag_groups.go +++ b/flag_groups.go @@ -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) + } } } @@ -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) + } } } @@ -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{} @@ -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 { @@ -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 { @@ -151,4 +156,4 @@ func sortedKeys(m map[string]map[string]bool) ([]string) { } sort.Strings(keys) return keys -} \ No newline at end of file +} diff --git a/flag_groups_test.go b/flag_groups_test.go index 515991e3e..d4053dd0b 100644 --- a/flag_groups_test.go +++ b/flag_groups_test.go @@ -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"}, @@ -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",