Skip to content

ci: add golangci-lint #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ on:
push:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v8

test:
runs-on: ubuntu-latest
strategy:
Expand Down
30 changes: 30 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: "2"

run:
issues-exit-code: 1

formatters:
enable:
- gofmt
- gci

linters:
enable:
- wrapcheck
settings:
wrapcheck:
ignore-package-globs:
# We already make sure your own packages wrap errors properly
- github.com/symfony-cli/*
errcheck:
exclude-functions:
- github.com/symfony-cli/terminal.Printf
- github.com/symfony-cli/terminal.Println
- github.com/symfony-cli/terminal.Printfln
- github.com/symfony-cli/terminal.Eprintf
- github.com/symfony-cli/terminal.Eprintln
- github.com/symfony-cli/terminal.Eprintfln
- github.com/symfony-cli/terminal.Eprint
- fmt.Fprintln
- fmt.Fprintf
- fmt.Fprint
13 changes: 11 additions & 2 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (a *Application) Run(arguments []string) (err error) {

if err != nil {
err = IncorrectUsageError{err}
ShowAppHelp(context)
_ = ShowAppHelp(context)
fmt.Fprintln(a.Writer)
HandleExitCoder(err)
return err
Expand Down Expand Up @@ -124,7 +124,7 @@ func (a *Application) Run(arguments []string) (err error) {
beforeErr := a.Before(context)
if beforeErr != nil {
fmt.Fprintf(a.Writer, "%v\n\n", beforeErr)
ShowAppHelp(context)
_ = ShowAppHelp(context)
HandleExitCoder(beforeErr)
err = beforeErr
return err
Expand All @@ -151,6 +151,15 @@ func (a *Application) Run(arguments []string) (err error) {
return err
}

// MustRun is the entry point to the CLI app. Parses the arguments slice and routes
// to the proper flag/args combination. Under the hood it calls `Run` but will panic
// if any error happen
func (a *Application) MustRun(arguments []string) {
if err := a.Run(arguments); err != nil {
panic(err)
}
}

// Command returns the named command on App. Returns nil if the command does not
// exist
func (a *Application) Command(name string) *Command {
Expand Down
50 changes: 35 additions & 15 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -92,7 +93,9 @@ func ExampleApplication_Run() {
},
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
// Output:
// Hello Jeremy
}
Expand All @@ -115,7 +118,9 @@ func ExampleApplication_Run_quiet() {
},
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
// Output:
}

Expand All @@ -138,7 +143,9 @@ func ExampleApplication_Run_quietDisabled() {
},
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
// Output:
// Hello Jeremy
// Byebye Jeremy
Expand All @@ -164,7 +171,9 @@ func (ts *ApplicationSuite) ExampleApplication_Run_quietInvalid(c *C) {
},
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
c.Assert(stdout.String(), Equals, `Output:
<info>greet</> version <comment>0.0.0</>
A new cli application
Expand Down Expand Up @@ -217,7 +226,9 @@ func (ts *ApplicationSuite) ExampleApplication_Run_appHelp(c *C) {
},
},
}
app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
c.Assert(stdout.String(), Equals, `Output:
<info>greet</> version <comment>0.1.0</>
A new cli application
Expand Down Expand Up @@ -262,7 +273,9 @@ func ExampleApplication_Run_commandHelp() {
},
},
}
app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
// Output:
// <comment>Description:</>
// use it to see a description
Expand Down Expand Up @@ -341,7 +354,7 @@ func (ts *ApplicationSuite) TestApp_CommandWithFlagBeforeTerminator(c *C) {
}
app.Commands = []*Command{command}

app.Run([]string{"", "cmd", "--option", "my-option", "my-arg", "--", "--notARealFlag"})
c.Assert(app.Run([]string{"", "cmd", "--option", "my-option", "my-arg", "--", "--notARealFlag"}), IsNil)

c.Assert(parsedOption, Equals, "my-option")
c.Assert(args, NotNil)
Expand All @@ -366,7 +379,7 @@ func (ts *ApplicationSuite) TestApp_CommandWithDash(c *C) {
}
app.Commands = []*Command{command}

app.Run([]string{"", "cmd", "my-arg", "-"})
c.Assert(app.Run([]string{"", "cmd", "my-arg", "-"}), IsNil)
c.Assert(args, NotNil)
c.Assert(args.Len(), Equals, 2)
c.Assert(args.Get("first"), Equals, "my-arg")
Expand All @@ -390,8 +403,7 @@ func (ts *ApplicationSuite) TestApp_CommandWithNoFlagBeforeTerminator(c *C) {
}
app.Commands = []*Command{command}

app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"})

c.Assert(app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"}), IsNil)
c.Assert(args.Get("first"), Equals, "my-arg")
c.Assert(args.Get("second"), Equals, "notAFlagAtAll")
}
Expand Down Expand Up @@ -465,7 +477,7 @@ func (ts *ApplicationSuite) TestApp_Float64Flag(c *C) {
},
}

app.Run([]string{"", "--height", "1.93"})
c.Assert(app.Run([]string{"", "--height", "1.93"}), IsNil)
c.Assert(meters, Equals, 1.93)
}

Expand Down Expand Up @@ -493,7 +505,9 @@ func TestApp_ParseSliceFlags(t *testing.T) {
}
app.Commands = []*Command{command}

app.Run([]string{"", "cmd", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4", "my-first-arg"})
if err := app.Run([]string{"", "cmd", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4", "my-first-arg"}); err != nil {
t.Error(err)
}

IntsEquals := func(a, b []int) bool {
if len(a) != len(b) {
Expand Down Expand Up @@ -556,7 +570,9 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
}
app.Commands = []*Command{command}

app.Run([]string{"", "cmd", "-a", "2", "-str", "A", "my-arg"})
if err := app.Run([]string{"", "cmd", "-a", "2", "-str", "A", "my-arg"}); err != nil {
t.Error(err)
}

var expectedIntSlice = []int{2}
var expectedStringSlice = []string{"A"}
Expand Down Expand Up @@ -784,7 +800,9 @@ func TestAppHelpPrinter(t *testing.T) {
}

app := &Application{}
app.Run([]string{"-h"})
if err := app.Run([]string{"-h"}); err != nil {
t.Error(err)
}

if wasCalled == false {
t.Errorf("Help printer expected to be called, but was not")
Expand Down Expand Up @@ -1036,7 +1054,9 @@ func TestApp_Run_Categories(t *testing.T) {
versionCommand.Hidden = nil
}()

app.Run([]string{"categories"})
if err := app.Run([]string{"categories"}); err != nil {
t.Error(err)
}

expect := commandCategories([]*commandCategory{
{
Expand Down
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (c *Command) Run(ctx *Context) (err error) {
err = checkRequiredArgs(c, context)
}
if err != nil {
ShowCommandHelp(ctx, c.FullName())
_ = ShowCommandHelp(ctx, c.FullName())
fmt.Fprintln(ctx.App.Writer)
return IncorrectUsageError{err}
}
Expand All @@ -153,7 +153,7 @@ func (c *Command) Run(ctx *Context) (err error) {
if c.Before != nil {
err = c.Before(context)
if err != nil {
ShowCommandHelp(ctx, c.FullName())
_ = ShowCommandHelp(ctx, c.FullName())
HandleExitCoder(err)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (cs *CommandSuite) TestCommandFlagParsing(c *C) {
app := &Application{}
app.setup()
set := flag.NewFlagSet("test", 0)
set.Parse(ca.testArgs)
c.Assert(set.Parse(ca.testArgs), IsNil)

context := NewContext(app, set, nil)

Expand All @@ -76,7 +76,7 @@ func (cs *CommandSuite) TestCommandFlagParsing(c *C) {
err := command.Run(context)

if ca.expectedErr == "" {
c.Assert(err, Equals, nil)
c.Assert(err, IsNil)
} else {
c.Assert(err, ErrorMatches, ca.expectedErr)
}
Expand Down
4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package console
import (
"flag"
"fmt"

"github.com/pkg/errors"
)

// Context is a type that is passed through to
Expand All @@ -45,7 +47,7 @@ func NewContext(app *Application, set *flag.FlagSet, parentCtx *Context) *Contex
// Set assigns a value to a context flag.
func (c *Context) Set(name, value string) error {
if fs := lookupFlagSet(name, c); fs != nil {
return fs.Set(name, value)
return errors.WithStack(fs.Set(name, value))
}

return fmt.Errorf("no such flag -%v", name)
Expand Down
24 changes: 13 additions & 11 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (cs *ContextSuite) TestContext_Args(c *C) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
ctx := NewContext(nil, set, nil)
set.Parse([]string{"--myflag", "bat", "baz"})
c.Assert(set.Parse([]string{"--myflag", "bat", "baz"}), IsNil)
c.Assert(ctx.Args().Len(), Equals, 2)
c.Assert(ctx.Bool("myflag"), Equals, true)
}
Expand All @@ -157,7 +157,7 @@ func (cs *ContextSuite) TestContext_NArg(c *C) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
ctx := NewContext(nil, set, nil)
set.Parse([]string{"--myflag", "bat", "baz"})
c.Assert(set.Parse([]string{"--myflag", "bat", "baz"}), IsNil)
c.Assert(ctx.NArg(), Equals, 2)
}

Expand Down Expand Up @@ -204,8 +204,8 @@ func (cs *ContextSuite) TestContext_IsSet(c *C) {
parentCtx := NewContext(nil, parentSet, nil)
ctx := NewContext(nil, set, parentCtx)

set.Parse([]string{"--one-flag", "--two-flag", "frob"})
parentSet.Parse([]string{"--top-flag"})
c.Assert(set.Parse([]string{"--one-flag", "--two-flag", "frob"}), IsNil)
c.Assert(parentSet.Parse([]string{"--top-flag"}), IsNil)

c.Assert(ctx.IsSet("one-flag"), Equals, true)
c.Assert(ctx.IsSet("two-flag"), Equals, true)
Expand All @@ -219,12 +219,14 @@ func (cs *ContextSuite) TestContext_Set(c *C) {
set.Int("int", 5, "an int")
ctx := NewContext(nil, set, nil)

ctx.Set("int", "1")
c.Assert(ctx.Set("int", "1"), IsNil)
c.Assert(ctx.Int("int"), Equals, 1)
}

func (cs *ContextSuite) TestContext_Set_AppFlags(c *C) {
defer terminal.SetLogLevel(1)
defer func() {
c.Assert(terminal.SetLogLevel(1), IsNil)
}()

app := &Application{
Commands: []*Command{
Expand All @@ -239,7 +241,7 @@ func (cs *ContextSuite) TestContext_Set_AppFlags(c *C) {
},
},
}
app.Run([]string{"cmd", "foo"})
app.MustRun([]string{"cmd", "foo"})
}

func (cs *ContextSuite) TestContext_Lineage(c *C) {
Expand All @@ -249,8 +251,8 @@ func (cs *ContextSuite) TestContext_Lineage(c *C) {
parentSet.Bool("top-flag", true, "doc")
parentCtx := NewContext(nil, parentSet, nil)
ctx := NewContext(nil, set, parentCtx)
set.Parse([]string{"--local-flag"})
parentSet.Parse([]string{"--top-flag"})
c.Assert(set.Parse([]string{"--local-flag"}), IsNil)
c.Assert(parentSet.Parse([]string{"--top-flag"}), IsNil)

lineage := ctx.Lineage()
c.Assert(len(lineage), Equals, 2)
Expand All @@ -265,8 +267,8 @@ func (cs *ContextSuite) TestContext_lookupFlagSet(c *C) {
parentSet.Bool("top-flag", true, "doc")
parentCtx := NewContext(nil, parentSet, nil)
ctx := NewContext(nil, set, parentCtx)
set.Parse([]string{"--local-flag"})
parentSet.Parse([]string{"--top-flag"})
c.Assert(set.Parse([]string{"--local-flag"}), IsNil)
c.Assert(parentSet.Parse([]string{"--top-flag"}), IsNil)

fs := lookupFlagSet("top-flag", ctx)
c.Assert(fs, Equals, parentCtx.flagSet)
Expand Down
2 changes: 1 addition & 1 deletion errors_stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func FormatErrorChain(buf *bytes.Buffer, err error, trimPaths bool) bool {
if trimPaths {
file = trimGOPATH(fn.Name(), file)
}
buf.WriteString(fmt.Sprintf("%s\n\t<info>%s:%d</>", fn.Name(), file, line))
fmt.Fprintf(buf, "%s\n\t<info>%s:%d</>", fn.Name(), file, line)
}
}

Expand Down
2 changes: 1 addition & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (m *StringMap) String() string {

// Serialized allows StringSlice to fulfill Serializeder
func (m *StringMap) Serialized() string {
jsonBytes, _ := json.Marshal(m)
jsonBytes, _ := json.Marshal(m.m)
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
}

Expand Down
Loading