From ba1a93368ed95d7160062f60fb8b579bc178d3aa Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Thu, 23 Dec 2021 18:21:35 +0100 Subject: [PATCH] ci/run: run golangci-lint Currently golangci-lint doesn't call all staticcheck linters: https://github.com/golangci/golangci-lint/issues/357 --- .golangci.yml | 37 ++++++++++++++++++++++++++++ ci/run | 2 ++ cmd/safcm/config/files_test.go | 2 +- cmd/safcm/config/groups_test.go | 4 +-- cmd/safcm/config/hosts_test.go | 2 +- cmd/safcm/config/permissions_test.go | 2 +- cmd/safcm/config/templates_test.go | 2 +- cmd/safcm/config/triggers_test.go | 2 +- cmd/safcm/main_sync_test.go | 4 +-- cmd/safcm/sync.go | 2 +- cmd/safcm/sync_sync_test.go | 2 +- cmd/safcm/sync_test.go | 2 +- frontend/loop.go | 4 +-- remote/ainsl/ainsl.go | 6 ++--- remote/ainsl/ainsl_test.go | 2 +- remote/main.go | 5 +++- remote/sync/files.go | 16 ++++++------ remote/sync/files_test.go | 4 +-- rpc/conn.go | 2 +- rpc/dial.go | 2 +- 20 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..13bcd7f --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,37 @@ +linters: + disable-all: true + enable: + # Enabled by default + - deadcode + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - structcheck + - typecheck + - unused + - varcheck + # Additional checks + - bodyclose + - contextcheck + - durationcheck + - errname + - exhaustive + - exportloopref + - gofmt + - nilerr + - nolintlint + - predeclared + - rowserrcheck + - unconvert + - wastedassign + + issues: + # Don't hide potential important issues + exclude-use-default: false + +linters-settings: + exhaustive: + # "default" is good enough to be exhaustive + default-signifies-exhaustive: true diff --git a/ci/run b/ci/run index 97354dc..f858a46 100755 --- a/ci/run +++ b/ci/run @@ -34,6 +34,8 @@ make test $flags # Additional static checks only run in CI go install honnef.co/go/tools/cmd/staticcheck@latest staticcheck ./... +go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest +golangci-lint run test -z "$(git clean -nd)" # any untracked files left? make clean diff --git a/cmd/safcm/config/files_test.go b/cmd/safcm/config/files_test.go index 110c012..552ffb7 100644 --- a/cmd/safcm/config/files_test.go +++ b/cmd/safcm/config/files_test.go @@ -39,7 +39,7 @@ func TestLoadFiles(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("../testdata/project") if err != nil { diff --git a/cmd/safcm/config/groups_test.go b/cmd/safcm/config/groups_test.go index 2c9658b..a12b6fb 100644 --- a/cmd/safcm/config/groups_test.go +++ b/cmd/safcm/config/groups_test.go @@ -29,7 +29,7 @@ func TestLoadGroups(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("../testdata/project") if err != nil { @@ -216,7 +216,7 @@ func TestResolveHostGroups(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("../testdata/project") if err != nil { diff --git a/cmd/safcm/config/hosts_test.go b/cmd/safcm/config/hosts_test.go index 5d3c334..4b5dcaf 100644 --- a/cmd/safcm/config/hosts_test.go +++ b/cmd/safcm/config/hosts_test.go @@ -29,7 +29,7 @@ func TestLoadHosts(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck sliceToHosts := func(hosts []*Host) *Hosts { res := &Hosts{ diff --git a/cmd/safcm/config/permissions_test.go b/cmd/safcm/config/permissions_test.go index 001489e..de21f3c 100644 --- a/cmd/safcm/config/permissions_test.go +++ b/cmd/safcm/config/permissions_test.go @@ -30,7 +30,7 @@ func TestLoadPermissions(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("../testdata/project") if err != nil { diff --git a/cmd/safcm/config/templates_test.go b/cmd/safcm/config/templates_test.go index fd9ecb7..3ed9bbf 100644 --- a/cmd/safcm/config/templates_test.go +++ b/cmd/safcm/config/templates_test.go @@ -30,7 +30,7 @@ func TestLoadTemplates(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("../testdata/project") if err != nil { diff --git a/cmd/safcm/config/triggers_test.go b/cmd/safcm/config/triggers_test.go index 63d702a..1997e86 100644 --- a/cmd/safcm/config/triggers_test.go +++ b/cmd/safcm/config/triggers_test.go @@ -30,7 +30,7 @@ func TestLoadTriggers(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("../testdata/project") if err != nil { diff --git a/cmd/safcm/main_sync_test.go b/cmd/safcm/main_sync_test.go index 387caee..6425256 100644 --- a/cmd/safcm/main_sync_test.go +++ b/cmd/safcm/main_sync_test.go @@ -35,7 +35,7 @@ func TestSyncSshEndToEnd(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck var suffix string // Needs different options in sshd_config @@ -56,7 +56,7 @@ func TestSyncSshEndToEnd(t *testing.T) { if err != nil { t.Fatal(err) } - defer sshCmd.Process.Kill() + defer sshCmd.Process.Kill() //nolint:errcheck // Wait until SSH server is ready (up to 30 seconds) for i := 0; i < 30; i++ { diff --git a/cmd/safcm/sync.go b/cmd/safcm/sync.go index 65745af..841898a 100644 --- a/cmd/safcm/sync.go +++ b/cmd/safcm/sync.go @@ -65,7 +65,7 @@ func MainSync(args []string) error { optionSshConfig := flag.String("sshconfig", "", "`path` to ssh configuration file; used for tests") - flag.CommandLine.Parse(args[2:]) + flag.CommandLine.Parse(args[2:]) //nolint:errcheck level, err := safcm.ParseLogLevel(*optionLog) if err != nil { diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index cfa47fa..7f478ff 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -33,7 +33,7 @@ func TestHostSyncReq(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck tests := []struct { name string diff --git a/cmd/safcm/sync_test.go b/cmd/safcm/sync_test.go index 8cde5ab..10ffec1 100644 --- a/cmd/safcm/sync_test.go +++ b/cmd/safcm/sync_test.go @@ -29,7 +29,7 @@ func TestHostsToSync(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("testdata/project") if err != nil { diff --git a/frontend/loop.go b/frontend/loop.go index 5e232a3..232b43a 100644 --- a/frontend/loop.go +++ b/frontend/loop.go @@ -144,10 +144,10 @@ func (l *Loop) syncHost(wg *sync.WaitGroup, host Host) error { err := host.Dial(conn) if err != nil { - conn.Kill() + conn.Kill() //nolint:errcheck return err } - defer conn.Kill() + defer conn.Kill() //nolint:errcheck err = l.SyncHostFunc(conn, host) if err != nil { diff --git a/remote/ainsl/ainsl.go b/remote/ainsl/ainsl.go index 2a64d63..8a65d4d 100644 --- a/remote/ainsl/ainsl.go +++ b/remote/ainsl/ainsl.go @@ -47,7 +47,7 @@ func Main(args []string) error { optionCreate := flag.Bool("create", false, "create the path if it does not exist") - flag.CommandLine.Parse(args[2:]) + flag.CommandLine.Parse(args[2:]) //nolint:errcheck if flag.NArg() != 2 { flag.Usage() @@ -119,7 +119,7 @@ func handle(path string, line string, create bool) ([]string, error) { gid = int(x.Gid) mode = stat.Mode() } - stat = nil // prevent accidental use + stat = nil //nolint:wastedassign // prevent accidental use // Check if the expected line is present var found bool @@ -161,7 +161,7 @@ func handle(path string, line string, create bool) ([]string, error) { } err = unix.Renameat(parentFd, tmpBase, parentFd, baseName) if err != nil { - unix.Unlinkat(parentFd, tmpBase, 0) + unix.Unlinkat(parentFd, tmpBase, 0) //nolint:errcheck return nil, err } err = unix.Fsync(parentFd) diff --git a/remote/ainsl/ainsl_test.go b/remote/ainsl/ainsl_test.go index 07d9242..2e5d2ef 100644 --- a/remote/ainsl/ainsl_test.go +++ b/remote/ainsl/ainsl_test.go @@ -33,7 +33,7 @@ func TestHandle(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.RemoveAll("testdata") if err != nil { diff --git a/remote/main.go b/remote/main.go index 61a6237..903af0a 100644 --- a/remote/main.go +++ b/remote/main.go @@ -75,7 +75,10 @@ func mainLoop() error { var logLevel safcm.LogLevel logFunc := func(level safcm.LogLevel, msg string) { if logLevel >= level { - conn.Send(safcm.MsgLog{ + // Handling errors here is complex and quite verbose. + // If it happens the connection is gone anyway so skip + // the error handling. + conn.Send(safcm.MsgLog{ //nolint:errcheck Level: level, Text: msg, }) diff --git a/remote/sync/files.go b/remote/sync/files.go index 507cc17..9803e93 100644 --- a/remote/sync/files.go +++ b/remote/sync/files.go @@ -250,7 +250,7 @@ reopen: } // Compare file content (if possible) - switch change.Old.Mode.Type() { + switch change.Old.Mode.Type() { //nolint:exhaustive case 0: // regular file x, err := io.ReadAll(oldFh) if err != nil { @@ -446,7 +446,7 @@ reopen: err = unix.Fchownat(parentFd, tmpBase, file.Uid, file.Gid, unix.AT_SYMLINK_NOFOLLOW) if err != nil { - unix.Unlinkat(parentFd, tmpBase, 0) + unix.Unlinkat(parentFd, tmpBase, 0) //nolint:errcheck return err } // Permissions are irrelevant for symlinks (on most systems) @@ -458,7 +458,7 @@ reopen: debugf("renaming %q", slashpath.Join(dir, tmpBase)) err = unix.Renameat(parentFd, tmpBase, parentFd, baseName) if err != nil { - unix.Unlinkat(parentFd, tmpBase, 0) + unix.Unlinkat(parentFd, tmpBase, 0) //nolint:errcheck return err } // To guarantee durability fsync must be called on a parent directory @@ -659,31 +659,31 @@ func WriteTempAt(dirFd int, base string, data []byte, uid, gid int, _, err = fh.Write(data) if err != nil { fh.Close() - unix.Unlinkat(dirFd, tmpBase, 0) + unix.Unlinkat(dirFd, tmpBase, 0) //nolint:errcheck return "", err } // createTempAt() creates the file with 0600 err = fh.Chown(uid, gid) if err != nil { fh.Close() - unix.Unlinkat(dirFd, tmpBase, 0) + unix.Unlinkat(dirFd, tmpBase, 0) //nolint:errcheck return "", err } err = fh.Chmod(mode) if err != nil { fh.Close() - unix.Unlinkat(dirFd, tmpBase, 0) + unix.Unlinkat(dirFd, tmpBase, 0) //nolint:errcheck return "", err } err = fh.Sync() if err != nil { fh.Close() - unix.Unlinkat(dirFd, tmpBase, 0) + unix.Unlinkat(dirFd, tmpBase, 0) //nolint:errcheck return "", err } err = fh.Close() if err != nil { - unix.Unlinkat(dirFd, tmpBase, 0) + unix.Unlinkat(dirFd, tmpBase, 0) //nolint:errcheck return "", err } diff --git a/remote/sync/files_test.go b/remote/sync/files_test.go index 34751ae..bdf432a 100644 --- a/remote/sync/files_test.go +++ b/remote/sync/files_test.go @@ -36,7 +36,7 @@ func TestSyncFiles(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.RemoveAll("testdata") if err != nil { @@ -887,7 +887,7 @@ func TestSyncFile(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Chdir(cwd) + defer os.Chdir(cwd) //nolint:errcheck err = os.RemoveAll("testdata") if err != nil { diff --git a/rpc/conn.go b/rpc/conn.go index 3f7ade1..0ca9cd4 100644 --- a/rpc/conn.go +++ b/rpc/conn.go @@ -140,7 +140,7 @@ func (c *Conn) Kill() error { c.debugf("Kill: killing connection") - c.cmd.Process.Kill() + c.cmd.Process.Kill() //nolint:errcheck return c.wait() } diff --git a/rpc/dial.go b/rpc/dial.go index c098714..fc702d3 100644 --- a/rpc/dial.go +++ b/rpc/dial.go @@ -90,7 +90,7 @@ func (c *Conn) DialSSH(cfg SSHConfig) error { err = c.dialSSH(stdin, stdout) if err != nil { - c.Kill() + c.Kill() //nolint:errcheck return err } c.conn = safcm.NewGobConn(stdout, stdin) -- 2.43.2