Skip to content

Commit 509babe

Browse files
committed
feat: add Makefile with lint/shellcheck and fmt/shfmt targets
- Add Makefile with build, test, lint, and fmt targets - Fix shellcheck warnings in scripts/helm.sh and scripts/version.sh - Format shell scripts with shfmt
1 parent d79fcde commit 509babe

File tree

5 files changed

+179
-92
lines changed

5 files changed

+179
-92
lines changed

Makefile

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Colors for output
2+
GREEN := $(shell printf '\033[32m')
3+
RESET := $(shell printf '\033[0m')
4+
BOLD := $(shell printf '\033[1m')
5+
6+
# Shell source files - use shfmt to find them (respects .editorconfig)
7+
SHELL_SRC_FILES := $(shell shfmt -f .)
8+
9+
.PHONY: all
10+
all: build
11+
12+
.PHONY: build
13+
build:
14+
go build ./...
15+
16+
.PHONY: test
17+
test:
18+
go test ./... -race
19+
20+
.PHONY: test/integration
21+
test/integration:
22+
go test -tags=integration -v -timeout=8m ./...
23+
24+
.PHONY: lint
25+
lint: lint/go lint/shellcheck
26+
27+
.PHONY: lint/go
28+
lint/go:
29+
golangci-lint run --timeout=5m
30+
31+
.PHONY: lint/shellcheck
32+
lint/shellcheck: $(SHELL_SRC_FILES)
33+
echo "--- shellcheck"
34+
shellcheck --external-sources $(SHELL_SRC_FILES)
35+
36+
.PHONY: fmt
37+
fmt: fmt/go fmt/shfmt
38+
39+
.PHONY: fmt/go
40+
fmt/go:
41+
go fmt ./...
42+
43+
.PHONY: fmt/shfmt
44+
fmt/shfmt: $(SHELL_SRC_FILES)
45+
ifdef FILE
46+
# Format single shell script
47+
if [[ -f "$(FILE)" ]] && [[ "$(FILE)" == *.sh ]]; then \
48+
echo "$(GREEN)==>$(RESET) $(BOLD)fmt/shfmt$(RESET) $(FILE)"; \
49+
shfmt -w "$(FILE)"; \
50+
fi
51+
else
52+
echo "$(GREEN)==>$(RESET) $(BOLD)fmt/shfmt$(RESET)"
53+
# Only do diff check in CI, errors on diff.
54+
ifdef CI
55+
shfmt -d $(SHELL_SRC_FILES)
56+
else
57+
shfmt -w $(SHELL_SRC_FILES)
58+
endif
59+
endif
60+
61+
.PHONY: clean
62+
clean:
63+
rm -f coder-logstream-kube
64+
65+
.PHONY: kind/create
66+
kind/create:
67+
./scripts/kind-setup.sh create
68+
69+
.PHONY: kind/delete
70+
kind/delete:
71+
./scripts/kind-setup.sh delete
72+
73+
.PHONY: help
74+
help:
75+
@echo "Available targets:"
76+
@echo " build - Build the project"
77+
@echo " test - Run unit tests"
78+
@echo " test/integration - Run integration tests (requires KinD cluster)"
79+
@echo " lint - Run all linters"
80+
@echo " lint/go - Run golangci-lint"
81+
@echo " lint/shellcheck - Run shellcheck on shell scripts"
82+
@echo " fmt - Format all code"
83+
@echo " fmt/go - Format Go code"
84+
@echo " fmt/shfmt - Format shell scripts"
85+
@echo " kind/create - Create KinD cluster for integration tests"
86+
@echo " kind/delete - Delete KinD cluster"
87+
@echo " clean - Remove build artifacts"

scripts/build.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ archs=(amd64 arm64 arm)
1313

1414
# build for all architectures
1515
for arch in "${archs[@]}"; do
16-
echo "Building for $arch"
17-
GOARCH=$arch GOOS=linux CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-logstream-kube-"$arch" ../
16+
echo "Building for $arch"
17+
GOARCH=$arch GOOS=linux CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-logstream-kube-"$arch" ../
1818
done
1919

2020
# We have to use docker buildx to tag multiple images with
@@ -24,26 +24,26 @@ BUILDER_EXISTS=$(docker buildx ls | grep $BUILDER_NAME || true)
2424

