Skip to content

Commit 7673197

Browse files
committed
Add linting
1 parent 47c7f67 commit 7673197

File tree

13 files changed

+139
-80
lines changed

13 files changed

+139
-80
lines changed

.golangci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
issues:
2+
exclude-rules:
3+
- path: cmd/
4+
linters:
5+
- gochecknoglobals
6+
- gochecknoinits
7+
8+
linters:
9+
presets:
10+
- bugs
11+
- comment
12+
# - complexity # limits cyclomatic complexity—typically results in hard-to-follow code
13+
- error
14+
- format
15+
- import
16+
- metalinter
17+
- module
18+
- performance
19+
- sql
20+
- style
21+
- test
22+
- unused
23+
24+
disable:
25+
- depguard # enforces dependency allow list; not using allow list
26+
- paralleltest # not enough tests to matter; not helpful
27+
28+
# annoying/bad
29+
- exhaustruct # enforces exhaustive structs; often incorrect; bad
30+
- goerr113 # disallows returning one-off errors; annoying
31+
- gofumpt # different code formatter; bad
32+
- gomnd # disallows magic numbers; annoying
33+
- ireturn # bans returning interfaces; often incorrect; bad
34+
- lll # limits line length; annoying
35+
- nlreturn # requires blank line before return; annoying
36+
- nonamedreturns # disallows named returns; often required for defer error checking; bad
37+
- tagliatelle # enforces tag name style; often incorrect; bad
38+
- varnamelen # limits variable name length; annoying
39+
- wsl # tedious whitespace rules; annoying

cmd/concurrently.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67

@@ -21,7 +22,7 @@ var cCommand = cobra.Command{
2122

2223
if workingDirectory != "" {
2324
if err := os.Chdir(workingDirectory); err != nil {
24-
return err
25+
return fmt.Errorf("changing working directory: %w", err)
2526
}
2627
}
2728

@@ -31,7 +32,7 @@ var cCommand = cobra.Command{
3132
}
3233

3334
if len(names) > 0 && len(names) != len(cmdParts) {
34-
return fmt.Errorf("number of names must match number of commands")
35+
return errors.New("number of names must match number of commands")
3536
}
3637

3738
labels := collectLabels(cmdStrings)
@@ -49,7 +50,11 @@ var cCommand = cobra.Command{
4950
dbg.Prettyln(commands)
5051
}
5152

52-
return err
53+
if err != nil {
54+
return fmt.Errorf("running commands: %w", err)
55+
}
56+
57+
return nil
5358
},
5459
}
5560

