Skip to content

Commit 914d5ea

Browse files
authored
Merge pull request #13 from tucksaun/ci/golangci-lint
ci: add golangci-lint
2 parents 9f1bdb8 + 3e2f0d6 commit 914d5ea

11 files changed

+121
-46
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

block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func splitsBlockLines(msg string, width int) ([]string, int) {
6060
}
6161

6262
for _, line := range strings.Split(msg, "\n") {
63-
line = strings.Replace(line, "\t", " ", -1)
63+
line = strings.ReplaceAll(line, "\t", " ")
6464
lastLinePos := 0
6565
inAnOpeningTag := false
6666
inAClosingTag := false

formatter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ func (s *styleStack) current() *FormatterStyle {
7272
const tagRegex = "[a-z][^<>]*"
7373

7474
var (
75-
FormattingRegexp = regexp.MustCompile("(?i)<((" + tagRegex + ")?|/(" + tagRegex + ")?)>")
76-
StyleRegexp = regexp.MustCompile("(?i)([^=]+)=([^;]+)(;|$)")
77-
EscapingRegexp = regexp.MustCompile("([^\\\\]?)<")
75+
FormattingRegexp = regexp.MustCompile(`(?i)<((` + tagRegex + ")?|/(" + tagRegex + ")?)>")
76+
StyleRegexp = regexp.MustCompile(`(?i)([^=]+)=([^;]+)(;|$)`)
77+
EscapingRegexp = regexp.MustCompile(`([^\\]?)<`)
7878
)
7979

8080
func Escape(msg []byte) []byte {
@@ -231,11 +231,11 @@ func (formatter *Formatter) Format(msg []byte, w io.Writer) (written int, err er
231231

232232
msg = output.Bytes()
233233
if bytes.Contains(msg, []byte("<<")) {
234-
msg = bytes.Replace(msg, []byte("\\<"), []byte("\\<"), -1)
235-
msg = bytes.Replace(msg, []byte("<<"), []byte("\\"), -1)
234+
msg = bytes.ReplaceAll(msg, []byte("\\<"), []byte("\\<"))
235+
msg = bytes.ReplaceAll(msg, []byte("<<"), []byte("\\"))
236236
}
237237

238-
msg = bytes.Replace(msg, []byte("\\<"), []byte("<"), -1)
238+
msg = bytes.ReplaceAll(msg, []byte("\\<"), []byte("<"))
239239
_, err = w.Write(msg)
240240

241241
return

formatter_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"bytes"
2424
"strings"
2525

26+
"github.com/rs/zerolog/log"
2627
. "gopkg.in/check.v1"
2728
)
2829

@@ -104,7 +105,9 @@ var _ = Suite(&OutputFormatterSuite{})
104105

105106
func (formatter *Formatter) formatString(msg string) string {
106107
buf := bytes.NewBuffer([]byte(``))
107-
formatter.Format([]byte(msg), buf)
108+
if _, err := formatter.Format([]byte(msg), buf); err != nil {
109+
log.Err(err)
110+
}
108111

109112
return buf.String()
110113
}

input.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"fmt"
2424
"io"
2525
"os"
26+
27+
"github.com/pkg/errors"
2628
)
2729

2830
var (
@@ -36,14 +38,16 @@ var (
3638
// space-separated values into successive arguments. Newlines count
3739
// as space. It returns the number of items successfully scanned.
3840
// If that is less than the number of arguments, err will report why.
39-
func Scan(a ...interface{}) (n int, err error) {
40-
return fmt.Fscan(Stdin, a...)
41+
func Scan(a ...interface{}) (int, error) {
42+
n, err := fmt.Fscan(Stdin, a...)
43+
return n, errors.WithStack(err)
4144
}
4245

4346
// Scanln is similar to Scan, but stops scanning at a newline and
4447
// after the final item there must be a newline or EOF.
45-
func Scanln(a ...interface{}) (n int, err error) {
46-
return fmt.Fscanln(Stdin, a...)
48+
func Scanln(a ...interface{}) (int, error) {
49+
n, err := fmt.Fscanln(Stdin, a...)
50+
return n, errors.WithStack(err)
4751
}
4852

4953
// Scanf scans text read from standard input, storing successive
@@ -53,8 +57,9 @@ func Scanln(a ...interface{}) (n int, err error) {
5357
// Newlines in the input must match newlines in the format.
5458
// The one exception: the verb %c always scans the next rune in the
5559
// input, even if it is a space (or tab etc.) or newline.
56-
func Scanf(format string, a ...interface{}) (n int, err error) {
57-
return fmt.Fscanf(Stdin, format, a...)
60+
func Scanf(format string, a ...interface{}) (int, error) {
61+
n, err := fmt.Fscanf(Stdin, format, a...)
62+
return n, errors.WithStack(err)
5863
}
5964

6065
type Input struct {
@@ -78,7 +83,8 @@ func NewInput(reader io.Reader) *Input {
7883
}
7984

8085
func (input *Input) Read(p []byte) (int, error) {
81-
return input.reader.Read(p)
86+
n, err := input.reader.Read(p)
87+
return n, errors.WithStack(err)
8288
}
8389

8490
func (input *Input) IsInteractive() bool {

input_helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func AskStringDefault(message, def string, validator func(string) (string, bool)
3939

4040
reader := bufio.NewReader(Stdin)
4141
for {
42-
Print(message)
42+
if _, err := Print(message); err != nil {
43+
Logger.Err(err).Msg("could not write to terminal")
44+
return def
45+
}
4346
answer, readError := reader.ReadString('\n')
4447
if readError != nil {
4548
continue

logging_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ type LoggingSuite struct{}
2929
var _ = Suite(&LoggingSuite{})
3030

3131
func (ts *LoggingSuite) TestSetLogLevel(c *C) {
32-
defer SetLogLevel(1)
32+
defer func() {
33+
c.Assert(SetLogLevel(1), IsNil)
34+
}()
3335
var err error
3436
c.Assert(Logger.GetLevel(), Equals, zerolog.ErrorLevel)
3537
c.Assert(GetLogLevel(), Equals, 1)
@@ -57,7 +59,9 @@ func (ts *LoggingSuite) TestSetLogLevel(c *C) {
5759
}
5860

5961
func (ts *LoggingSuite) TestIsVerbose(c *C) {
60-
defer SetLogLevel(1)
62+
defer func() {
63+
c.Assert(SetLogLevel(1), IsNil)
64+
}()
6165
var err error
6266

6367
c.Assert(IsVerbose(), Equals, false)
@@ -72,7 +76,9 @@ func (ts *LoggingSuite) TestIsVerbose(c *C) {
7276
}
7377

7478
func (ts *LoggingSuite) TestIsDebug(c *C) {
75-
defer SetLogLevel(1)
79+
defer func() {
80+
c.Assert(SetLogLevel(1), IsNil)
81+
}()
7682
var err error
7783

7884
c.Assert(IsDebug(), Equals, false)

output.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"fmt"
2424
"io"
2525
"os"
26+
27+
"github.com/pkg/errors"
2628
)
2729

2830
func Formatf(msg string, a ...interface{}) string {
@@ -36,55 +38,63 @@ func Format(msg string) string {
3638
// Print formats using the default formats for its operands and writes to standard output.
3739
// Spaces are added between operands when neither is a string.
3840
// It returns the number of bytes written and any write error encountered.
39-
func Print(a ...interface{}) (n int, err error) {
40-
return fmt.Fprint(Stdout, a...)
41+
func Print(a ...interface{}) (int, error) {
42+
n, err := fmt.Fprint(Stdout, a...)
43+
return n, errors.WithStack(err)
4144
}
4245

4346
// Printf formats according to a format specifier and writes to standard output.
4447
// It returns the number of bytes written and any write error encountered.
45-
func Printf(format string, a ...interface{}) (n int, err error) {
46-
return fmt.Fprintf(Stdout, format, a...)
48+
func Printf(format string, a ...interface{}) (int, error) {
49+
n, err := fmt.Fprintf(Stdout, format, a...)
50+
return n, errors.WithStack(err)
4751
}
4852

4953
// Println formats using the default formats for its operands and writes to standard output.
5054
// Spaces are always added between operands and a newline is appended.
5155
// It returns the number of bytes written and any write error encountered.
52-
func Println(a ...interface{}) (n int, err error) {
53-
return fmt.Fprintln(Stdout, a...)
56+
func Println(a ...interface{}) (int, error) {
57+
n, err := fmt.Fprintln(Stdout, a...)
58+
return n, errors.WithStack(err)
5459
}
5560

5661
// Printfln formats according to a format specifier and writes to standard error output.
5762
// A newline is appended.
5863
// It returns the number of bytes written and any write error encountered.
59-
func Printfln(format string, a ...interface{}) (n int, err error) {
60-
return fmt.Fprintf(Stdout, format+"\n", a...)
64+
func Printfln(format string, a ...interface{}) (int, error) {
65+
n, err := fmt.Fprintf(Stdout, format+"\n", a...)
66+
return n, errors.WithStack(err)
6167
}
6268

6369
// Eprint formats using the default formats for its operands and writes to standard error output.
6470
// Spaces are added between operands when neither is a string.
6571
// It returns the number of bytes written and any write error encountered.
66-
func Eprint(a ...interface{}) (n int, err error) {
67-
return fmt.Fprint(Stderr, a...)
72+
func Eprint(a ...interface{}) (int, error) {
73+
n, err := fmt.Fprint(Stderr, a...)
74+
return n, errors.WithStack(err)
6875
}
6976

7077
// Eprintf formats according to a format specifier and writes to standard error output.
7178
// It returns the number of bytes written and any write error encountered.
72-
func Eprintf(format string, a ...interface{}) (n int, err error) {
73-
return fmt.Fprintf(Stderr, format, a...)
79+
func Eprintf(format string, a ...interface{}) (int, error) {
80+
n, err := fmt.Fprintf(Stderr, format, a...)
81+
return n, errors.WithStack(err)
7482
}
7583

7684
// Eprintln formats using the default formats for its operands and writes to standard error output.
7785
// Spaces are always added between operands and a newline is appended.
7886
// It returns the number of bytes written and any write error encountered.
79-
func Eprintln(a ...interface{}) (n int, err error) {
80-
return fmt.Fprintln(Stderr, a...)
87+
func Eprintln(a ...interface{}) (int, error) {
88+
n, err := fmt.Fprintln(Stderr, a...)
89+
return n, errors.WithStack(err)
8190
}
8291

8392
// Eprintfln formats according to a format specifier and writes to standard error output.
8493
// A newline is appended.
8594
// It returns the number of bytes written and any write error encountered.
86-
func Eprintfln(format string, a ...interface{}) (n int, err error) {
87-
return fmt.Fprintf(Stderr, format+"\n", a...)
95+
func Eprintfln(format string, a ...interface{}) (int, error) {
96+
n, err := fmt.Fprintf(Stderr, format+"\n", a...)
97+
return n, errors.WithStack(err)
8898
}
8999

90100
var (
@@ -144,7 +154,7 @@ func (o Output) Write(message []byte) (int, error) {
144154

145155
func (o Output) Close() error {
146156
if wc, ok := o.writer.(io.WriteCloser); ok {
147-
return wc.Close()
157+
return errors.WithStack(wc.Close())
148158
}
149159

150160
return nil

output_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func (ts *ConsoleOutputSuite) TestConsoleOutput(c *C) {
4646
buffer := new(bytes.Buffer)
4747
output := NewBufferedConsoleOutput(buffer, buffer)
4848

49-
output.Write([]byte("test"))
49+
_, err := output.Write([]byte("test"))
50+
c.Assert(err, IsNil)
5051
c.Assert(buffer.String(), Equals, "test")
5152

5253
formatter := NewFormatter()
@@ -82,34 +83,42 @@ func (ts *ConsoleOutputSuite) TestWrappers(c *C) {
8283
Stderr = Stdout.Stderr
8384

8485
bufferStdout.Reset()
85-
Print("test")
86+
_, err := Print("test")
87+
c.Assert(err, IsNil)
8688
c.Check(bufferStdout.String(), Equals, "test")
8789

8890
bufferStdout.Reset()
89-
Println("test")
91+
_, err = Println("test")
92+
c.Assert(err, IsNil)
9093
c.Check(bufferStdout.String(), Equals, "test\n")
9194

9295
bufferStdout.Reset()
93-
Printf("test %d", 2)
96+
_, err = Printf("test %d", 2)
97+
c.Assert(err, IsNil)
9498
c.Check(bufferStdout.String(), Equals, "test 2")
9599

96100
bufferStdout.Reset()
97-
Printfln("test %d", 3)
101+
_, err = Printfln("test %d", 3)
102+
c.Assert(err, IsNil)
98103
c.Check(bufferStdout.String(), Equals, "test 3\n")
99104

100105
bufferStderr.Reset()
101-
Eprint("test")
106+
_, err = Eprint("test")
107+
c.Assert(err, IsNil)
102108
c.Check(bufferStderr.String(), Equals, "test")
103109

104110
bufferStderr.Reset()
105-
Eprintln("test")
111+
_, err = Eprintln("test")
112+
c.Assert(err, IsNil)
106113
c.Check(bufferStderr.String(), Equals, "test\n")
107114

108115
bufferStderr.Reset()
109-
Eprintf("test %d", 2)
116+
_, err = Eprintf("test %d", 2)
117+
c.Assert(err, IsNil)
110118
c.Check(bufferStderr.String(), Equals, "test 2")
111119

112120
bufferStderr.Reset()
113-
Eprintfln("test %d", 3)
121+
_, err = Eprintfln("test %d", 3)
122+
c.Assert(err, IsNil)
114123
c.Check(bufferStderr.String(), Equals, "test 3\n")
115124
}

spinner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (s *Spinner) Start() {
101101
fmt.Fprintf(&b, "%s%s%s %s%s", s.PrefixText, s.PrefixIndicator, s.chars[i], s.SuffixIndicator, s.SuffixText)
102102
cursor.ClearLineAfter()
103103
cursor.RestorePosition()
104-
b.WriteTo(s.Writer)
104+
_, _ = b.WriteTo(s.Writer)
105105
s.lock.Unlock()
106106
time.Sleep(s.delay)
107107
}

0 commit comments

Comments
 (0)