2525
# If builder doesn't exist, create it
2626
if [ -z "$BUILDER_EXISTS" ]; then
27-
echo "Creating dockerx builder $BUILDER_NAME..."
28-
docker buildx create --use --platform=linux/arm64,linux/amd64,linux/arm/v7 --name $BUILDER_NAME
27+
echo "Creating dockerx builder $BUILDER_NAME..."
28+
docker buildx create --use --platform=linux/arm64,linux/amd64,linux/arm/v7 --name $BUILDER_NAME
2929
else
30-
echo "Builder $BUILDER_NAME already exists. Using it."
30+
echo "Builder $BUILDER_NAME already exists. Using it."
3131
fi
3232

3333
# Ensure the builder is bootstrapped and ready to use
3434
docker buildx inspect --bootstrap &>/dev/null
3535

3636
# Build and push the image
3737
if [ "$CI" = "false" ]; then
38-
docker buildx build --platform linux/"$current" -t coder-logstream-kube --load .
38+
docker buildx build --platform linux/"$current" -t coder-logstream-kube --load .
3939
else
40-
VERSION=$(../scripts/version.sh)
41-
BASE=ghcr.io/coder/coder-logstream-kube
42-
IMAGE=$BASE:$VERSION
43-
# if version contains "rc" skip pushing to latest
44-
if [[ $VERSION == *"rc"* ]]; then
45-
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t "$IMAGE" --push .
46-
else
47-
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t "$IMAGE" -t $BASE:latest --push .
48-
fi
40+
VERSION=$(../scripts/version.sh)
41+
BASE=ghcr.io/coder/coder-logstream-kube
42+
IMAGE=$BASE:$VERSION
43+
# if version contains "rc" skip pushing to latest
44+
if [[ $VERSION == *"rc"* ]]; then
45+
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t "$IMAGE" --push .
46+
else
47+
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t "$IMAGE" -t $BASE:latest --push .
48+
fi
4949
fi

scripts/helm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# to the Coder OSS repo. This requires `gsutil` to be installed and configured.
1616

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

