Skip to content

ci: add linters, run linters, make linters happy #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
with:
go-version: "~1.22"

- name: Lint
run: make -j lint

- name: Test
run: make test
docs:
Expand Down
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ 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')
GOLANGCI_LINT_VERSION := v1.59.1

fmt: $(shell find . -type f -name '*.go')
go run mvdan.cc/[email protected] -l -w .

.PHONY: lint
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
lint/shellcheck: $(SHELL_SRC_FILES)
echo "--- shellcheck"
shellcheck --external-sources $(SHELL_SRC_FILES)

develop:
./scripts/develop.sh

Expand Down
15 changes: 10 additions & 5 deletions cmd/envbuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,33 @@ 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,
// this will be reported and help us understand
// 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())
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions envbuilder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
echo hey there
sleep 1

echo INIT_COMMAND=/bin/sh >> $ENVBUILDER_ENV
echo INIT_ARGS="-c /bin/bash" >> $ENVBUILDER_ENV
echo INIT_COMMAND=/bin/sh >> "${ENVBUILDER_ENV}"
echo INIT_ARGS="-c /bin/bash" >> "${ENVBUILDER_ENV}"
6 changes: 4 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

cd $(dirname "${BASH_SOURCE[0]}")
cd "$(dirname "${BASH_SOURCE[0]}")"
set -euo pipefail

archs=()
Expand Down Expand Up @@ -48,24 +48,24 @@ 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 )
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
2 changes: 1 addition & 1 deletion scripts/develop.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

cd $(dirname "${BASH_SOURCE[0]}")
cd "$(dirname "${BASH_SOURCE[0]}")"
set -euxo pipefail

./build.sh
Expand Down
2 changes: 1 addition & 1 deletion scripts/diagram.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/version.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 0 additions & 10 deletions testutil/registrytest/registrytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down