cmd/proc.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ var procCommand = cobra.Command{
2020
Use: "proc",
2121
Aliases: []string{"p"},
2222
Short: "Run commands defined in a Procfile (alias: p)",
23-
RunE: func(cmd *cobra.Command, args []string) error {
23+
RunE: func(cmd *cobra.Command, _ []string) error {
2424
dbg := debugger.Get(cmd.Context())
2525
dbg.Flags(cmd)
2626

2727
if workingDirectory != "" {
2828
if err := os.Chdir(workingDirectory); err != nil {
29-
return err
29+
return fmt.Errorf("changing working directory: %w", err)
3030
}
3131
}
3232

3333
procfile, err := os.Open(procfile)
3434
if err != nil {
35-
return err
35+
return fmt.Errorf("opening procfile: %w", err)
3636
}
3737
defer procfile.Close()
3838

@@ -54,7 +54,7 @@ var procCommand = cobra.Command{
5454
if !noEnvFile {
5555
envFile, err := os.ReadFile(envFile)
5656
if err != nil {
57-
return err
57+
return fmt.Errorf("reading env file: %w", err)
5858
}
5959
envLines = strings.Split(string(envFile), "\n")
6060
}
@@ -93,7 +93,11 @@ var procCommand = cobra.Command{
9393
dbg.Prettyln(commands)
9494
}
9595

96-
return err
96+
if err != nil {
97+
return fmt.Errorf("running commands: %w", err)
98+
}
99+
100+
return nil
97101
},
98102
}
99103

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var rootCmd = cobra.Command{
2020
Use: "konk",
2121
Short: "Konk is a tool for running multiple processes",
2222
Version: Version,
23-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
23+
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
2424
// Ensures that usage isn't printed for errors such as non-zero exits.
2525
// SEE: https://github.com/spf13/cobra/issues/340#issuecomment-378726225
2626
cmd.SilenceUsage = true

cmd/run.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"os"
78
"sort"
9+
"strconv"
810
"strings"
911

1012
"github.com/spf13/cobra"
@@ -18,7 +20,7 @@ var runCommand = cobra.Command{
1820
Use: "run <subcommand>",
1921
Aliases: []string{"r"},
2022
Short: "Run commands serially or concurrently (alias: r)",
21-
RunE: func(cmd *cobra.Command, args []string) error {
23+
RunE: func(cmd *cobra.Command, _ []string) error {
2224
_ = cmd.Help()
2325
os.Exit(1)
2426
return nil
@@ -51,16 +53,22 @@ func collectCommands(args []string) ([]string, []string, error) {
5153
prefix := strings.TrimSuffix(cmd, "*")
5254
pkgFile, err := os.ReadFile("package.json")
5355
if err != nil {
54-
return nil, nil, err
56+
return nil, nil, fmt.Errorf("reading package.json: %w", err)
5557
}
56-
var pkg map[string]interface{}
58+
var pkg map[string]any
5759
if err := json.Unmarshal(pkgFile, &pkg); err != nil {
58-
return nil, nil, err
60+
return nil, nil, fmt.Errorf("unmarshalling package.json: %w", err)
5961
}
6062

6163
// See if any "scripts" match our prefix
6264
matchingScripts := []string{}
63-
for script := range pkg["scripts"].(map[string]interface{}) {
65+
66+
scripts, ok := pkg["scripts"].(map[string]any)
67+
if !ok {
68+
return nil, nil, errors.New("invalid scripts in package.json")
69+
}
70+
71+
for script := range scripts {
6472
if strings.HasPrefix(script, prefix) {
6573
matchingScripts = append(matchingScripts, script)
6674
}
@@ -70,13 +78,13 @@ func collectCommands(args []string) ([]string, []string, error) {
7078

7179
for _, script := range matchingScripts {
7280
commandStrings = append(commandStrings, script)
73-
commands = append(commands, fmt.Sprintf("npm run %s", script))
81+
commands = append(commands, "npm run "+script)
7482
}
7583

7684
continue
7785
}
7886

79-
script := fmt.Sprintf("npm run %s", cmd)
87+
script := "npm run " + cmd
8088
commandStrings = append(commandStrings, cmd)
8189
commands = append(commands, script)
8290
}
@@ -88,12 +96,13 @@ func collectLabels(commandStrings []string) []string {
8896
labels := make([]string, len(commandStrings))
8997

9098
for i, cmdStr := range commandStrings {
91-
if cmdAsLabel {
99+
switch {
100+
case cmdAsLabel:
92101
labels[i] = cmdStr
93-
} else if len(names) > 0 {
102+
case len(names) > 0:
94103
labels[i] = names[i]
95-
} else {
96-
labels[i] = fmt.Sprintf("%d", i)
104+
default:
105+
labels[i] = strconv.Itoa(i)
97106
}
98107
}
99108

cmd/serially.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78

@@ -21,7 +22,7 @@ var sCommand = cobra.Command{
2122

2223
if workingDirectory != "" {
2324
if err := os.Chdir(workingDirectory); err != nil {
24-
return err
25+
return fmt.Errorf("changing working directory: %w", err)
2526
}
2627
}
2728

@@ -31,12 +32,12 @@ var sCommand = cobra.Command{
3132
}
3233

3334
if len(names) > 0 && len(names) != len(cmdParts) {
34-
return fmt.Errorf("number of names must match number of commands")
35+
return errors.New("number of names must match number of commands")
3536
}
3637

3738
labels := collectLabels(commandStrings)
3839

39-
var cmdErr error
40+
var errCmd error
4041

4142
commands := make([]*konk.Command, len(cmdParts))
4243

@@ -47,7 +48,7 @@ var sCommand = cobra.Command{
4748
parts, err := shellwords.Parse(cmd)
4849

4950
if err != nil {
50-
return err
51+
return fmt.Errorf("parsing command: %w", err)
5152
}
5253

5354
c = konk.NewCommand(konk.CommandConfig{
@@ -73,18 +74,14 @@ var sCommand = cobra.Command{
7374
ctx, cancel := context.WithCancel(context.Background())
7475
defer cancel()
7576

76-
err := c.Run(ctx, cancel, konk.RunCommandConfig{})
77-
78-
if err != nil && !continueOnError {
79-
return err
80-
}
81-
82-
if err != nil {
83-
cmdErr = err
77+
if err := c.Run(ctx, cancel, konk.RunCommandConfig{}); err != nil && continueOnError {
78+
errCmd = err
79+
} else if err != nil {
80+
return fmt.Errorf("running command: %w", err)
8481
}
8582
}
8683

87-
return cmdErr
84+
return errCmd
8885
},
8986
}
9087

integration/helpers.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package test
1+
package integration_test
22

33
import (
4-
"fmt"
54
"os/exec"
65
"sort"
76
"strings"
@@ -71,5 +70,5 @@ func sortOut(t *testing.T, out string) string {
7170
}
7271

7372
// Our output always ends in a newline.
74-
return fmt.Sprintf("%s\n", strings.Join(sortedLines, "\n"))
73+
return strings.Join(sortedLines, "\n") + "\n"
7574
}

integration/proc_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
package test
1+
package integration_test
22

33
import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestProc(t *testing.T) {
1011
t.Parallel()
1112

1213
out, err := newProcRunner().run(t)
13-
assert.NoError(t, err)
14+
require.NoError(t, err)
1415

1516
assert.Equal(t, `[echo-a] a
1617
[echo-b] b
@@ -25,7 +26,7 @@ func TestProcEnvSpaces(t *testing.T) {
2526
"-e", ".env-spaces",
2627
"-p", "Procfile-spaces").
2728
run(t)
28-
assert.NoError(t, err)
29+
require.NoError(t, err)
2930

3031
assert.Equal(t, `[echo-abc] a b c
3132
[echo-def] d "e" f
@@ -39,7 +40,7 @@ func TestProcWithExternalEnvNoEnv(t *testing.T) {
3940
withFlags("-E").
4041
withEnv("A=new-a", "B=new-b", "C=new-c").
4142
run(t)
42-
assert.NoError(t, err)
43+
require.NoError(t, err)
4344

4445
assert.Equal(t, `[echo-a] new-a
4546
[echo-b] new-b
@@ -53,7 +54,7 @@ func TestProcWithExternalEnvAndEnv(t *testing.T) {
5354
out, err := newProcRunner().
5455
withEnv("C=c").
5556
run(t)
56-
assert.NoError(t, err)
57+
require.NoError(t, err)
5758

5859
assert.Equal(t, `[echo-a] a
5960
[echo-b] b

0 commit comments

Comments
 (0)