Skip to content

Commit

Permalink
Add linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jclem committed Feb 15, 2024
1 parent 47c7f67 commit 7673197
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 80 deletions.
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
issues:
exclude-rules:
- path: cmd/
linters:
- gochecknoglobals
- gochecknoinits

linters:
presets:
- bugs
- comment
# - complexity # limits cyclomatic complexity—typically results in hard-to-follow code
- error
- format
- import
- metalinter
- module
- performance
- sql
- style
- test
- unused

disable:
- depguard # enforces dependency allow list; not using allow list
- paralleltest # not enough tests to matter; not helpful

# annoying/bad
- exhaustruct # enforces exhaustive structs; often incorrect; bad
- goerr113 # disallows returning one-off errors; annoying
- gofumpt # different code formatter; bad
- gomnd # disallows magic numbers; annoying
- ireturn # bans returning interfaces; often incorrect; bad
- lll # limits line length; annoying
- nlreturn # requires blank line before return; annoying
- nonamedreturns # disallows named returns; often required for defer error checking; bad
- tagliatelle # enforces tag name style; often incorrect; bad
- varnamelen # limits variable name length; annoying
- wsl # tedious whitespace rules; annoying
11 changes: 8 additions & 3 deletions cmd/concurrently.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"os"

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

if workingDirectory != "" {
if err := os.Chdir(workingDirectory); err != nil {
return err
return fmt.Errorf("changing working directory: %w", err)
}
}

Expand All @@ -31,7 +32,7 @@ var cCommand = cobra.Command{
}

if len(names) > 0 && len(names) != len(cmdParts) {
return fmt.Errorf("number of names must match number of commands")
return errors.New("number of names must match number of commands")
}

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

return err
if err != nil {
return fmt.Errorf("running commands: %w", err)
}

return nil
},
}

