Skip to content

Commit a35b429

Browse files
committed
Cosmetic cleanup
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) - some tests enhanced with added info And: - adding a WhiteList test for internal/com Signed-off-by: apostasie <[email protected]>
1 parent dd610a5 commit a35b429

21 files changed

+330
-232
lines changed

mod/tigron/.golangci.yml

+47
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ issues:
1212

1313
linters:
1414
default: all
15+
enable:
16+
# These are the default set of golangci
17+
- errcheck # Errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases.
18+
- govet # Vet examines Go source code and reports suspicious constructs. It is roughly the same as 'go vet' and uses its passes.
19+
- ineffassign # Detects when assignments to existing variables are not used.
20+
- staticcheck # It's the set of rules from staticcheck.
21+
- unused # Checks Go code for unused constants, variables, functions and types.
22+
# These are the linters we knowingly want enabled in addition to the default set
23+
- containedctx # avoid embedding context into structs
24+
- depguard # Allows to explicitly allow or disallow third party modules
25+
- err113 # encourage static errors
26+
- forcetypeassert # not asserting is risky and bad influence for newcomers
27+
- gochecknoglobals # globals should be avoided as much as possible
28+
- godot # forces dot at the end of comments
29+
- gosec # various security checks
30+
- interfacebloat # limit complexity in public APIs
31+
- paralleltest # enforces tests using parallel
32+
- revive # meta linter (see settings below)
33+
- testpackage # test packages should be separate from the package they test (eg: name them package_test)
34+
- testableexamples # makes sure that examples are testable (have an expected output)
35+
- thelper # enforces use of t.Helper()
36+
- varnamelen # encourage readable descriptive names for variables instead of x, y, z
1537
disable:
1638
# These are the linters that we know we do not want
1739
- cyclop # provided by revive
@@ -31,6 +53,31 @@ linters:
3153
- testifylint # no testify
3254
- 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
3481
depguard:
3582
rules:
3683
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

0 commit comments

Comments
 (0)