forked from openfga/openfga
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
158 lines (133 loc) · 7.58 KB
/
Makefile
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#-----------------------------------------------------------------------------------------------------------------------
# Variables (https://www.gnu.org/software/make/manual/html_node/Using-Variables.html#Using-Variables)
#-----------------------------------------------------------------------------------------------------------------------
.DEFAULT_GOAL := help
BINARY_NAME = openfga
BUILD_DIR ?= $(CURDIR)/dist
GO_BIN ?= $(shell go env GOPATH)/bin
GO_PACKAGES := $(shell go list ./... | grep -vE "vendor")
DATASTORE ?= "in-memory"
# Colors for the printf
RESET = $(shell tput sgr0)
COLOR_WHITE = $(shell tput setaf 7)
COLOR_BLUE = $(shell tput setaf 4)
TEXT_ENABLE_STANDOUT = $(shell tput smso)
TEXT_DISABLE_STANDOUT = $(shell tput rmso)
#-----------------------------------------------------------------------------------------------------------------------
# Rules (https://www.gnu.org/software/make/manual/html_node/Rule-Introduction.html#Rule-Introduction)
#-----------------------------------------------------------------------------------------------------------------------
.PHONY: help clean
help: ## Show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
clean: ## Clean project files
${call print, "Removing ${BUILD_DIR}/${BINARY_NAME}"}
@rm "${BUILD_DIR}/${BINARY_NAME}"
@go clean -x -r -i
#-----------------------------------------------------------------------------------------------------------------------
# Dependencies
#-----------------------------------------------------------------------------------------------------------------------
.PHONY: deps
deps: ## Download dependencies
${call print, "Downloading dependencies"}
@go mod vendor && go mod tidy
$(GO_BIN)/golangci-lint:
${call print, "Installing golangci-lint within ${GO_BIN}"}
@go install -v github.com/golangci/golangci-lint/cmd/golangci-lint@latest
$(GO_BIN)/mockgen:
${call print, "Installing mockgen within ${GO_BIN}"}
@go install -v go.uber.org/mock/mockgen@latest
$(GO_BIN)/CompileDaemon:
${call print, "Installing CompileDaemon within ${GO_BIN}"}
@go install -v github.com/githubnemo/CompileDaemon@latest
$(GO_BIN)/openfga: install
generate-mocks: $(GO_BIN)/mockgen ## Generate mock stubs
${call print, "Generating mock stubs"}
@go generate ./...
#-----------------------------------------------------------------------------------------------------------------------
# Building & Installing
#-----------------------------------------------------------------------------------------------------------------------
.PHONY: build install
build: ## Build the OpenFGA service binary. Build directory can be overridden using BUILD_DIR="desired/path", default is ".dist/". Usage `BUILD_DIR="." make build`
${call print, "Building the OpenFGA binary within ${BUILD_DIR}/${BINARY_NAME}"}
@go build -v -o "${BUILD_DIR}/${BINARY_NAME}" "$(CURDIR)/cmd/openfga"
install: ## Install the OpenFGA service within $GO_BIN. Ensure that $GO_BIN is available on the $PATH to run the executable from anywhere
${call print, "Installing the OpenFGA binary within ${GO_BIN}"}
@go install -v "$(CURDIR)/cmd/${BINARY_NAME}"
#-----------------------------------------------------------------------------------------------------------------------
# Checks
#-----------------------------------------------------------------------------------------------------------------------
.PHONY: lint
lint: $(GO_BIN)/golangci-lint ## Lint Go source files
${call print, "Linting Go source files"}
@golangci-lint run -v --fix -c .golangci.yaml ./...
#-----------------------------------------------------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------------------------------------------------
.PHONY: test test-docker test-bench generate-mocks
test: generate-mocks ## Run all tests. To run a specific test, pass the FILTER var. Usage `make test FILTER="TestCheckLogs"`
${call print, "Running tests"}
@go test -race \
-run "$(FILTER)" \
-coverpkg=./... \
-coverprofile=coverageunit.tmp.out \
-covermode=atomic \
-count=1 \
-timeout=10m \
${GO_PACKAGES}
@cat coverageunit.tmp.out | grep -v "mock" > coverageunit.out
@rm coverageunit.tmp.out
test-docker: ## Run tests requiring Docker
${call print, "Running docker tests"}
@if [ -z "$${CI}" ]; then \
docker build -t="openfga/openfga:dockertest" .; \
fi
@go test -v -count=1 -timeout=5m -tags=docker ./cmd/openfga/...
test-bench: generate-mocks ## Run benchmark tests. See https://pkg.go.dev/cmd/go#hdr-Testing_flags
${call print, "Running benchmark tests"}
@go test ./... -bench . -benchtime 5s -timeout 0 -run=XXX -cpu 1 -benchmem
#-----------------------------------------------------------------------------------------------------------------------
# Development
#-----------------------------------------------------------------------------------------------------------------------
.PHONY: dev-run
dev-run: $(GO_BIN)/CompileDaemon $(GO_BIN)/openfga ## Run the OpenFGA server with hot reloading. Data storage type can be overridden using DATASTORE="mysql", available options are `in-memory`, `mysql`, ´postgres`, ´mssql`, default is "in-memory". Usage `DATASTORE="mysql" make dev-run`
${call print, "Starting OpenFGA server"}
@case "${DATASTORE}" in \
"in-memory") \
echo "==> Running OpenFGA with In-Memory data storage"; \
CompileDaemon -graceful-kill -build='make install' -command='openfga run'; \
break; \
;; \
"mysql") \
echo "==> Running OpenFGA with MySQL data storage"; \
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=openfga mysql:8 > /dev/null 2>&1 || docker start mysql; \
sleep 2; \
openfga migrate --datastore-engine mysql --datastore-uri 'root:secret@tcp(localhost:3306)/openfga?parseTime=true'; \
CompileDaemon -graceful-kill -build='make install' -command="openfga run --datastore-engine mysql --datastore-uri root:secret@tcp(localhost:3306)/openfga?parseTime=true"; \
break; \
;; \
"postgres") \
echo "==> Running OpenFGA with Postgres data storage"; \
docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password postgres:14 > /dev/null 2>&1 || docker start postgres; \
sleep 2; \
openfga migrate --datastore-engine postgres --datastore-uri 'postgres://postgres:password@localhost:5432/postgres'; \
CompileDaemon -graceful-kill -build='make install' -command="openfga run --datastore-engine postgres --datastore-uri postgres://postgres:password@localhost:5432/postgres"; \
break; \
;; \
"mssql") \
echo "==> Running OpenFGA with MSSQL data storage"; \
docker run -d --name mssql -p 1433:1433 -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=pKC8mMA_qu5SLeaG" mcr.microsoft.com/mssql/server:2022-latest > /dev/null 2>&1 || docker start mssql; \
sleep 2; \
openfga migrate --datastore-engine mssql --datastore-uri 'sqlserver://sa:pKC8mMA_qu5SLeaG@localhost:1433'; \
CompileDaemon -graceful-kill -build='make install' -command="openfga run --datastore-engine mssql --datastore-uri sqlserver://sa:pKC8mMA_qu5SLeaG@localhost:1433"; \
break; \
;; \
*) \
echo "Invalid option. Try again."; \
;; \
esac; \
#-----------------------------------------------------------------------------------------------------------------------
# Helpers
#-----------------------------------------------------------------------------------------------------------------------
define print
@printf "${TEXT_ENABLE_STANDOUT}${COLOR_WHITE} 🚀 ${COLOR_BLUE} %-70s ${COLOR_WHITE} ${TEXT_DISABLE_STANDOUT}\n" $(1)
endef