Skip to content

Commit 2ac8896

Browse files
committed
build: introduce Makefile for unified build, lint, and dev workflows
- Add Makefile to automate building, testing, linting, and development workflows - Include targets for building on multiple platforms, hot reload support, and code generation with templ - Add linting, formatting, and dependency management tasks - Provide Docker integration for build and run - Implement tool checks and project cleanup commands Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent 5e17206 commit 2ac8896

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Makefile

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
GO ?= go
2+
EXECUTABLE := cli
3+
GOFILES := $(shell find . -type f -name "*.go")
4+
TAGS ?=
5+
TEMPL_VERSION ?= latest
6+
7+
ifneq ($(shell uname), Darwin)
8+
EXTLDFLAGS = -extldflags "-static" $(null)
9+
else
10+
EXTLDFLAGS =
11+
endif
12+
13+
ifneq ($(DRONE_TAG),)
14+
VERSION ?= $(DRONE_TAG)
15+
else
16+
VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD)
17+
endif
18+
COMMIT ?= $(shell git rev-parse --short HEAD)
19+
20+
LDFLAGS ?=
21+
## build: build the authgate binary
22+
build: generate $(EXECUTABLE)
23+
24+
$(EXECUTABLE): $(GOFILES)
25+
$(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o bin/$@ .
26+
27+
## air: Install air for hot reload.
28+
air:
29+
@hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
30+
$(GO) install github.com/air-verse/air@latest; \
31+
fi
32+
33+
## dev: Run the application with hot reload.
34+
dev: air
35+
air
36+
37+
## install: install the authgate binary
38+
install: $(GOFILES)
39+
$(GO) install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
40+
41+
## test: run tests
42+
test: generate
43+
@$(GO) test -v -cover -coverprofile coverage.txt ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1
44+
45+
## coverage: view test coverage in browser
46+
coverage: test
47+
$(GO) tool cover -html=coverage.txt
48+
49+
## install-golangci-lint: install golangci-lint if not present
50+
install-golangci-lint:
51+
@command -v golangci-lint >/dev/null 2>&1 || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $$($(GO) env GOPATH)/bin v2.7.2
52+
53+
## fmt: format go files using golangci-lint
54+
fmt: install-golangci-lint
55+
golangci-lint fmt
56+
57+
## lint: run golangci-lint to check for issues
58+
lint: install-golangci-lint
59+
golangci-lint run
60+
61+
## build_linux_amd64: build the authgate binary for linux amd64
62+
build_linux_amd64: generate
63+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/amd64/$(EXECUTABLE) .
64+
65+
## build_linux_arm64: build the authgate binary for linux arm64
66+
build_linux_arm64: generate
67+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/arm64/$(EXECUTABLE) .
68+
69+
## clean: remove build artifacts and test coverage
70+
clean:
71+
rm -rf bin/ release/ coverage.txt
72+
find internal/templates -name "*_templ.go" -delete
73+
74+
## rebuild: clean and build
75+
rebuild: clean build
76+
77+
.PHONY: help build install test coverage fmt lint clean rebuild
78+
.PHONY: build_linux_amd64 build_linux_arm64
79+
.PHONY: install-templ generate watch air dev
80+
.PHONY: install-golangci-lint mod-download mod-tidy mod-verify check-tools version
81+
.PHONY: docker-build docker-run
82+
83+
## install-templ: install templ CLI if not installed
84+
install-templ:
85+
@command -v templ >/dev/null 2>&1 || $(GO) install github.com/a-h/templ/cmd/templ@$(TEMPL_VERSION)
86+
87+
## generate: run templ generate to compile .templ files
88+
generate: install-templ
89+
templ generate
90+
91+
## watch: watch mode for automatic regeneration
92+
watch: install-templ
93+
templ generate --watch
94+
95+
## help: print this help message
96+
help:
97+
@echo 'Usage:'
98+
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
99+
100+
## mod-download: download go module dependencies
101+
mod-download:
102+
$(GO) mod download
103+
104+
## mod-tidy: tidy go module dependencies
105+
mod-tidy:
106+
$(GO) mod tidy
107+
108+
## mod-verify: verify go module dependencies
109+
mod-verify:
110+
$(GO) mod verify
111+
112+
## check-tools: verify required tools are installed
113+
check-tools:
114+
@command -v $(GO) >/dev/null 2>&1 || (echo "Go not found" && exit 1)
115+
@command -v templ >/dev/null 2>&1 || echo "templ not installed (run: make install-templ)"
116+
@command -v golangci-lint >/dev/null 2>&1 || echo "golangci-lint not installed (run: make install-golangci-lint)"
117+
118+
## version: display version information
119+
version:
120+
@echo "Version: $(VERSION)"
121+
@echo "Commit: $(COMMIT)"
122+
@echo "Go Version: $(shell $(GO) version)"
123+
124+
## docker-build: build docker image
125+
docker-build:
126+
docker build -t authgate:$(VERSION) -f Dockerfile .
127+
128+
## docker-run: run docker container
129+
docker-run:
130+
docker run -p 8080:8080 --env-file .env authgate:$(VERSION)
131+

0 commit comments

Comments
 (0)