Skip to content

Commit 1e0b404

Browse files
committed
Cosmetic cleanup and linting
This is mostly a cosmetic changeset: - refined golangcilint, explicitly calling which linters we really care about, along with settings for revive - comments on reasons for some of //nolint - comments line breaks - additional FIXME information - assert type check fixes - const-ification - overall making linter happy(-ier) Signed-off-by: apostasie <[email protected]>
1 parent cece0a9 commit 1e0b404

22 files changed

+164
-92
lines changed

mod/tigron/.golangci.yml

+71-16
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,80 @@ issues:
1212

1313
linters:
1414
default: all
15+
enable:
16+
# These are the default set of golangci (errcheck is disabled, see below)
17+
- govet # Vet examines Go source code and reports suspicious constructs. It is roughly the same as 'go vet' and uses its passes.
18+
- ineffassign # Detects when assignments to existing variables are not used.
19+
- staticcheck # It's the set of rules from staticcheck.
20+
- unused # Checks Go code for unused constants, variables, functions and types.
21+
# These are the linters we knowingly want enabled in addition to the default set
22+
- containedctx # avoid embedding context into structs
23+
- depguard # Allows to explicitly allow or disallow third party modules
24+
- err113 # encourage static errors
25+
- gochecknoglobals # globals should be avoided as much as possible
26+
- godot # forces dot at the end of comments
27+
- gosec # various security checks
28+
- interfacebloat # limit complexity in public APIs
29+
- paralleltest # enforces tests using parallel
30+
- revive # meta linter (see settings below)
31+
- testpackage # test packages should be separate from the package they test (eg: name them package_test)
32+
- testableexamples # makes sure that examples are testable (have an expected output)
33+
- thelper # enforces use of t.Helper()
34+
- varnamelen # encourage readable descriptive names for variables instead of x, y, z
1535
disable:
1636
# These are the linters that we know we do not want
17-
- cyclop # provided by revive
18-
- exhaustruct # does not serve much of a purpose
19-
- funlen # provided by revive
20-
- gocognit # provided by revive
21-
- goconst # provided by revive
22-
- godox # not helpful unless we could downgrade it to warning / info
23-
- ginkgolinter # no ginkgo
24-
- gomodguard # we use depguard instead
25-
- ireturn # too annoying with not enough value
26-
- lll # provided by golines
27-
- nonamedreturns # named returns are occasionally useful
28-
- prealloc # premature optimization
29-
- promlinter # no prometheus
30-
- sloglint # no slog
31-
- testifylint # no testify
32-
- zerologlint # no zerolog
37+
- cyclop # provided by revive
38+
- exhaustruct # does not serve much of a purpose
39+
- errcheck # provided by revive
40+
- forcetypeassert # provided by revive
41+
- funlen # provided by revive
42+
- gocognit # provided by revive
43+
- goconst # provided by revive
44+
- godox # not helpful unless we could downgrade it to warning / info
45+
- ginkgolinter # no ginkgo
46+
- gomodguard # we use depguard instead
47+
- ireturn # too annoying with not enough value
48+
- lll # provided by golines
49+
- nonamedreturns # named returns are occasionally useful
50+
- prealloc # premature optimization
51+
- promlinter # no prometheus
52+
- sloglint # no slog
53+
- testifylint # no testify
54+
- zerologlint # no zerolog
3355
settings:
56+
interfacebloat:
57+
# Default is 10
58+
max: 13
59+
revive:
60+
enable-all-rules: true
61+
rules:
62+
- name: cognitive-complexity
63+
# Default is 7
64+
arguments: [60]
65+
- name: function-length
66+
# Default is 50, 75
67+
arguments: [80, 160]
68+
- name: cyclomatic
69+
# Default is 10
70+
arguments: [30]
71+
- name: add-constant
72+
arguments:
73+
- allowInts: "0,1,2"
74+
allowStrs: '""'
75+
- name: flag-parameter
76+
# Not sure why this is valuable.
77+
disabled: true
78+
- name: line-length-limit
79+
# Formatter `golines` takes care of this.
80+
disabled: true
81+
- name: unhandled-error
82+
arguments:
83+
- "fmt.Print"
84+
- "fmt.Println"
85+
- "fmt.Printf"
86+
- "fmt.Fprint"
87+
- "fmt.Fprintln"
88+
- "fmt.Fprintf"
3489
depguard:
3590
rules:
3691
main:

