From d7bd472f3068f30389ca9f716f9cf52c6a39347b Mon Sep 17 00:00:00 2001 From: Samuel Stoltenberg Date: Mon, 6 Mar 2023 11:04:37 -0600 Subject: [PATCH] Fix linting --- altsrc/flag.go | 10 ++--- app.go | 24 ----------- app_test.go | 64 +++++++++++++++-------------- category.go | 2 +- context_test.go | 8 ++-- flag_test.go | 42 +++++++++++++++---- help.go | 22 +++++----- internal/build/build.go | 30 +++++++------- internal/example-cli/example-cli.go | 2 +- suggestions_test.go | 8 +++- 10 files changed, 110 insertions(+), 102 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index 084ba3f9e1..d7c4eb2f8e 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -109,10 +109,10 @@ func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSour continue } underlyingFlag.Value = &sliceValue - f.set.Set(n, sliceValue.Serialize()) + _ = f.set.Set(n, sliceValue.Serialize()) } if f.Destination != nil { - f.Destination.Set(sliceValue.Serialize()) + _ = f.Destination.Set(sliceValue.Serialize()) } } return nil @@ -143,7 +143,7 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC underlyingFlag.Value = &sliceValue } if f.Destination != nil { - f.Destination.Set(sliceValue.Serialize()) + _ = f.Destination.Set(sliceValue.Serialize()) } } return nil @@ -174,7 +174,7 @@ func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourc underlyingFlag.Value = &sliceValue } if f.Destination != nil { - f.Destination.Set(sliceValue.Serialize()) + _ = f.Destination.Set(sliceValue.Serialize()) } } return nil @@ -205,7 +205,7 @@ func (f *Float64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSou underlyingFlag.Value = &sliceValue } if f.Destination != nil { - f.Destination.Set(sliceValue.Serialize()) + _ = f.Destination.Set(sliceValue.Serialize()) } } return nil diff --git a/app.go b/app.go index e07dd7b192..f69a1939b1 100644 --- a/app.go +++ b/app.go @@ -458,30 +458,6 @@ func (a *App) handleExitCoder(cCtx *Context, err error) { } } -func (a *App) commandNames() []string { - var cmdNames []string - - for _, cmd := range a.Commands { - cmdNames = append(cmdNames, cmd.Names()...) - } - - return cmdNames -} - -func (a *App) validCommandName(checkCmdName string) bool { - valid := false - allCommandNames := a.commandNames() - - for _, cmdName := range allCommandNames { - if checkCmdName == cmdName { - valid = true - break - } - } - - return valid -} - func (a *App) argsWithDefaultCommand(oldArgs Args) Args { if a.DefaultCommand != "" { rawArgs := append([]string{a.DefaultCommand}, oldArgs.Slice()...) diff --git a/app_test.go b/app_test.go index d07d2c0735..f27c3d5451 100644 --- a/app_test.go +++ b/app_test.go @@ -48,7 +48,9 @@ func ExampleApp_Run() { Authors: []*Author{{Name: "Oliver Allen", Email: "oliver@toyshop.example.com"}}, } - app.Run(os.Args) + if err := app.Run(os.Args); err != nil { + return + } // Output: // Hello Jeremy } @@ -2719,8 +2721,8 @@ func TestFlagAction(t *testing.T) { if v == "" { return fmt.Errorf("empty string") } - c.App.Writer.Write([]byte(v + " ")) - return nil + _, err := c.App.Writer.Write([]byte(v + " ")) + return err }, } app := &App{ @@ -2750,8 +2752,8 @@ func TestFlagAction(t *testing.T) { if v[0] == "err" { return fmt.Errorf("error string slice") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &BoolFlag{ @@ -2760,8 +2762,8 @@ func TestFlagAction(t *testing.T) { if !v { return fmt.Errorf("value is false") } - c.App.Writer.Write([]byte(fmt.Sprintf("%t ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%t ", v))) + return err }, }, &DurationFlag{ @@ -2770,8 +2772,8 @@ func TestFlagAction(t *testing.T) { if v == 0 { return fmt.Errorf("empty duration") } - c.App.Writer.Write([]byte(v.String() + " ")) - return nil + _, err := c.App.Writer.Write([]byte(v.String() + " ")) + return err }, }, &Float64Flag{ @@ -2780,8 +2782,8 @@ func TestFlagAction(t *testing.T) { if v < 0 { return fmt.Errorf("negative float64") } - c.App.Writer.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64) + " ")) - return nil + _, err := c.App.Writer.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64) + " ")) + return err }, }, &Float64SliceFlag{ @@ -2790,8 +2792,8 @@ func TestFlagAction(t *testing.T) { if len(v) > 0 && v[0] < 0 { return fmt.Errorf("invalid float64 slice") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &GenericFlag{ @@ -2806,8 +2808,8 @@ func TestFlagAction(t *testing.T) { } } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &IntFlag{ @@ -2816,8 +2818,8 @@ func TestFlagAction(t *testing.T) { if v < 0 { return fmt.Errorf("negative int") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &IntSliceFlag{ @@ -2826,8 +2828,8 @@ func TestFlagAction(t *testing.T) { if len(v) > 0 && v[0] < 0 { return fmt.Errorf("invalid int slice") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &Int64Flag{ @@ -2836,8 +2838,8 @@ func TestFlagAction(t *testing.T) { if v < 0 { return fmt.Errorf("negative int64") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &Int64SliceFlag{ @@ -2846,8 +2848,8 @@ func TestFlagAction(t *testing.T) { if len(v) > 0 && v[0] < 0 { return fmt.Errorf("invalid int64 slice") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &PathFlag{ @@ -2856,8 +2858,8 @@ func TestFlagAction(t *testing.T) { if v == "" { return fmt.Errorf("empty path") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &TimestampFlag{ @@ -2867,8 +2869,8 @@ func TestFlagAction(t *testing.T) { if v.IsZero() { return fmt.Errorf("zero timestamp") } - c.App.Writer.Write([]byte(v.Format(time.RFC3339) + " ")) - return nil + _, err := c.App.Writer.Write([]byte(v.Format(time.RFC3339) + " ")) + return err }, }, &UintFlag{ @@ -2877,8 +2879,8 @@ func TestFlagAction(t *testing.T) { if v == 0 { return fmt.Errorf("zero uint") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, &Uint64Flag{ @@ -2887,8 +2889,8 @@ func TestFlagAction(t *testing.T) { if v == 0 { return fmt.Errorf("zero uint64") } - c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) - return nil + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err }, }, }, diff --git a/category.go b/category.go index ccc043c254..b1847eca18 100644 --- a/category.go +++ b/category.go @@ -111,7 +111,7 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories { } } - if categorized == true { + if categorized { for _, fl := range fs { if cf, ok := fl.(CategorizableFlag); ok { if cf.GetCategory() == "" { diff --git a/context_test.go b/context_test.go index 9faf157386..5dc4fae6d2 100644 --- a/context_test.go +++ b/context_test.go @@ -325,7 +325,7 @@ func TestContext_Set_InvalidFlagAccessHandler(t *testing.T) { }, } c := NewContext(app, set, nil) - c.Set("missing", "") + expect(t, c.Set("missing", "") != nil, true) expect(t, flagName, "missing") } @@ -411,10 +411,12 @@ func TestNonNilContext(t *testing.T) { // *cli.Context always has a valid // context.Context func TestContextPropagation(t *testing.T) { + type testKey struct{} + parent := NewContext(nil, nil, nil) - parent.Context = context.WithValue(context.Background(), "key", "val") + parent.Context = context.WithValue(context.Background(), testKey{}, "val") ctx := NewContext(nil, nil, parent) - val := ctx.Context.Value("key") + val := ctx.Context.Value(testKey{}) if val == nil { t.Fatal("expected a parent context to be inherited but got nil") } diff --git a/flag_test.go b/flag_test.go index 2e01766ecf..6da9e66142 100644 --- a/flag_test.go +++ b/flag_test.go @@ -510,7 +510,7 @@ func TestStringFlagHelpOutput(t *testing.T) { fl := &StringFlag{Name: test.name, Aliases: test.aliases, Usage: test.usage, Value: test.value} // create a tmp flagset tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + _ = fl.Apply(tfs) output := fl.String() if output != test.expected { @@ -609,7 +609,10 @@ func TestPathFlagHelpOutput(t *testing.T) { // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() @@ -638,7 +641,10 @@ func TestPathFlagApply_SetsAllNames(t *testing.T) { v := "mmm" fl := PathFlag{Name: "path", Aliases: []string{"p", "PATH"}, Destination: &v} set := flag.NewFlagSet("test", 0) - _ = fl.Apply(set) + if err := fl.Apply(set); err != nil { + t.Error(err) + return + } err := set.Parse([]string{"--path", "/path/to/file/path", "-p", "/path/to/file/p", "--PATH", "/path/to/file/PATH"}) expect(t, err, nil) @@ -885,7 +891,10 @@ func TestIntFlagHelpOutput(t *testing.T) { // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() @@ -944,7 +953,10 @@ func TestInt64FlagHelpOutput(t *testing.T) { // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() @@ -992,7 +1004,10 @@ func TestUintFlagHelpOutput(t *testing.T) { // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() @@ -1040,7 +1055,10 @@ func TestUint64FlagHelpOutput(t *testing.T) { // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() @@ -1088,7 +1106,10 @@ func TestDurationFlagHelpOutput(t *testing.T) { // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() @@ -1892,7 +1913,10 @@ func TestGenericFlagHelpOutput(t *testing.T) { fl := &GenericFlag{Name: test.name, Value: test.value, Usage: "test flag"} // create a temporary flag set to apply tfs := flag.NewFlagSet("test", 0) - fl.Apply(tfs) + if err := fl.Apply(tfs); err != nil { + t.Error(err) + return + } output := fl.String() if output != test.expected { diff --git a/help.go b/help.go index e855a72fc2..e6adaef7f1 100644 --- a/help.go +++ b/help.go @@ -376,17 +376,17 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0) t := template.Must(template.New("help").Funcs(funcMap).Parse(templ)) - t.New("helpNameTemplate").Parse(helpNameTemplate) - t.New("usageTemplate").Parse(usageTemplate) - t.New("descriptionTemplate").Parse(descriptionTemplate) - t.New("visibleCommandTemplate").Parse(visibleCommandTemplate) - t.New("copyrightTemplate").Parse(copyrightTemplate) - t.New("versionTemplate").Parse(versionTemplate) - t.New("visibleFlagCategoryTemplate").Parse(visibleFlagCategoryTemplate) - t.New("visibleFlagTemplate").Parse(visibleFlagTemplate) - t.New("visibleGlobalFlagCategoryTemplate").Parse(strings.Replace(visibleFlagCategoryTemplate, "OPTIONS", "GLOBAL OPTIONS", -1)) - t.New("authorsTemplate").Parse(authorsTemplate) - t.New("visibleCommandCategoryTemplate").Parse(visibleCommandCategoryTemplate) + _, _ = t.New("helpNameTemplate").Parse(helpNameTemplate) + _, _ = t.New("usageTemplate").Parse(usageTemplate) + _, _ = t.New("descriptionTemplate").Parse(descriptionTemplate) + _, _ = t.New("visibleCommandTemplate").Parse(visibleCommandTemplate) + _, _ = t.New("copyrightTemplate").Parse(copyrightTemplate) + _, _ = t.New("versionTemplate").Parse(versionTemplate) + _, _ = t.New("visibleFlagCategoryTemplate").Parse(visibleFlagCategoryTemplate) + _, _ = t.New("visibleFlagTemplate").Parse(visibleFlagTemplate) + _, _ = t.New("visibleGlobalFlagCategoryTemplate").Parse(strings.Replace(visibleFlagCategoryTemplate, "OPTIONS", "GLOBAL OPTIONS", -1)) + _, _ = t.New("authorsTemplate").Parse(authorsTemplate) + _, _ = t.New("visibleCommandCategoryTemplate").Parse(visibleCommandCategoryTemplate) err := t.Execute(w, data) if err != nil { diff --git a/internal/build/build.go b/internal/build/build.go index 490edb3faa..d42a47dd8b 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -179,7 +179,7 @@ func sh(exe string, args ...string) (string, error) { func topRunAction(arg string, args ...string) cli.ActionFunc { return func(cCtx *cli.Context) error { - os.Chdir(cCtx.Path("top")) + _ = os.Chdir(cCtx.Path("top")) return runCmd(arg, args...) } @@ -283,7 +283,7 @@ func testCleanup(packages []string) error { lines := strings.Split(string(lineBytes), "\n") - fmt.Fprintf(out, strings.Join(lines[1:], "\n")) + fmt.Fprint(out, strings.Join(lines[1:], "\n")) if err := os.Remove(filename); err != nil { return err @@ -456,10 +456,10 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { desiredMaxSizeString := fmt.Sprintf(mbStringFormatter, desiredMaxBinarySize) // show guidance - fmt.Println(fmt.Sprintf("\n%s is the current binary size", roundedFileSizeString)) + fmt.Printf("\n%s is the current binary size\n", roundedFileSizeString) // show guidance for min size if isLessThanDesiredMin { - fmt.Println(fmt.Sprintf(" %s %s is the target min size", goodNewsEmoji, desiredMinSizeString)) + fmt.Printf(" %s %s is the target min size\n", goodNewsEmoji, desiredMinSizeString) fmt.Println("") // visual spacing fmt.Println(" The binary is smaller than the target min size, which is great news!") fmt.Println(" That means that your changes are shrinking the binary size.") @@ -470,11 +470,11 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { fmt.Println("") // visual spacing os.Exit(1) } else { - fmt.Println(fmt.Sprintf(" %s %s is the target min size", checksPassedEmoji, desiredMinSizeString)) + fmt.Printf(" %s %s is the target min size\n", checksPassedEmoji, desiredMinSizeString) } // show guidance for max size if isMoreThanDesiredMax { - fmt.Println(fmt.Sprintf(" %s %s is the target max size", badNewsEmoji, desiredMaxSizeString)) + fmt.Printf(" %s %s is the target max size\n", badNewsEmoji, desiredMaxSizeString) fmt.Println("") // visual spacing fmt.Println(" The binary is larger than the target max size.") fmt.Println(" That means that your changes are increasing the binary size.") @@ -488,7 +488,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) { fmt.Println("") // visual spacing os.Exit(1) } else { - fmt.Println(fmt.Sprintf(" %s %s is the target max size", checksPassedEmoji, desiredMaxSizeString)) + fmt.Printf(" %s %s is the target max size\n", checksPassedEmoji, desiredMaxSizeString) } return nil @@ -531,13 +531,13 @@ func YAMLFmtActionFunc(cCtx *cli.Context) error { return err } - os.Chdir(cCtx.Path("top")) + _ = os.Chdir(cCtx.Path("top")) return runCmd(yqBin, "eval", "--inplace", "flag-spec.yaml") } func DiffCheckActionFunc(cCtx *cli.Context) error { - os.Chdir(cCtx.Path("top")) + _ = os.Chdir(cCtx.Path("top")) if err := runCmd("git", "diff", "--exit-code"); err != nil { return err @@ -548,7 +548,7 @@ func DiffCheckActionFunc(cCtx *cli.Context) error { func EnsureGoimportsActionFunc(cCtx *cli.Context) error { top := cCtx.Path("top") - os.Chdir(top) + _ = os.Chdir(top) if err := runCmd( "goimports", @@ -567,7 +567,7 @@ func EnsureGfmrunActionFunc(cCtx *cli.Context) error { top := cCtx.Path("top") gfmrunExe := filepath.Join(top, ".local/bin/gfmrun") - os.Chdir(top) + _ = os.Chdir(top) if v, err := sh(gfmrunExe, "--version"); err == nil && strings.TrimSpace(v) == gfmrunVersion { return nil @@ -587,7 +587,7 @@ func EnsureGfmrunActionFunc(cCtx *cli.Context) error { } func EnsureMkdocsActionFunc(cCtx *cli.Context) error { - os.Chdir(cCtx.Path("top")) + _ = os.Chdir(cCtx.Path("top")) if err := runCmd("mkdocs", "--version"); err == nil { return nil @@ -608,7 +608,7 @@ func SetMkdocsRemoteActionFunc(cCtx *cli.Context) error { return errors.New("empty github token") } - os.Chdir(cCtx.Path("top")) + _ = os.Chdir(cCtx.Path("top")) if err := runCmd("git", "remote", "rm", "origin"); err != nil { return err @@ -622,7 +622,7 @@ func SetMkdocsRemoteActionFunc(cCtx *cli.Context) error { func LintActionFunc(cCtx *cli.Context) error { top := cCtx.Path("top") - os.Chdir(top) + _ = os.Chdir(top) out, err := sh(filepath.Join(top, ".local/bin/goimports"), "-l", ".") if err != nil { @@ -640,7 +640,7 @@ func LintActionFunc(cCtx *cli.Context) error { } func V2Diff(cCtx *cli.Context) error { - os.Chdir(cCtx.Path("top")) + _ = os.Chdir(cCtx.Path("top")) err := runCmd( "diff", diff --git a/internal/example-cli/example-cli.go b/internal/example-cli/example-cli.go index 06cbbfffcb..9d8c545b9c 100644 --- a/internal/example-cli/example-cli.go +++ b/internal/example-cli/example-cli.go @@ -7,5 +7,5 @@ import ( ) func main() { - (&cli.App{}).Run([]string{""}) + _ = (&cli.App{}).Run([]string{""}) } diff --git a/suggestions_test.go b/suggestions_test.go index 5efbc62695..2136acdc46 100644 --- a/suggestions_test.go +++ b/suggestions_test.go @@ -136,7 +136,9 @@ func ExampleApp_Suggest() { }, } - app.Run([]string{"greet", "--nema", "chipmunk"}) + if err := app.Run([]string{"greet", "--nema", "chipmunk"}); err == nil { + return + } // Output: // Incorrect Usage: flag provided but not defined: -nema // @@ -177,7 +179,9 @@ func ExampleApp_Suggest_command() { }, } - app.Run([]string{"greet", "neighbors", "--sliming"}) + if err := app.Run([]string{"greet", "neighbors", "--sliming"}); err == nil { + return + } // Output: // Incorrect Usage: flag provided but not defined: -sliming //