2020
log() {
2121
echo "$*" 1>&2

scripts/kind-setup.sh

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,102 +8,102 @@ set -euo pipefail
88
CLUSTER_NAME="${KIND_CLUSTER_NAME:-logstream-integration-test}"
99

1010
usage() {
11-
echo "Usage: $0 [create|delete|status]"
12-
echo ""
13-
echo "Commands:"
14-
echo " create - Create a KinD cluster for integration tests"
15-
echo " delete - Delete the KinD cluster"
16-
echo " status - Check if the cluster exists and is running"
17-
echo ""
18-
echo "Environment variables:"
19-
echo " KIND_CLUSTER_NAME - Name of the cluster (default: logstream-integration-test)"
20-
exit 1
11+
echo "Usage: $0 [create|delete|status]"
12+
echo ""
13+
echo "Commands:"
14+
echo " create - Create a KinD cluster for integration tests"
15+
echo " delete - Delete the KinD cluster"
16+
echo " status - Check if the cluster exists and is running"
17+
echo ""
18+
echo "Environment variables:"
19+
echo " KIND_CLUSTER_NAME - Name of the cluster (default: logstream-integration-test)"
20+
exit 1
2121
}
2222

2323
check_kind() {
24-
if ! command -v kind &> /dev/null; then
25-
echo "Error: 'kind' is not installed."
26-
echo "Install it from: https://kind.sigs.k8s.io/docs/user/quick-start/#installation"
27-
exit 1
28-
fi
24+
if ! command -v kind &>/dev/null; then
25+
echo "Error: 'kind' is not installed."
26+
echo "Install it from: https://kind.sigs.k8s.io/docs/user/quick-start/#installation"
27+
exit 1
28+
fi
2929
}
3030

3131
check_kubectl() {
32-
if ! command -v kubectl &> /dev/null; then
33-
echo "Error: 'kubectl' is not installed."
34-
echo "Install it from: https://kubernetes.io/docs/tasks/tools/"
35-
exit 1
36-
fi
32+
if ! command -v kubectl &>/dev/null; then
33+
echo "Error: 'kubectl' is not installed."
34+
echo "Install it from: https://kubernetes.io/docs/tasks/tools/"
35+
exit 1
36+
fi
3737
}
3838

3939
cluster_exists() {
40-
kind get clusters 2>/dev/null | grep -q "^${CLUSTER_NAME}$"
40+
kind get clusters 2>/dev/null | grep -q "^${CLUSTER_NAME}$"
4141
}
4242

4343
create_cluster() {
44-
check_kind
45-
check_kubectl
46-
47-
if cluster_exists; then
48-
echo "Cluster '${CLUSTER_NAME}' already exists."
49-
echo "Use '$0 delete' to remove it first, or '$0 status' to check its status."
50-
exit 0
51-
fi
52-
53-
echo "Creating KinD cluster '${CLUSTER_NAME}'..."
54-
kind create cluster --name "${CLUSTER_NAME}" --wait 60s
55-
56-
echo ""
57-
echo "Cluster created successfully!"
58-
echo ""
59-
echo "To run integration tests:"
60-
echo " go test -tags=integration -v ./..."
61-
echo ""
62-
echo "To delete the cluster when done:"
63-
echo " $0 delete"
44+
check_kind
45+
check_kubectl
46+
47+
if cluster_exists; then
48+
echo "Cluster '${CLUSTER_NAME}' already exists."
49+
echo "Use '$0 delete' to remove it first, or '$0 status' to check its status."
50+
exit 0
51+
fi
52+
53+
echo "Creating KinD cluster '${CLUSTER_NAME}'..."
54+
kind create cluster --name "${CLUSTER_NAME}" --wait 60s
55+
56+
echo ""
57+
echo "Cluster created successfully!"
58+
echo ""
59+
echo "To run integration tests:"
60+
echo " go test -tags=integration -v ./..."
61+
echo ""
62+
echo "To delete the cluster when done:"
63+
echo " $0 delete"
6464
}
6565

6666
delete_cluster() {
67-
check_kind
67+
check_kind
6868

69-
if ! cluster_exists; then
70-
echo "Cluster '${CLUSTER_NAME}' does not exist."
71-
exit 0
72-
fi
69+
if ! cluster_exists; then
70+
echo "Cluster '${CLUSTER_NAME}' does not exist."
71+
exit 0
72+
fi
7373

74-
echo "Deleting KinD cluster '${CLUSTER_NAME}'..."
75-
kind delete cluster --name "${CLUSTER_NAME}"
76-
echo "Cluster deleted successfully!"
74+
echo "Deleting KinD cluster '${CLUSTER_NAME}'..."
75+
kind delete cluster --name "${CLUSTER_NAME}"
76+
echo "Cluster deleted successfully!"
7777
}
7878

7979
status_cluster() {
80-
check_kind
81-
82-
if cluster_exists; then
83-
echo "Cluster '${CLUSTER_NAME}' exists."
84-
echo ""
85-
echo "Cluster info:"
86-
kubectl cluster-info --context "kind-${CLUSTER_NAME}" 2>/dev/null || echo " (unable to get cluster info)"
87-
echo ""
88-
echo "Nodes:"
89-
kubectl get nodes --context "kind-${CLUSTER_NAME}" 2>/dev/null || echo " (unable to get nodes)"
90-
else
91-
echo "Cluster '${CLUSTER_NAME}' does not exist."
92-
echo "Use '$0 create' to create it."
93-
fi
80+
check_kind
81+
82+
if cluster_exists; then
83+
echo "Cluster '${CLUSTER_NAME}' exists."
84+
echo ""
85+
echo "Cluster info:"
86+
kubectl cluster-info --context "kind-${CLUSTER_NAME}" 2>/dev/null || echo " (unable to get cluster info)"
87+
echo ""
88+
echo "Nodes:"
89+
kubectl get nodes --context "kind-${CLUSTER_NAME}" 2>/dev/null || echo " (unable to get nodes)"
90+
else
91+
echo "Cluster '${CLUSTER_NAME}' does not exist."
92+
echo "Use '$0 create' to create it."
93+
fi
9494
}
9595

9696
case "${1:-}" in
97-
create)
98-
create_cluster
99-
;;
100-
delete)
101-
delete_cluster
102-
;;
103-
status)
104-
status_cluster
105-
;;
106-
*)
107-
usage
108-
;;
97+
create)
98+
create_cluster
99+
;;
100+
delete)
101+
delete_cluster
102+
;;
103+
status)
104+
status_cluster
105+
;;
106+
*)
107+
usage
108+
;;
109109
esac

scripts/version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

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

66
last_tag="$(git describe --tags --abbrev=0)"
77
version="$last_tag"

0 commit comments

Comments
 (0)