Expand Down
14 changes: 9 additions & 5 deletions cmd/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ var procCommand = cobra.Command{
Use: "proc",
Aliases: []string{"p"},
Short: "Run commands defined in a Procfile (alias: p)",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
dbg := debugger.Get(cmd.Context())
dbg.Flags(cmd)

if workingDirectory != "" {
if err := os.Chdir(workingDirectory); err != nil {
return err
return fmt.Errorf("changing working directory: %w", err)
}
}

procfile, err := os.Open(procfile)
if err != nil {
return err
return fmt.Errorf("opening procfile: %w", err)
}
defer procfile.Close()

Expand All @@ -54,7 +54,7 @@ var procCommand = cobra.Command{
if !noEnvFile {
envFile, err := os.ReadFile(envFile)
if err != nil {
return err
return fmt.Errorf("reading env file: %w", err)
}
envLines = strings.Split(string(envFile), "\n")
}
Expand Down Expand Up @@ -93,7 +93,11 @@ var procCommand = cobra.Command{
dbg.Prettyln(commands)
}

return err
if err != nil {
return fmt.Errorf("running commands: %w", err)
}

return nil
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var rootCmd = cobra.Command{
Use: "konk",
Short: "Konk is a tool for running multiple processes",
Version: Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
// Ensures that usage isn't printed for errors such as non-zero exits.
// SEE: https://github.com/spf13/cobra/issues/340#issuecomment-378726225
cmd.SilenceUsage = true
Expand Down
31 changes: 20 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand All @@ -18,7 +20,7 @@ var runCommand = cobra.Command{
Use: "run <subcommand>",
Aliases: []string{"r"},
Short: "Run commands serially or concurrently (alias: r)",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
_ = cmd.Help()
os.Exit(1)
return nil
Expand Down Expand Up @@ -51,16 +53,22 @@ func collectCommands(args []string) ([]string, []string, error) {
prefix := strings.TrimSuffix(cmd, "*")
pkgFile, err := os.ReadFile("package.json")
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("reading package.json: %w", err)
}
var pkg map[string]interface{}
var pkg map[string]any
if err := json.Unmarshal(pkgFile, &pkg); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("unmarshalling package.json: %w", err)
}

// See if any "scripts" match our prefix
matchingScripts := []string{}
for script := range pkg["scripts"].(map[string]interface{}) {

scripts, ok := pkg["scripts"].(map[string]any)
if !ok {
return nil, nil, errors.New("invalid scripts in package.json")
}

for script := range scripts {
if strings.HasPrefix(script, prefix) {
matchingScripts = append(matchingScripts, script)
}
Expand All @@ -70,13 +78,13 @@ func collectCommands(args []string) ([]string, []string, error) {

for _, script := range matchingScripts {
commandStrings = append(commandStrings, script)
commands = append(commands, fmt.Sprintf("npm run %s", script))
commands = append(commands, "npm run "+script)
}

continue
}

script := fmt.Sprintf("npm run %s", cmd)
script := "npm run " + cmd
commandStrings = append(commandStrings, cmd)
commands = append(commands, script)
}
Expand All @@ -88,12 +96,13 @@ func collectLabels(commandStrings []string) []string {
labels := make([]string, len(commandStrings))

for i, cmdStr := range commandStrings {
if cmdAsLabel {
switch {
case cmdAsLabel:
labels[i] = cmdStr
} else if len(names) > 0 {
case len(names) > 0:
labels[i] = names[i]
} else {
labels[i] = fmt.Sprintf("%d", i)
default:
labels[i] = strconv.Itoa(i)
}
}

Expand Down
23 changes: 10 additions & 13 deletions cmd/serially.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"

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

if workingDirectory != "" {
if err := os.Chdir(workingDirectory); err != nil {
return err
return fmt.Errorf("changing working directory: %w", err)
}
}

Expand All @@ -31,12 +32,12 @@ var sCommand = cobra.Command{
}

if len(names) > 0 && len(names) != len(cmdParts) {
return fmt.Errorf("number of names must match number of commands")
return errors.New("number of names must match number of commands")
}

labels := collectLabels(commandStrings)

var cmdErr error
var errCmd error

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

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

if err != nil {
return err
return fmt.Errorf("parsing command: %w", err)
}

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

err := c.Run(ctx, cancel, konk.RunCommandConfig{})

if err != nil && !continueOnError {
return err
}

if err != nil {
cmdErr = err
if err := c.Run(ctx, cancel, konk.RunCommandConfig{}); err != nil && continueOnError {
errCmd = err
} else if err != nil {
return fmt.Errorf("running command: %w", err)
}
}

return cmdErr
return errCmd
},
}

Expand Down
5 changes: 2 additions & 3 deletions integration/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test
package integration_test

import (
"fmt"
"os/exec"
"sort"
"strings"
Expand Down Expand Up @@ -71,5 +70,5 @@ func sortOut(t *testing.T, out string) string {
}

// Our output always ends in a newline.
return fmt.Sprintf("%s\n", strings.Join(sortedLines, "\n"))
return strings.Join(sortedLines, "\n") + "\n"
}
11 changes: 6 additions & 5 deletions integration/proc_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package test
package integration_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestProc(t *testing.T) {
t.Parallel()

out, err := newProcRunner().run(t)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, `[echo-a] a
[echo-b] b
Expand All @@ -25,7 +26,7 @@ func TestProcEnvSpaces(t *testing.T) {
"-e", ".env-spaces",
"-p", "Procfile-spaces").
run(t)
assert.NoError(t, err)
require.NoError(t, err)

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

assert.Equal(t, `[echo-a] new-a
[echo-b] new-b
Expand All @@ -53,7 +54,7 @@ func TestProcWithExternalEnvAndEnv(t *testing.T) {
out, err := newProcRunner().
withEnv("C=c").
run(t)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, `[echo-a] a
[echo-b] b
Expand Down
Loading

0 comments on commit 7673197

Please sign in to comment.