Skip to content

Commit 21d2752

Browse files
authored
Merge pull request #26 from tucksaun/ci/golangci-lint
ci: add golangci-lint
2 parents c05f6ef + 8acad55 commit 21d2752

17 files changed

+238
-141
lines changed

.github/workflows/test.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ on:
55
push:
66

77
jobs:
8+
lint:
9+
name: Lint
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: golangci-lint
14+
uses: golangci/golangci-lint-action@v8
15+
816
test:
917
runs-on: ubuntu-latest
1018
strategy:

.golangci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: "2"
2+
3+
run:
4+
issues-exit-code: 1
5+
6+
formatters:
7+
enable:
8+
- gofmt
9+
- gci
10+
11+
linters:
12+
enable:
13+
- wrapcheck
14+
settings:
15+
wrapcheck:
16+
ignore-package-globs:
17+
# We already make sure your own packages wrap errors properly
18+
- github.com/symfony-cli/*
19+
errcheck:
20+
exclude-functions:
21+
- github.com/symfony-cli/terminal.Printf
22+
- github.com/symfony-cli/terminal.Println
23+
- github.com/symfony-cli/terminal.Printfln
24+
- github.com/symfony-cli/terminal.Eprintf
25+
- github.com/symfony-cli/terminal.Eprintln
26+
- github.com/symfony-cli/terminal.Eprintfln
27+
- github.com/symfony-cli/terminal.Eprint
28+
- fmt.Fprintln
29+
- fmt.Fprintf
30+
- fmt.Fprint

application.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (a *Application) Run(arguments []string) (err error) {
9696

9797
if err != nil {
9898
err = IncorrectUsageError{err}
99-
ShowAppHelp(context)
99+
_ = ShowAppHelp(context)
100100
fmt.Fprintln(a.Writer)
101101
HandleExitCoder(err)
102102
return err
@@ -124,7 +124,7 @@ func (a *Application) Run(arguments []string) (err error) {
124124
beforeErr := a.Before(context)
125125
if beforeErr != nil {
126126
fmt.Fprintf(a.Writer, "%v\n\n", beforeErr)
127-
ShowAppHelp(context)
127+
_ = ShowAppHelp(context)
128128
HandleExitCoder(beforeErr)
129129
err = beforeErr
130130
return err
@@ -151,6 +151,15 @@ func (a *Application) Run(arguments []string) (err error) {
151151
return err
152152
}
153153

154+
// MustRun is the entry point to the CLI app. Parses the arguments slice and routes
155+
// to the proper flag/args combination. Under the hood it calls `Run` but will panic
156+
// if any error happen
157+
func (a *Application) MustRun(arguments []string) {
158+
if err := a.Run(arguments); err != nil {
159+
panic(err)
160+
}
161+
}
162+
154163
// Command returns the named command on App. Returns nil if the command does not
155164
// exist
156165
func (a *Application) Command(name string) *Command {

application_test.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"flag"
2626
"fmt"
2727
"io"
28+
"log"
2829
"os"
2930
"reflect"
3031
"strings"
@@ -92,7 +93,9 @@ func ExampleApplication_Run() {
9293
},
9394
}
9495

95-
app.Run(os.Args)
96+
if err := app.Run(os.Args); err != nil {
97+
log.Fatal(err)
98+
}
9699
// Output:
97100
// Hello Jeremy
98101
}
@@ -115,7 +118,9 @@ func ExampleApplication_Run_quiet() {
115118
},
116119
}
117120

118-
app.Run(os.Args)
121+
if err := app.Run(os.Args); err != nil {
122+
log.Fatal(err)
123+
}
119124
// Output:
120125
}
121126

@@ -138,7 +143,9 @@ func ExampleApplication_Run_quietDisabled() {
138143
},
139144
}
140145

141-
app.Run(os.Args)
146+
if err := app.Run(os.Args); err != nil {
147+
log.Fatal(err)
148+
}
142149
// Output:
143150
// Hello Jeremy
144151
// Byebye Jeremy
@@ -164,7 +171,9 @@ func (ts *ApplicationSuite) ExampleApplication_Run_quietInvalid(c *C) {
164171
},
165172
}
166173

167-
app.Run(os.Args)
174+
if err := app.Run(os.Args); err != nil {
175+
log.Fatal(err)
176+
}
168177
c.Assert(stdout.String(), Equals, `Output:
169178
<info>greet</> version <comment>0.0.0</>
170179
A new cli application
@@ -217,7 +226,9 @@ func (ts *ApplicationSuite) ExampleApplication_Run_appHelp(c *C) {
217226
},
218227
},
219228
}
220-
app.Run(os.Args)
229+
if err := app.Run(os.Args); err != nil {
230+
log.Fatal(err)
231+
}
221232
c.Assert(stdout.String(), Equals, `Output:
222233
<info>greet</> version <comment>0.1.0</>
223234
A new cli application
@@ -262,7 +273,9 @@ func ExampleApplication_Run_commandHelp() {
262273
},
263274
},
264275
}
265-
app.Run(os.Args)
276+
if err := app.Run(os.Args); err != nil {
277+
log.Fatal(err)
278+
}
266279
// Output:
267280
// <comment>Description:</>
268281
// use it to see a description
@@ -341,7 +354,7 @@ func (ts *ApplicationSuite) TestApp_CommandWithFlagBeforeTerminator(c *C) {
341354
}
342355
app.Commands = []*Command{command}
343356

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

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

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

393-
app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"})
394-
406+
c.Assert(app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"}), IsNil)
395407
c.Assert(args.Get("first"), Equals, "my-arg")
396408
c.Assert(args.Get("second"), Equals, "notAFlagAtAll")
397409
}
@@ -465,7 +477,7 @@ func (ts *ApplicationSuite) TestApp_Float64Flag(c *C) {
465477
},
466478
}
467479

468-
app.Run([]string{"", "--height", "1.93"})
480+
c.Assert(app.Run([]string{"", "--height", "1.93"}), IsNil)
469481
c.Assert(meters, Equals, 1.93)
470482
}
471483

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

496-
app.Run([]string{"", "cmd", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4", "my-first-arg"})
508+
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 {
509+
t.Error(err)
510+
}
497511

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

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

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

786802
app := &Application{}
787-
app.Run([]string{"-h"})
803+
if err := app.Run([]string{"-h"}); err != nil {
804+
t.Error(err)
805+
}
788806

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

1039-
app.Run([]string{"categories"})
1057+
if err := app.Run([]string{"categories"}); err != nil {
1058+
t.Error(err)
1059+
}
10401060

10411061
expect := commandCategories([]*commandCategory{
10421062
{

command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (c *Command) Run(ctx *Context) (err error) {
127127
err = checkRequiredArgs(c, context)
128128
}
129129
if err != nil {
130-
ShowCommandHelp(ctx, c.FullName())
130+
_ = ShowCommandHelp(ctx, c.FullName())
131131
fmt.Fprintln(ctx.App.Writer)
132132
return IncorrectUsageError{err}
133133
}
@@ -153,7 +153,7 @@ func (c *Command) Run(ctx *Context) (err error) {
153153
if c.Before != nil {
154154
err = c.Before(context)
155155
if err != nil {
156-
ShowCommandHelp(ctx, c.FullName())
156+
_ = ShowCommandHelp(ctx, c.FullName())
157157
HandleExitCoder(err)
158158
return err
159159
}

command_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (cs *CommandSuite) TestCommandFlagParsing(c *C) {
5252
app := &Application{}
5353
app.setup()
5454
set := flag.NewFlagSet("test", 0)
55-
set.Parse(ca.testArgs)
55+
c.Assert(set.Parse(ca.testArgs), IsNil)
5656

5757
context := NewContext(app, set, nil)
5858

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

7878
if ca.expectedErr == "" {
79-
c.Assert(err, Equals, nil)
79+
c.Assert(err, IsNil)
8080
} else {
8181
c.Assert(err, ErrorMatches, ca.expectedErr)
8282
}

context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ package console
2222
import (
2323
"flag"
2424
"fmt"
25+
26+
"github.com/pkg/errors"
2527
)
2628

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

5153
return fmt.Errorf("no such flag -%v", name)

context_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (cs *ContextSuite) TestContext_Args(c *C) {
148148
set := flag.NewFlagSet("test", 0)
149149
set.Bool("myflag", false, "doc")
150150
ctx := NewContext(nil, set, nil)
151-
set.Parse([]string{"--myflag", "bat", "baz"})
151+
c.Assert(set.Parse([]string{"--myflag", "bat", "baz"}), IsNil)
152152
c.Assert(ctx.Args().Len(), Equals, 2)
153153
c.Assert(ctx.Bool("myflag"), Equals, true)
154154
}
@@ -157,7 +157,7 @@ func (cs *ContextSuite) TestContext_NArg(c *C) {
157157
set := flag.NewFlagSet("test", 0)
158158
set.Bool("myflag", false, "doc")
159159
ctx := NewContext(nil, set, nil)
160-
set.Parse([]string{"--myflag", "bat", "baz"})
160+
c.Assert(set.Parse([]string{"--myflag", "bat", "baz"}), IsNil)
161161
c.Assert(ctx.NArg(), Equals, 2)
162162
}
163163

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

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

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

222-
ctx.Set("int", "1")
222+
c.Assert(ctx.Set("int", "1"), IsNil)
223223
c.Assert(ctx.Int("int"), Equals, 1)
224224
}
225225

226226
func (cs *ContextSuite) TestContext_Set_AppFlags(c *C) {
227-
defer terminal.SetLogLevel(1)
227+
defer func() {
228+
c.Assert(terminal.SetLogLevel(1), IsNil)
229+
}()
228230

229231
app := &Application{
230232
Commands: []*Command{
@@ -239,7 +241,7 @@ func (cs *ContextSuite) TestContext_Set_AppFlags(c *C) {
239241
},
240242
},
241243
}
242-
app.Run([]string{"cmd", "foo"})
244+
app.MustRun([]string{"cmd", "foo"})
243245
}
244246

245247
func (cs *ContextSuite) TestContext_Lineage(c *C) {
@@ -249,8 +251,8 @@ func (cs *ContextSuite) TestContext_Lineage(c *C) {
249251
parentSet.Bool("top-flag", true, "doc")
250252
parentCtx := NewContext(nil, parentSet, nil)
251253
ctx := NewContext(nil, set, parentCtx)
252-
set.Parse([]string{"--local-flag"})
253-
parentSet.Parse([]string{"--top-flag"})
254+
c.Assert(set.Parse([]string{"--local-flag"}), IsNil)
255+
c.Assert(parentSet.Parse([]string{"--top-flag"}), IsNil)
254256

255257
lineage := ctx.Lineage()
256258
c.Assert(len(lineage), Equals, 2)
@@ -265,8 +267,8 @@ func (cs *ContextSuite) TestContext_lookupFlagSet(c *C) {
265267
parentSet.Bool("top-flag", true, "doc")
266268
parentCtx := NewContext(nil, parentSet, nil)
267269
ctx := NewContext(nil, set, parentCtx)
268-
set.Parse([]string{"--local-flag"})
269-
parentSet.Parse([]string{"--top-flag"})
270+
c.Assert(set.Parse([]string{"--local-flag"}), IsNil)
271+
c.Assert(parentSet.Parse([]string{"--top-flag"}), IsNil)
270272

271273
fs := lookupFlagSet("top-flag", ctx)
272274
c.Assert(fs, Equals, parentCtx.flagSet)

errors_stacktrace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func FormatErrorChain(buf *bytes.Buffer, err error, trimPaths bool) bool {
157157
if trimPaths {
158158
file = trimGOPATH(fn.Name(), file)
159159
}
160-
buf.WriteString(fmt.Sprintf("%s\n\t<info>%s:%d</>", fn.Name(), file, line))
160+
fmt.Fprintf(buf, "%s\n\t<info>%s:%d</>", fn.Name(), file, line)
161161
}
162162
}
163163

flag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (m *StringMap) String() string {
192192

193193
// Serialized allows StringSlice to fulfill Serializeder
194194
func (m *StringMap) Serialized() string {
195-
jsonBytes, _ := json.Marshal(m)
195+
jsonBytes, _ := json.Marshal(m.m)
196196
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
197197
}
198198

0 commit comments

Comments
 (0)