Skip to content

Commit e8970fd

Browse files
committed
refactor make file
1 parent 3832887 commit e8970fd

File tree

4 files changed

+131
-129
lines changed

4 files changed

+131
-129
lines changed

Makefile

Lines changed: 2 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -5,135 +5,20 @@ SHELL := /bin/bash
55
## NOTINTERMEDIATE requires make >=4.4
66
.NOTINTERMEDIATE:
77

8-
GO_EXEC ?= go
9-
export GO_EXEC
10-
DOCKER_EXEC ?= docker
11-
export DOCKER_EXEC
12-
13-
MODULE := $(shell cat go.mod | grep -e "^module" | sed "s/^module //")
14-
VERSION ?= 0.0.0
15-
X_FLAGS = \
16-
-X '$(MODULE)/build.Version=$(VERSION)' \
17-
-X '$(MODULE)/build.Branch=$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || true)' \
18-
-X '$(MODULE)/build.Commit=$(shell git rev-parse HEAD 2>/dev/null || true)' \
19-
-X '$(MODULE)/build.CommitShort=$(shell git rev-parse --short HEAD 2>/dev/null || true)' \
20-
-X '$(MODULE)/build.Tag=$(shell git describe --tags 2>/dev/null || true)'
21-
IMAGE_NAME ?= goboilerplate
22-
IMAGE_TAG ?= latest
23-
24-
GO_PACKAGES = $(GO_EXEC) list -tags='$(TAGS)' -mod=vendor ./...
25-
GO_FOLDERS = $(GO_EXEC) list -tags='$(TAGS)' -mod=vendor -f '{{ .Dir }}' ./...
26-
GO_FILES = find . -type f -name '*.go' -not -path './vendor/*'
27-
28-
export GO111MODULE := on
29-
#export GOFLAGS := -mod=vendor
30-
GOPATH := $(shell go env GOPATH)
31-
GO_VER := $(shell go env GOVERSION)
32-
BUILD_OUTPUT ?= $(CURDIR)/output
33-
8+
include $(CURDIR)/scripts/go.mk
9+
include $(CURDIR)/scripts/docker.mk
3410
include $(CURDIR)/scripts/tools.mk
3511

3612
.DEFAULT_GOAL=default
3713
.PHONY: default
3814
default: checks build
3915

