From 165b00e76555ffabb37c8ea14a9db17d46017bde Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 2 Jul 2024 17:42:46 +0100 Subject: [PATCH 1/5] chore: add lint target to Makefile --- Makefile | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Makefile b/Makefile index 42fd1db4..32bad859 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,23 @@ PWD=$(shell pwd) GO_SRC_FILES := $(shell find . -type f -name '*.go' -not -name '*_test.go') GO_TEST_FILES := $(shell find . -type f -not -name '*.go' -name '*_test.go') GOLDEN_FILES := $(shell find . -type f -name '*.golden') +SHELL_SRC_FILES := $(shell find . -type f -name '*.sh') fmt: $(shell find . -type f -name '*.go') go run mvdan.cc/gofumpt@v0.6.0 -l -w . +.PHONY: lint +lint: lint/go lint/shellcheck + +.PHONY: lint/go +lint/go: $(GO_SRC_FILES) + golangci-lint run + +.PHONY: lint/shellcheck +lint/shellcheck: $(SHELL_SRC_FILES) + echo "--- shellcheck" + shellcheck --external-sources $(SHELL_SRC_FILES) + develop: ./scripts/develop.sh From e54bf6610647e0fd231944c78b0eaa7298b52187 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 2 Jul 2024 17:43:24 +0100 Subject: [PATCH 2/5] ci: run make lint --- .github/workflows/ci.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6dc12b3d..dbd4e181 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -39,6 +39,9 @@ jobs: with: go-version: "~1.22" + - name: Lint + run: make -j lint + - name: Test run: make test docs: From ef12e29ac1788bbf457c255dc5e2e8c5c38f5239 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 2 Jul 2024 17:43:39 +0100 Subject: [PATCH 3/5] appease the linter gods --- cmd/envbuilder/main.go | 15 ++++++++++----- envbuilder_internal_test.go | 15 ++++++++++----- integration/integration_test.go | 6 ++++-- scripts/version.sh | 2 +- testutil/registrytest/registrytest.go | 10 ---------- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/cmd/envbuilder/main.go b/cmd/envbuilder/main.go index aa3b3ec4..aea83d25 100644 --- a/cmd/envbuilder/main.go +++ b/cmd/envbuilder/main.go @@ -57,8 +57,11 @@ func envbuilderCmd() serpent.Command { }, } var flushAndClose func(ctx context.Context) error + // nolint: staticcheck // FIXME: https://github.com/coder/envbuilder/issues/260 sendLogs, flushAndClose = notcodersdk.LogsSender(notcodersdk.ExternalLogSourceID, client.PatchLogs, slog.Logger{}) - defer flushAndClose(inv.Context()) + defer func() { + _ = flushAndClose(inv.Context()) + }() // This adds the envbuilder subsystem. // If telemetry is enabled in a Coder deployment, @@ -66,19 +69,21 @@ func envbuilderCmd() serpent.Command { // envbuilder usage. if !slices.Contains(options.CoderAgentSubsystem, string(notcodersdk.AgentSubsystemEnvbuilder)) { options.CoderAgentSubsystem = append(options.CoderAgentSubsystem, string(notcodersdk.AgentSubsystemEnvbuilder)) - os.Setenv("CODER_AGENT_SUBSYSTEM", strings.Join(options.CoderAgentSubsystem, ",")) + _ = os.Setenv("CODER_AGENT_SUBSYSTEM", strings.Join(options.CoderAgentSubsystem, ",")) } } options.Logger = func(level notcodersdk.LogLevel, format string, args ...interface{}) { output := fmt.Sprintf(format, args...) - fmt.Fprintln(inv.Stderr, output) + _, _ = fmt.Fprintln(inv.Stderr, output) if sendLogs != nil { - sendLogs(inv.Context(), notcodersdk.Log{ + if err := sendLogs(inv.Context(), notcodersdk.Log{ CreatedAt: time.Now(), Output: output, Level: level, - }) + }); err != nil { + _, _ = fmt.Fprintf(inv.Stderr, "failed to send logs: %s\n", err.Error()) + } } } diff --git a/envbuilder_internal_test.go b/envbuilder_internal_test.go index 6ca5fc12..65edb9cd 100644 --- a/envbuilder_internal_test.go +++ b/envbuilder_internal_test.go @@ -52,7 +52,8 @@ func TestFindDevcontainerJSON(t *testing.T) { fs := memfs.New() err := fs.MkdirAll("/workspace/.devcontainer", 0o600) require.NoError(t, err) - fs.Create("/workspace/.devcontainer/devcontainer.json") + _, err = fs.Create("/workspace/.devcontainer/devcontainer.json") + require.NoError(t, err) // when devcontainerPath, devcontainerDir, err := findDevcontainerJSON(Options{ @@ -73,7 +74,8 @@ func TestFindDevcontainerJSON(t *testing.T) { fs := memfs.New() err := fs.MkdirAll("/workspace/experimental-devcontainer", 0o600) require.NoError(t, err) - fs.Create("/workspace/experimental-devcontainer/devcontainer.json") + _, err = fs.Create("/workspace/experimental-devcontainer/devcontainer.json") + require.NoError(t, err) // when devcontainerPath, devcontainerDir, err := findDevcontainerJSON(Options{ @@ -95,7 +97,8 @@ func TestFindDevcontainerJSON(t *testing.T) { fs := memfs.New() err := fs.MkdirAll("/workspace/.devcontainer", 0o600) require.NoError(t, err) - fs.Create("/workspace/.devcontainer/experimental.json") + _, err = fs.Create("/workspace/.devcontainer/experimental.json") + require.NoError(t, err) // when devcontainerPath, devcontainerDir, err := findDevcontainerJSON(Options{ @@ -117,7 +120,8 @@ func TestFindDevcontainerJSON(t *testing.T) { fs := memfs.New() err := fs.MkdirAll("/workspace", 0o600) require.NoError(t, err) - fs.Create("/workspace/devcontainer.json") + _, err = fs.Create("/workspace/devcontainer.json") + require.NoError(t, err) // when devcontainerPath, devcontainerDir, err := findDevcontainerJSON(Options{ @@ -138,7 +142,8 @@ func TestFindDevcontainerJSON(t *testing.T) { fs := memfs.New() err := fs.MkdirAll("/workspace/.devcontainer/sample", 0o600) require.NoError(t, err) - fs.Create("/workspace/.devcontainer/sample/devcontainer.json") + _, err = fs.Create("/workspace/.devcontainer/sample/devcontainer.json") + require.NoError(t, err) // when devcontainerPath, devcontainerDir, err := findDevcontainerJSON(Options{ diff --git a/integration/integration_test.go b/integration/integration_test.go index 1364e966..ae7047c0 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -1612,9 +1612,11 @@ func cleanOldEnvbuilders() { panic(err) } for _, ctr := range ctrs { - cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{ + if err := cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{ Force: true, - }) + }); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "failed to remove old test container: %s\n", err.Error()) + } } } diff --git a/scripts/version.sh b/scripts/version.sh index bf78d02c..31968d27 100755 --- a/scripts/version.sh +++ b/scripts/version.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -euo pipefail -cd $(dirname "${BASH_SOURCE[0]}") +cd "$(dirname "${BASH_SOURCE[0]}")" last_tag="$(git describe --tags --abbrev=0)" version="$last_tag" diff --git a/testutil/registrytest/registrytest.go b/testutil/registrytest/registrytest.go index 0bc3d312..033fd75b 100644 --- a/testutil/registrytest/registrytest.go +++ b/testutil/registrytest/registrytest.go @@ -44,16 +44,6 @@ func New(t *testing.T) string { return srv.URL } -type logrusFormatter struct { - callback func(entry *logrus.Entry) - empty []byte -} - -func (f *logrusFormatter) Format(entry *logrus.Entry) ([]byte, error) { - f.callback(entry) - return f.empty, nil -} - // WriteContainer uploads a container to the registry server. // It returns the reference to the uploaded container. func WriteContainer(t *testing.T, serverURL, containerRef, mediaType string, files map[string]any) string { From c9433877e94db2bd8bcf36001e12b451d140a1af Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 2 Jul 2024 18:52:14 +0100 Subject: [PATCH 4/5] go install golangci-lint --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 32bad859..01a4f216 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ GO_SRC_FILES := $(shell find . -type f -name '*.go' -not -name '*_test.go') GO_TEST_FILES := $(shell find . -type f -not -name '*.go' -name '*_test.go') GOLDEN_FILES := $(shell find . -type f -name '*.golden') SHELL_SRC_FILES := $(shell find . -type f -name '*.sh') +GOLANGCI_LINT_VERSION := v1.59.1 fmt: $(shell find . -type f -name '*.go') go run mvdan.cc/gofumpt@v0.6.0 -l -w . @@ -14,6 +15,7 @@ lint: lint/go lint/shellcheck .PHONY: lint/go lint/go: $(GO_SRC_FILES) + go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) golangci-lint run .PHONY: lint/shellcheck From 2157d4dce470afb8898319451664b88b22007242 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 2 Jul 2024 18:59:01 +0100 Subject: [PATCH 5/5] appease the shell linter god --- init.sh | 4 ++-- scripts/build.sh | 12 ++++++------ scripts/develop.sh | 2 +- scripts/diagram.sh | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/init.sh b/init.sh index 350a664a..a2990e0d 100644 --- a/init.sh +++ b/init.sh @@ -3,5 +3,5 @@ echo hey there sleep 1 -echo INIT_COMMAND=/bin/sh >> $ENVBUILDER_ENV -echo INIT_ARGS="-c /bin/bash" >> $ENVBUILDER_ENV \ No newline at end of file +echo INIT_COMMAND=/bin/sh >> "${ENVBUILDER_ENV}" +echo INIT_ARGS="-c /bin/bash" >> "${ENVBUILDER_ENV}" \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh index 2fac5e04..e186dc02 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -cd $(dirname "${BASH_SOURCE[0]}") +cd "$(dirname "${BASH_SOURCE[0]}")" set -euo pipefail archs=() @@ -48,13 +48,13 @@ docker buildx inspect --bootstrap &> /dev/null for arch in "${archs[@]}"; do echo "Building for $arch..." - GOARCH=$arch CGO_ENABLED=0 go build -o ./envbuilder-$arch ../cmd/envbuilder & + GOARCH=$arch CGO_ENABLED=0 go build -o "./envbuilder-${arch}" ../cmd/envbuilder & done wait args=() for arch in "${archs[@]}"; do - args+=( --platform linux/$arch ) + args+=( --platform "linux/${arch}" ) done if [ "$push" = true ]; then args+=( --push ) @@ -62,10 +62,10 @@ else args+=( --load ) fi -docker buildx build --builder $BUILDER_NAME "${args[@]}" -t $base:$tag -t $base:latest -f Dockerfile . +docker buildx build --builder $BUILDER_NAME "${args[@]}" -t "${base}:${tag}" -t "${base}:latest" -f Dockerfile . # Check if archs contains the current. If so, then output a message! -if [[ -z "${CI:-}" ]] && [[ " ${archs[@]} " =~ " ${current} " ]]; then - docker tag $base:$tag envbuilder:latest +if [[ -z "${CI:-}" ]] && [[ " ${archs[*]} " =~ ${current} ]]; then + docker tag "${base}:${tag}" envbuilder:latest echo "Tagged $current as envbuilder:latest!" fi diff --git a/scripts/develop.sh b/scripts/develop.sh index 8336eca7..3147244b 100755 --- a/scripts/develop.sh +++ b/scripts/develop.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -cd $(dirname "${BASH_SOURCE[0]}") +cd "$(dirname "${BASH_SOURCE[0]}")" set -euxo pipefail ./build.sh diff --git a/scripts/diagram.sh b/scripts/diagram.sh index e0c5e6b4..b6fe5da2 100755 --- a/scripts/diagram.sh +++ b/scripts/diagram.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -cd $(dirname "${BASH_SOURCE[0]}") +cd "$(dirname "${BASH_SOURCE[0]}")" set -euxo pipefail d2 ./diagram.d2 --pad=32 -t 1 ./diagram-light.svg