mod/tigron/expect/comparators_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17+
//revive:disable:add-constant
1718
package expect_test
1819

1920
import (

mod/tigron/internal/com/command.go

+14-21
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ var (
4343
ErrFailedStarting = errors.New("command failed starting")
4444
// ErrSignaled is returned by Wait() if a signal was sent to the command while running.
4545
ErrSignaled = errors.New("command execution signaled")
46-
// ErrExecutionFailed is returned by Wait() when a command executes but returns a non-zero error
47-
// code.
46+
// ErrExecutionFailed is returned by Wait() when a command executes but returns a non-zero error code.
4847
ErrExecutionFailed = errors.New("command returned a non-zero exit code")
4948
// ErrFailedSendingSignal may happen if sending a signal to an already terminated process.
5049
ErrFailedSendingSignal = errors.New("failed sending signal")
5150

5251
// ErrExecAlreadyStarted is a system error normally indicating a bogus double call to Run().
5352
ErrExecAlreadyStarted = errors.New("command has already been started (double `Run`)")
54-
// ErrExecNotStarted is a system error normally indicating that Wait() has been called without
55-
// first calling Run().
53+
// ErrExecNotStarted is a system error normally indicating that Wait() has been called without first calling Run().
5654
ErrExecNotStarted = errors.New("command has not been started (call `Run` first)")
5755
// ErrExecAlreadyFinished is a system error indicating a double call to Wait().
5856
ErrExecAlreadyFinished = errors.New("command is already finished")
@@ -75,7 +73,7 @@ type Result struct {
7573
}
7674

7775
type execution struct {
78-
//nolint:containedctx
76+
//nolint:containedctx // Is there a way around this?
7977
context context.Context
8078
cancel context.CancelFunc
8179
command *exec.Cmd
@@ -138,27 +136,24 @@ func (gc *Command) Clone() *Command {
138136
return com
139137
}
140138

141-
// WithPTY requests that the command be executed with a pty for std streams. Parameters allow
142-
// showing which streams
143-
// are to be tied to the pty.
139+
// WithPTY requests that the command be executed with a pty for std streams.
140+
// Parameters allow showing which streams are to be tied to the pty.
144141
// This command has no effect if Run has already been called.
145142
func (gc *Command) WithPTY(stdin, stdout, stderr bool) {
146143
gc.ptyStdout = stdout
147144
gc.ptyStderr = stderr
148145
gc.ptyStdin = stdin
149146
}
150147

151-
// WithFeeder ensures that the provider function will be executed and its output fed to the command
152-
// stdin. WithFeeder, like Feed, can be used multiple times, and writes will be performed
153-
// sequentially, in order.
148+
// WithFeeder ensures that the provider function will be executed and its output fed to the command stdin.
149+
// WithFeeder, like Feed, can be used multiple times, and writes will be performed sequentially, in order.
154150
// This command has no effect if Run has already been called.
155151
func (gc *Command) WithFeeder(writers ...func() io.Reader) {
156152
gc.writers = append(gc.writers, writers...)
157153
}
158154

159155
// Feed ensures that the provider reader will be copied on the command stdin.
160-
// Feed, like WithFeeder, can be used multiple times, and writes will be performed in sequentially,
161-
// in order.
156+
// Feed, like WithFeeder, can be used multiple times, and writes will be performed in sequentially, in order.
162157
// This command has no effect if Run has already been called.
163158
func (gc *Command) Feed(reader io.Reader) {
164159
gc.writers = append(gc.writers, func() io.Reader {
@@ -198,7 +193,6 @@ func (gc *Command) Run(parentCtx context.Context) error {
198193

199194
// Create a contextual command, set the logger
200195
cmd = gc.buildCommand(ctx)
201-
202196
// Get a debug-logger from the context
203197
var (
204198
log logger.Logger
@@ -339,8 +333,7 @@ func (gc *Command) wrap() error {
339333
err error
340334
)
341335

342-
// XXXgolang: this is troubling. cmd.ProcessState.ExitCode() is always fine, even if
343-
// cmd.ProcessState is nil.
336+
// XXXgolang: this is troubling. cmd.ProcessState.ExitCode() is always fine, even if cmd.ProcessState is nil.
344337
exitCode = cmd.ProcessState.ExitCode()
345338

346339
if cmd.ProcessState != nil {
@@ -357,7 +350,7 @@ func (gc *Command) wrap() error {
357350
}
358351
}
359352

360-
// Catch-up on the context
353+
// Catch-up on the context.
361354
switch ctx.Err() {
362355
case context.DeadlineExceeded:
363356
err = ErrTimeout
@@ -366,7 +359,7 @@ func (gc *Command) wrap() error {
366359
default:
367360
}
368361

369-
// Stuff everything in Result and return err
362+
// Stuff everything in Result and return err.
370363
gc.result = &Result{
371364
ExitCode: exitCode,
372365
Stdout: pipes.fromStdout,
@@ -383,7 +376,7 @@ func (gc *Command) wrap() error {
383376
}
384377

385378
func (gc *Command) buildCommand(ctx context.Context) *exec.Cmd {
386-
// Build arguments and binary
379+
// Build arguments and binary.
387380
args := gc.Args
388381
if gc.PrependArgs != nil {
389382
args = append(gc.PrependArgs, args...)
@@ -459,12 +452,12 @@ func (gc *Command) buildCommand(ctx context.Context) *exec.Cmd {
459452
cmd.Env = append(cmd.Env, k+"="+v)
460453
}
461454

462-
// Attach platform ProcAttr and get optional custom cancellation routine
455+
// Attach platform ProcAttr and get optional custom cancellation routine.
463456
if cancellation := addAttr(cmd); cancellation != nil {
464457
cmd.Cancel = func() error {
465458
gc.exec.log.Log("command cancelled")
466459

467-
// Call the platform dependent cancellation routine
460+
// Call the platform dependent cancellation routine.
468461
return cancellation()
469462
}
470463
}

mod/tigron/internal/com/command_other.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
)
2525

2626
func addAttr(cmd *exec.Cmd) func() error {
27-
// Default shutdown will leave child processes behind in certain circumstances
27+
// Default shutdown will leave child processes behind in certain circumstances.
2828
cmd.SysProcAttr = &syscall.SysProcAttr{
2929
Setsid: true,
30-
// FIXME: understand why we would want that
30+
// FIXME: understand why we would want that.
3131
// Setctty: true,
3232
}
3333

mod/tigron/internal/com/package_example_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17+
//revive:disable:add-constant
1718
package com_test
1819

1920
import (

mod/tigron/internal/com/package_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func TestMain(m *testing.M) {
5050
diff := highk.Diff(string(before), string(after))
5151

5252
if len(diff) != 0 {
53-
_, _ = fmt.Fprintln(os.Stderr, "Leaking file descriptors")
53+
fmt.Fprintln(os.Stderr, "Leaking file descriptors")
5454

5555
for _, file := range diff {
56-
_, _ = fmt.Fprintln(os.Stderr, file)
56+
fmt.Fprintln(os.Stderr, file)
5757
}
5858

5959
exitCode = 1
6060
}
6161
}
6262

6363
if err := highk.FindGoRoutines(); err != nil {
64-
_, _ = fmt.Fprintln(os.Stderr, "Leaking go routines")
65-
_, _ = fmt.Fprintln(os.Stderr, err.Error())
64+
fmt.Fprintln(os.Stderr, "Leaking go routines")
65+
fmt.Fprintln(os.Stderr, err.Error())
6666

6767
exitCode = 1
6868
}

mod/tigron/internal/com/pipes.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ import (
3333
var (
3434
// ErrFailedCreating could be returned by newStdPipes() on pty creation failure.
3535
ErrFailedCreating = errors.New("failed acquiring pipe")
36-
// ErrFailedReading could be returned by the ioGroup in case the go routines fails to read out
37-
// of a pipe.
36+
// ErrFailedReading could be returned by the ioGroup in case the go routines fails to read out of a pipe.
3837
ErrFailedReading = errors.New("failed reading")
39-
// ErrFailedWriting could be returned by the ioGroup in case the go routines fails to write on a
40-
// pipe.
38+
// ErrFailedWriting could be returned by the ioGroup in case the go routines fails to write on a pipe.
4139
ErrFailedWriting = errors.New("failed writing")
4240
)
4341

@@ -108,7 +106,6 @@ func (pipes *stdPipes) closeCaller() {
108106
}
109107
}
110108

111-
//nolint:gocognit
112109
func newStdPipes(
113110
ctx context.Context,
114111
log *logger.ConcreteLogger,

mod/tigron/internal/doc.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package internal provides an assert library, pty, a command wrapper, and a leak detection library
18+
// for internal use in Tigron. The objective for these is not to become generic use-cases libraries,
19+
// but instead to deliver what Tigron
20+
// needs in the simplest possible form.
21+
package internal

mod/tigron/internal/exit.go

-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
limitations under the License.
1515
*/
1616

17-
// Package internal provides an assert library, pty, a command wrapper, and a leak detection library
18-
// for internal use in Tigron.
19-
// The objective for these is not to become generic use-cases libraries, but instead to deliver what
20-
// Tigron needs
21-
// in the simplest possible form.
2217
package internal
2318

2419
// This is duplicated from `expect` to avoid circular imports.

mod/tigron/internal/formatter/osc8.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ type OSC8 struct {
2727

2828
func (o *OSC8) String() string {
2929
// FIXME: not sure if any desktop software does support line numbers anchors?
30+
// FIXME: test that the terminal is able to display these and fallback to printing the information if not.
3031
return fmt.Sprintf("\x1b]8;;%s#%d:1\x07%s\x1b]8;;\x07"+"\u001b[0m", o.Location, o.Line, o.Text)
3132
}

mod/tigron/internal/highk/doc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
limitations under the License.
1515
*/
1616

17-
// Package highk (for "high-κ dielectric") is a highly experimental leak detection library
18-
// (for file descriptors and go routines).
17+
// Package highk (for "high-κ dielectric") is a highly experimental leak detection library (for file descriptors and go
18+
// routines).
1919
// It is purely internal for now and used only as part of the tests for tigron.
2020
// TODO:
2121
// - get rid of lsof and implement in go

mod/tigron/internal/highk/fileleak.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ import (
2626
"syscall"
2727
)
2828

29-
// FIXME: it seems that lsof (or go test) is interefering and showing false positive KQUEUE / inodes
29+
// FIXME: it seems that lsof (or go test) is interfering and showing false positive KQUEUE / inodes
3030
//
31-
//nolint:gochecknoglobals
31+
//nolint:gochecknoglobals // FIXME rewrite all of this anyhow
3232
var whitelist = map[string]bool{
3333
"KQUEUE": true,
3434
"a_inode": true,
3535
}
3636

3737
// SnapshotOpenFiles will capture the list of currently open-files for the process.
3838
//
39-
//nolint:wrapcheck
39+
//nolint:wrapcheck // FIXME: work in progress
4040
func SnapshotOpenFiles(file *os.File) ([]byte, error) {
41-
// Using a buffer would add a pipe to the list of files
42-
// Reimplement this stuff in go ASAP and toss lsof instead of passing around fd
41+
// Using a buffer would add a pipe to the list of files.
42+
// Reimplement this stuff in go ASAP and toss lsof instead of passing around fd.
4343
_, _ = file.Seek(0, 0)
4444
_ = file.Truncate(0)
4545

@@ -48,7 +48,7 @@ func SnapshotOpenFiles(file *os.File) ([]byte, error) {
4848
return nil, err
4949
}
5050

51-
//nolint:gosec
51+
//nolint:gosec // G204 is fine here
5252
cmd := exec.Command(exe, "-nP", "-p", strconv.Itoa(syscall.Getpid()))
5353
cmd.Stdout = file
5454

mod/tigron/internal/highk/goroutines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
// FindGoRoutines retrieves leaked go routines, which are returned as an error.
2424
//
25-
//nolint:wrapcheck
25+
//nolint:wrapcheck // FIXME: work in progress
2626
func FindGoRoutines() error {
2727
return goleak.Find()
2828
}

0 commit comments

Comments
 (0)