40-
.PHONY: mod
41-
mod:
42-
$(GO_EXEC) mod tidy -go=1.23
43-
$(GO_EXEC) mod verify
44-
45-
.PHONY: vendor
46-
vendor:
47-
$(GO_EXEC) mod vendor
48-
49-
# https://go.dev/ref/mod#go-get
50-
# -u flag tells go get to upgrade modules
51-
# -t flag tells go get to consider modules needed to build tests of packages named on the command line.
52-
# When -t and -u are used together, go get will update test dependencies as well.
53-
.PHONY: go-deps-upgrade
54-
go-deps-upgrade:
55-
$(GO_EXEC) get -u -t ./...
56-
$(GO_EXEC) mod tidy -go=1.23
57-
$(GO_EXEC) mod vendor
58-
59-
# https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies
60-
# https://pkg.go.dev/cmd/compile
61-
# https://pkg.go.dev/cmd/link
62-
63-
#BUILD_FLAGS := -mod=vendor -a -ldflags "-s -w $(X_FLAGS) -extldflags='-static'" -tags '$(TAGS)'
64-
BUILD_FLAGS := -mod=vendor -a -ldflags '-s -w $(X_FLAGS)' -tags '$(TAGS)'
65-
BUILD_FLAGS_DEBUG := -mod=vendor -ldflags '$(X_FLAGS)' -tags '$(TAGS)'
66-
67-
.PHONY: build
68-
build: $(shell ls -d cmd/* | sed -e 's/\//./')
69-
70-
cmd.%: CMDNAME=$*
71-
cmd.%:
72-
$(GO_EXEC) env
73-
@echo ''
74-
CGO_ENABLED=0 $(GO_EXEC) build $(BUILD_FLAGS) -o $(BUILD_OUTPUT)/$(CMDNAME) ./cmd/$(CMDNAME)
75-
76-
dbg.%: BUILD_FLAGS=$(BUILD_FLAGS_DEBUG)
77-
dbg.%: cmd.%
78-
@echo "debug binary done"
79-
80-
.PHONY: clean
81-
clean:
82-
rm -rf $(BUILD_OUTPUT)
83-
8416
# man git-clean
8517
.PHONY: git-reset
8618
git-reset:
8719
git reset --hard
8820
git clean -fd
8921

90-
## https://docs.docker.com/reference/cli/docker/buildx/build/
91-
## --output='type=docker'
92-
## --output='type=image,push=true'
93-
## --platform=linux/arm64
94-
## --platform=linux/amd64,linux/arm64,linux/arm/v7
95-
## --platform=local
96-
## --progress='plain'
97-
## make DOCKER_BUILD_PLATFORM=linux/arm64 image
98-
DOCKER_BUILD_PLATFORM ?= local
99-
DOCKER_BUILD_OUTPUT ?= type=docker
100-
.PHONY: image
101-
image:
102-
$(DOCKER_EXEC) buildx build \
103-
--output='type=docker' \
104-
--file='$(CURDIR)/build/docker/Dockerfile' \
105-
--tag='$(IMAGE_NAME):$(IMAGE_TAG)' \
106-
--platform='$(DOCKER_BUILD_PLATFORM)' \
107-
--output='$(DOCKER_BUILD_OUTPUT)' \
108-
.
109-
110-
DOCKER_COMPOSE_EXEC ?= $(DOCKER_EXEC) compose -f $(CURDIR)/deployments/local/docker-compose.yml
111-
112-
.PHONY: compose-up
113-
compose-up:
114-
$(DOCKER_COMPOSE_EXEC) up --force-recreate --build
115-
116-
.PHONY: compose-up-detach
117-
compose-up-detach:
118-
$(DOCKER_COMPOSE_EXEC) up --force-recreate --build --detach
119-
120-
.PHONY: compose-down
121-
compose-down:
122-
$(DOCKER_COMPOSE_EXEC) down --volumes --rmi local --remove-orphans
123-
124-
# If the first target is "compose-exec"
125-
# remove the first argument 'compose-exec' and store the rest in DOCKER_COMPOSE_ARGS
126-
# and ignore the subsequent arguments as make targets.
127-
# (using spaces for indentation)
128-
ifeq (compose-exec,$(firstword $(MAKECMDGOALS)))
129-
DOCKER_COMPOSE_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
130-
$(eval $(DOCKER_COMPOSE_ARGS):;@:)
131-
endif
132-
133-
.PHONY: compose-exec
134-
compose-exec:
135-
$(DOCKER_COMPOSE_EXEC) exec $(DOCKER_COMPOSE_ARGS)
136-
13722
.PHONY: env
13823
env:
13924
@echo "Module: $(MODULE)"
@@ -154,19 +39,9 @@ env:
15439
@echo ">>> Path:"
15540
@echo "$${PATH}" | tr ':' '\n'
15641

157-
.PHONY: test
158-
test:
159-
CGO_ENABLED=1 $(GO_EXEC) test -timeout 60s -race -tags="$(TAGS)" -coverprofile cover.out -covermode atomic ./...
160-
@$(GO_EXEC) tool cover -func cover.out
161-
@rm cover.out
162-
16342
.PHONY: checks
16443
checks: vet staticcheck gofumpt goimports golangci-lint-github-actions
16544

166-
.PHONY: run
167-
run:
168-
$(GO_EXEC) run -mod=vendor ./cmd/goboilerplate
169-
17045
.PHONY: ci-gen-n-format
17146
ci-gen-n-format: goimports gofumpt
17247
./scripts/git-check-dirty
@@ -179,4 +54,3 @@ ci-mod: mod
17954
ci-sh: shfmt
18055
@./scripts/sh-checks
18156
@./scripts/git-check-dirty
182-

docs/makefile_targets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
| `compose-up` | Deploys (rebuilds and recreates) the docker compose file `deployments/compose/docker-compose.yml` in attached mode. This docker compose file is intended for local development.<br>It uses the docker file `build/docker/debug.Dockerfile` and runs `air` target (see below), including hot-reload (rebuild on changes) and debug server.
1515
| `compose-down` | Stops (if started) the containers specified by the docker compose file `deployments/compose/docker-compose.yml` removes containers, cleans up the volumes and delete local docker images.
1616
| `air` | Installs (if needed) [air](https://github.com/air-verse/air) under `TOOLS_BIN` folder (default is `./.tools/bin`) and runs `air -c .air.toml`.<br>Air watches for code file changes and rebuilds the binary according to the configuration `.air.toml`.<br>Current air configuration executes `cmd.goboilerplate` target (on each file change) and then runs the `./build/dlv` that starts the debug server (`dlv exec`) with the produced binary.
17-
| `image` | Builds the docker image using the docker file `./build/docker/Dockerfile`.<br>This docker file is intended to be used as a production image.<br>Image name and tag are specified by `IMAGE_NAME` and `IMAGE_TAG`. Default values can be overwritten during execution (eg `make IMAGE_NAME=myimage IMAGE_TAG=1.0.0 image`).
17+
| `build-image` | Builds the docker image using the docker file `./build/docker/Dockerfile`.<br>This docker file is intended to be used as a production image.<br>Image name and tag are specified by `IMAGE_NAME` and `IMAGE_TAG`. Default values can be overwritten during execution (eg `make IMAGE_NAME=myimage IMAGE_TAG=1.0.0 image`).
1818
| `test` | Runs go [test](https://pkg.go.dev/cmd/go/internal/test) with race conditions and prints cover report.
1919
| `tools` | Installs (if needed) all tools (goimports, staticcheck, gofumpt, etc) under `TOOLS_BIN` folder (default is `./.tools/bin`).
2020
| `checks` | Runs all default checks (`vet`, `staticcheck`, `gofumpt`, `goimports`, `golangci-lint`).

scripts/docker.mk

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
DOCKER_EXEC ?= docker
2+
export DOCKER_EXEC
3+
4+
IMAGE_NAME ?= goboilerplate
5+
IMAGE_TAG ?= latest
6+
7+
## https://docs.docker.com/reference/cli/docker/buildx/build/
8+
## --output='type=docker'
9+
## --output='type=image,push=true'
10+
## --platform=linux/arm64
11+
## --platform=linux/amd64,linux/arm64,linux/arm/v7
12+
## --platform=local
13+
## --progress='plain'
14+
## make DOCKER_BUILD_PLATFORM=linux/arm64 image
15+
DOCKER_BUILD_PLATFORM ?= local
16+
DOCKER_BUILD_OUTPUT ?= type=docker
17+
.PHONY: build-image
18+
build-image:
19+
$(DOCKER_EXEC) buildx build \
20+
--output='type=docker' \
21+
--file='$(CURDIR)/build/docker/Dockerfile' \
22+
--tag='$(IMAGE_NAME):$(IMAGE_TAG)' \
23+
--platform='$(DOCKER_BUILD_PLATFORM)' \
24+
--output='$(DOCKER_BUILD_OUTPUT)' \
25+
.
26+
27+
DOCKER_COMPOSE_EXEC ?= $(DOCKER_EXEC) compose -f $(CURDIR)/deployments/local/docker-compose.yml
28+
29+
.PHONY: compose-up
30+
compose-up:
31+
$(DOCKER_COMPOSE_EXEC) up --force-recreate --build
32+
33+
.PHONY: compose-up-detach
34+
compose-up-detach:
35+
$(DOCKER_COMPOSE_EXEC) up --force-recreate --build --detach
36+
37+
.PHONY: compose-down
38+
compose-down:
39+
$(DOCKER_COMPOSE_EXEC) down --volumes --rmi local --remove-orphans
40+
41+
# If the first target is "compose-exec"
42+
# remove the first argument 'compose-exec' and store the rest in DOCKER_COMPOSE_ARGS
43+
# and ignore the subsequent arguments as make targets.
44+
# (using spaces for indentation)
45+
ifeq (compose-exec,$(firstword $(MAKECMDGOALS)))
46+
DOCKER_COMPOSE_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
47+
$(eval $(DOCKER_COMPOSE_ARGS):;@:)
48+
endif
49+
50+
.PHONY: compose-exec
51+
compose-exec:
52+
$(DOCKER_COMPOSE_EXEC) exec $(DOCKER_COMPOSE_ARGS)

scripts/go.mk

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
GO_EXEC ?= go
2+
export GO_EXEC
3+
4+
MODULE := $(shell cat go.mod | grep -e "^module" | sed "s/^module //")
5+
VERSION ?= 0.0.0
6+
X_FLAGS = \
7+
-X '$(MODULE)/build.Version=$(VERSION)' \
8+
-X '$(MODULE)/build.Branch=$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || true)' \
9+
-X '$(MODULE)/build.Commit=$(shell git rev-parse HEAD 2>/dev/null || true)' \
10+
-X '$(MODULE)/build.CommitShort=$(shell git rev-parse --short HEAD 2>/dev/null || true)' \
11+
-X '$(MODULE)/build.Tag=$(shell git describe --tags 2>/dev/null || true)'
12+
13+
GO_PACKAGES = $(GO_EXEC) list -tags='$(TAGS)' -mod=vendor ./...
14+
GO_FOLDERS = $(GO_EXEC) list -tags='$(TAGS)' -mod=vendor -f '{{ .Dir }}' ./...
15+
GO_FILES = find . -type f -name '*.go' -not -path './vendor/*'
16+
17+
export GO111MODULE := on
18+
#export GOFLAGS := -mod=vendor
19+
GOPATH := $(shell go env GOPATH)
20+
GO_VER := $(shell go env GOVERSION)
21+
BUILD_OUTPUT ?= $(CURDIR)/output
22+
23+
24+
.PHONY: mod
25+
mod:
26+
$(GO_EXEC) mod tidy -go=1.23
27+
$(GO_EXEC) mod verify
28+
29+
.PHONY: vendor
30+
vendor:
31+
$(GO_EXEC) mod vendor
32+
33+
# https://go.dev/ref/mod#go-get
34+
# -u flag tells go get to upgrade modules
35+
# -t flag tells go get to consider modules needed to build tests of packages named on the command line.
36+
# When -t and -u are used together, go get will update test dependencies as well.
37+
.PHONY: go-deps-upgrade
38+
go-deps-upgrade:
39+
$(GO_EXEC) get -u -t ./...
40+
$(GO_EXEC) mod tidy -go=1.23
41+
$(GO_EXEC) mod vendor
42+
43+
# https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies
44+
# https://pkg.go.dev/cmd/compile
45+
# https://pkg.go.dev/cmd/link
46+
47+
#BUILD_FLAGS := -mod=vendor -a -ldflags "-s -w $(X_FLAGS) -extldflags='-static'" -tags '$(TAGS)'
48+
BUILD_FLAGS := -mod=vendor -a -ldflags '-s -w $(X_FLAGS)' -tags '$(TAGS)'
49+
BUILD_FLAGS_DEBUG := -mod=vendor -ldflags '$(X_FLAGS)' -tags '$(TAGS)'
50+
51+
cmd.%: CMDNAME=$*
52+
cmd.%:
53+
$(GO_EXEC) env
54+
@echo ''
55+
CGO_ENABLED=0 $(GO_EXEC) build $(BUILD_FLAGS) -o $(BUILD_OUTPUT)/$(CMDNAME) ./cmd/$(CMDNAME)
56+
57+
dbg.%: BUILD_FLAGS=$(BUILD_FLAGS_DEBUG)
58+
dbg.%: cmd.%
59+
@echo "debug binary done"
60+
61+
.PHONY: build
62+
build: $(shell ls -d cmd/* | sed -e 's/\//./')
63+
64+
.PHONY: clean
65+
clean:
66+
rm -rf $(BUILD_OUTPUT)
67+
68+
.PHONY: test
69+
test:
70+
CGO_ENABLED=1 $(GO_EXEC) test -timeout 60s -race -tags="$(TAGS)" -coverprofile cover.out -covermode atomic ./...
71+
@$(GO_EXEC) tool cover -func cover.out
72+
@rm cover.out
73+
74+
.PHONY: run
75+
run:
76+
$(GO_EXEC) run -mod=vendor ./cmd/goboilerplate

0 commit comments

Comments
 (0)