github.com/openfga/openfga@v1.5.4-rc1/Makefile (about)

     1  #-----------------------------------------------------------------------------------------------------------------------
     2  # Variables (https://www.gnu.org/software/make/manual/html_node/Using-Variables.html#Using-Variables)
     3  #-----------------------------------------------------------------------------------------------------------------------
     4  .DEFAULT_GOAL := help
     5  
     6  BINARY_NAME = openfga
     7  BUILD_DIR ?= $(CURDIR)/dist
     8  GO_BIN ?= $(shell go env GOPATH)/bin
     9  GO_PACKAGES := $(shell go list ./... | grep -vE "vendor")
    10  
    11  DATASTORE ?= "in-memory"
    12  
    13  # Colors for the printf
    14  RESET = $(shell tput sgr0)
    15  COLOR_WHITE = $(shell tput setaf 7)
    16  COLOR_BLUE = $(shell tput setaf 4)
    17  TEXT_ENABLE_STANDOUT = $(shell tput smso)
    18  TEXT_DISABLE_STANDOUT = $(shell tput rmso)
    19  
    20  #-----------------------------------------------------------------------------------------------------------------------
    21  # Rules (https://www.gnu.org/software/make/manual/html_node/Rule-Introduction.html#Rule-Introduction)
    22  #-----------------------------------------------------------------------------------------------------------------------
    23  .PHONY: help clean
    24  
    25  help: ## Show this help
    26  	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
    27  
    28  clean: ## Clean project files
    29  	${call print, "Removing ${BUILD_DIR}/${BINARY_NAME}"}
    30  	@rm "${BUILD_DIR}/${BINARY_NAME}"
    31  	@go clean -x -r -i
    32  
    33  #-----------------------------------------------------------------------------------------------------------------------
    34  # Dependencies
    35  #-----------------------------------------------------------------------------------------------------------------------
    36  .PHONY: deps
    37  
    38  deps: ## Download dependencies
    39  	${call print, "Downloading dependencies"}
    40  	@go mod vendor && go mod tidy
    41  
    42  $(GO_BIN)/golangci-lint:
    43  	${call print, "Installing golangci-lint within ${GO_BIN}"}
    44  	@go install -v github.com/golangci/golangci-lint/cmd/golangci-lint@latest
    45  
    46  $(GO_BIN)/mockgen:
    47  	${call print, "Installing mockgen within ${GO_BIN}"}
    48  	@go install -v go.uber.org/mock/mockgen@latest
    49  
    50  $(GO_BIN)/CompileDaemon:
    51  	${call print, "Installing CompileDaemon within ${GO_BIN}"}
    52  	@go install -v github.com/githubnemo/CompileDaemon@latest
    53  
    54  $(GO_BIN)/openfga: install
    55  
    56  generate-mocks: $(GO_BIN)/mockgen ## Generate mock stubs
    57  	${call print, "Generating mock stubs"}
    58  	@go generate ./...
    59  
    60  #-----------------------------------------------------------------------------------------------------------------------
    61  # Building & Installing
    62  #-----------------------------------------------------------------------------------------------------------------------
    63  .PHONY: build install
    64  
    65  build: ## Build the OpenFGA service binary. Build directory can be overridden using BUILD_DIR="desired/path", default is ".dist/". Usage `BUILD_DIR="." make build`
    66  	${call print, "Building the OpenFGA binary within ${BUILD_DIR}/${BINARY_NAME}"}
    67  	@go build -v -o "${BUILD_DIR}/${BINARY_NAME}" "$(CURDIR)/cmd/openfga"
    68  
    69  install: ## Install the OpenFGA service within $GO_BIN. Ensure that $GO_BIN is available on the $PATH to run the executable from anywhere
    70  	${call print, "Installing the OpenFGA binary within ${GO_BIN}"}
    71  	@go install -v "$(CURDIR)/cmd/${BINARY_NAME}"
    72  
    73  #-----------------------------------------------------------------------------------------------------------------------
    74  # Checks
    75  #-----------------------------------------------------------------------------------------------------------------------
    76  .PHONY: lint
    77  
    78  lint: $(GO_BIN)/golangci-lint ## Lint Go source files
    79  	${call print, "Linting Go source files"}
    80  	@golangci-lint run -v --fix -c .golangci.yaml ./...
    81  
    82  #-----------------------------------------------------------------------------------------------------------------------
    83  # Tests
    84  #-----------------------------------------------------------------------------------------------------------------------
    85  .PHONY: test test-docker test-bench generate-mocks
    86  
    87  test: generate-mocks ## Run all tests. To run a specific test, pass the FILTER var. Usage `make test FILTER="TestCheckLogs"`
    88  	${call print, "Running tests"}
    89  	@go test -race \
    90  			-run "$(FILTER)" \
    91  			-coverpkg=./... \
    92  			-coverprofile=coverageunit.tmp.out \
    93  			-covermode=atomic \
    94  			-count=1 \
    95  			-timeout=10m \
    96  			${GO_PACKAGES}
    97  	@cat coverageunit.tmp.out | grep -v "mocks" > coverageunit.out
    98  	@rm coverageunit.tmp.out
    99  
   100  test-docker: ## Run tests requiring Docker
   101  	${call print, "Running docker tests"}
   102  	@if [ -z "$${CI}" ]; then \
   103  		docker build -t="openfga/openfga:dockertest" .; \
   104  	fi
   105  	@go test -v -count=1 -timeout=5m -tags=docker ./cmd/openfga/...
   106  
   107  test-bench: generate-mocks ## Run benchmark tests. See https://pkg.go.dev/cmd/go#hdr-Testing_flags
   108  	${call print, "Running benchmark tests"}
   109  	@go test ./... -bench . -benchtime 5s -timeout 0 -run=XXX -cpu 1 -benchmem
   110  
   111  #-----------------------------------------------------------------------------------------------------------------------
   112  # Development
   113  #-----------------------------------------------------------------------------------------------------------------------
   114  .PHONY: dev-run
   115  
   116  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`, default is "in-memory". Usage `DATASTORE="mysql" make dev-run`
   117  	${call print, "Starting OpenFGA server"}
   118  	@case "${DATASTORE}" in \
   119  		"in-memory") \
   120  			echo "==> Running OpenFGA with In-Memory data storage"; \
   121  			CompileDaemon -graceful-kill -build='make install' -command='openfga run'; \
   122  			break; \
   123  			;; \
   124  		"mysql") \
   125  			echo "==> Running OpenFGA with MySQL data storage"; \
   126  			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; \
   127  			sleep 2; \
   128  			openfga migrate --datastore-engine mysql --datastore-uri 'root:secret@tcp(localhost:3306)/openfga?parseTime=true'; \
   129  			CompileDaemon -graceful-kill -build='make install' -command="openfga run --datastore-engine mysql --datastore-uri root:secret@tcp(localhost:3306)/openfga?parseTime=true"; \
   130  			break; \
   131  			;; \
   132  		"postgres") \
   133  			echo "==> Running OpenFGA with Postgres data storage"; \
   134  			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; \
   135  			sleep 2; \
   136  			openfga migrate --datastore-engine postgres --datastore-uri 'postgres://postgres:password@localhost:5432/postgres'; \
   137  			CompileDaemon -graceful-kill -build='make install' -command="openfga run --datastore-engine postgres --datastore-uri postgres://postgres:password@localhost:5432/postgres"; \
   138  			break; \
   139  			;; \
   140  		*) \
   141  			echo "Invalid option. Try again."; \
   142  			;; \
   143  	esac; \
   144  
   145  #-----------------------------------------------------------------------------------------------------------------------
   146  # Helpers
   147  #-----------------------------------------------------------------------------------------------------------------------
   148  define print
   149  	@printf "${TEXT_ENABLE_STANDOUT}${COLOR_WHITE} 🚀 ${COLOR_BLUE} %-70s ${COLOR_WHITE} ${TEXT_DISABLE_STANDOUT}\n" $(1)
   150  endef