-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
118 lines (92 loc) · 4.4 KB
/
Makefile
File metadata and controls
118 lines (92 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
.PHONY: clean run build install dep test lint format docker gqlgen podman
SHELL := /bin/bash
PATHINSTBIN = $(abspath ./bin)
SCRIPTS_DIR = $(abspath ./scripts)
export PATH := $(PATHINSTBIN):$(SCRIPTS_DIR):$(PATH)
BIN_NAME ?= fetch-api
DEFAULT_INSTALL_DIR := $(go env GOPATH)/$(PATHINSTBIN)
DEFAULT_ARCH := $(shell go env GOARCH)
DEFAULT_GOOS := $(shell go env GOOS)
ARCH ?= $(DEFAULT_ARCH)
GOOS ?= $(DEFAULT_GOOS)
INSTALL_DIR ?= $(DEFAULT_INSTALL_DIR)
.DEFAULT_GOAL := run
VERSION := $(shell git describe --tags || echo "v0.0.0")
VER_CUT := $(shell echo $(VERSION) | cut -c2-)
# Dependency versions
GOLANGCI_VERSION = latest
PROTOC_VERSION = 33.4
PROTOC_GEN_GO_VERSION = $(shell go list -m -f '{{.Version}}' google.golang.org/protobuf)
PROTOC_GEN_GO_GRPC_VERSION = v1.5.1
help:
@echo "\nSpecify a subcommand:\n"
@grep -hE '^[0-9a-zA-Z_-]+:.*?## .*$$' ${MAKEFILE_LIST} | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[0;36m%-20s\033[m %s\n", $$1, $$2}'
@echo ""
build: ## build the binary
@CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(ARCH) \
go build -o $(PATHINSTBIN)/$(BIN_NAME) ./cmd/$(BIN_NAME)
run: build ## run the binary
@$(PATHINSTBIN)/$(BIN_NAME)
all: clean target
clean: ## clean the binary
@rm -rf $(PATHINSTBIN)
install: build ## install the binary
@install -d $(INSTALL_DIR)
@rm -f $(INSTALL_DIR)/$(BIN_NAME)
@cp $(PATHINSTBIN)/$(BIN_NAME) $(INSTALL_DIR)/$(BIN_NAME)
tidy: ## tidy the go mod
@go mod tidy
test: ## run tests
@go test ./...
lint: ## run linter
@PATH=$$PATH golangci-lint run --timeout 10m
docker: dep ## build docker image
@docker build --build-arg APP_NAME=$(BIN_NAME) -f ./Dockerfile . -t dimozone/$(BIN_NAME):$(VER_CUT)
@docker tag dimozone/$(BIN_NAME):$(VER_CUT) dimozone/$(BIN_NAME):latest
# Build multi-arch (amd64 + arm64) and push with a random tag. Does not trigger GitHub workflows.
# Requires: docker buildx, docker login. Run from repo root.
docker-push-multiarch:
$(eval TAG := dev-$(shell openssl rand -hex 6))
@echo "Building and pushing dimozone/$(BIN_NAME):$(TAG) (linux/amd64, linux/arm64)"
@docker buildx build --build-arg APP_NAME=$(BIN_NAME) --platform linux/amd64,linux/arm64 -f ./Dockerfile --push -t dimozone/$(BIN_NAME):$(TAG) .
@echo "Pushed dimozone/$(BIN_NAME):$(TAG)"
# Same as docker-push-multiarch but using podman (manifest list + manifest push --all).
podman-push-multiarch:
$(eval TAG := dev-$(shell openssl rand -hex 6))
$(eval IMAGE := dimozone/$(BIN_NAME):$(TAG))
@echo "Building and pushing $(IMAGE) (linux/amd64, linux/arm64)"
@podman build --build-arg APP_NAME=$(BIN_NAME) --platform linux/amd64,linux/arm64 --manifest $(IMAGE) -f ./Dockerfile .
@podman manifest push --all $(IMAGE) docker://$(IMAGE)
@echo "Pushed $(IMAGE)"
tools-golangci-lint: ## install golangci-lint
@mkdir -p $(PATHINSTBIN)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- ${GOLANGCI_VERSION}
tools-protoc: ## install protoc
@mkdir -p $(PATHINSTBIN)
rm -rf $(PATHINSTBIN)/protoc
ifeq ($(shell uname | tr A-Z a-z), darwin)
curl -L https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-osx-x86_64.zip > bin/protoc.zip
endif
ifeq ($(shell uname | tr A-Z a-z), linux)
curl -L https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip > bin/protoc.zip
endif
unzip -o $(PATHINSTBIN)/protoc.zip -d $(PATHINSTBIN)/protoclib
mv -f $(PATHINSTBIN)/protoclib/bin/protoc $(PATHINSTBIN)/protoc
rm -rf $(PATHINSTBIN)/include
mv $(PATHINSTBIN)/protoclib/include $(PATHINSTBIN)/
rm $(PATHINSTBIN)/protoc.zip
tools-protoc-plugins: ## install protoc-gen-go and protoc-gen-go-grpc from go.mod tool directive
@mkdir -p $(PATHINSTBIN)
GOBIN=$(PATHINSTBIN) go install google.golang.org/protobuf/cmd/protoc-gen-go
GOBIN=$(PATHINSTBIN) go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
make tools: tools-golangci-lint tools-protoc tools-protoc-plugins ## install all tools
gqlgen: ## Generate gqlgen code.
@go tool gqlgen generate
generate: gqlgen generate-go generate-grpc ## run all file generation for the project
generate-go:## run go generate
@go generate ./...
generate-grpc: ## generate grpc files
@PATH=$$PATH protoc --version
@PATH=$$PATH protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
pkg/grpc/*.proto