Skip to content

Instantly share code, notes, and snippets.

@kkumar326
Last active April 19, 2022 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkumar326/696e4f507e78d034f308c370fa44f5c7 to your computer and use it in GitHub Desktop.
Save kkumar326/696e4f507e78d034f308c370fa44f5c7 to your computer and use it in GitHub Desktop.
Go Makefile
SERVICE_NAME := global-search-infrastructure
BINARY_NAME := app
ENV := prod dev local
OS = darwin linux windows
ARCHS = 386 amd64
.DEFAULT_GOAL := help
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)
HASH := $(shell git rev-parse --short HEAD)
COMMIT_DATE := $(shell git show -s --format=%ci ${HASH})
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S')
VERSION := ${HASH} (${COMMIT_DATE})
FILES := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
## Build Commands
build: ## builds for current OS and architecture
go build -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}' -X 'main.branch=${BRANCH}'" -o bin/${BINARY_NAME} main.go
build-all: ## builds for all the available OS and architectures
mkdir -p bin
@for arch in $(ARCHS);\
do \
for os in $(OS);\
do \
CGO_ENABLED=0 GOARCH=$$arch GOOS=$$os go build -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}' -X 'main.branch=${BRANCH}'" -o bin/${BINARY_NAME}-$$os-$$arch main.go; \
done \
done
## Setup Commands
install-dev: ## gets the installation dependencies
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/swaggo/swag/cmd/swag@latest
go install golang.org/x/tools/cmd/goimports@latest
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
dep: ## installs all the missing dependencies
go get -v -d ./...
init: ## initializes for dev/ local setup
install-dev dep
## Docs and Swagger Commands
doc: ## generates swagger document
swag init
swagger-ui: ## run a local swagger-ui to view the generated swagger docs
sudo docker run -p 8000:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/foo/swagger.yaml -v `pwd`/docs:/foo swaggerapi/swagger-ui
## Code, Test and Formatting Commands
fmt: ## format the go source files
@gofmt -l -s -w .
goimports -w $(FILES)
.PHONY: test
test: ## runs test throughout the code
go test ./...
test-out: ## outputs test result as json into a file
mkdir -p report
go test -json ./... | tee report/test.out
test-with-benchmark: ## runs tests including benchamark tests
go test -bench=. ./...
test-with-benchmark-out: ## outputs tests including benchmark tests into a file
mkdir -p report
go test -json -bench=. ./... | tee report/test-with-benchmark.out
test-with-race: ## runs tests including race conditions
go test -race ./...
test-with-race-out: ## outputs tests including race conditions into a file
mkdir -p report
go test -json -race ./... | tee report/test-with-race.out
coverage: ## outputs the code coverage by tests
go test ./... -json -cover
coverage-out: ## outputs code coverage as json into a file
mkdir -p report
go test ./... -json -cover -coverprofile=report/coverage.out
coverage-html-out: ## outputs code coverage as html
go tool cover -html=report/coverage.out
vet: ## reports suspicious constructs
go vet ./...
vet-out: ## outputs vet output into a file
mkdir -p report
go vet ./... 2>&- | tee report/govet.out
lint: ## outputs issues found by linter
golangci-lint run
lint-out-json: ## outputs linting issues as json into a file
golangci-lint run --out-format "json" 2>&- | tee report/linter.json
lint-out-html: ## outputs linting issues into a html file
golangci-lint run --out-format "html" 2>&- | tee report/linter.html
## Run Commands
# refer: https://gobyexample.com/command-line-flags
run: ## runs the application, use "make run env=dev"
ifneq ($(filter $(env),$(ENV)),)
./bin/${BINARY_NAME} -env=$(env)
else
echo "invalid environment!"
endif
## Cleanup Commands
clean: ## cleans and removes temporary objects and binary builds
@go clean
@rm -rf bin/*
@rm -rf report/*
@echo "cleaning completed!\n"
## Docker Commands
IMAGE_NAME := image-${SERVICE_NAME}
CONTAINER_NAME := container-${SERVICE_NAME}
build-docker-image: ## builds docker image
sudo docker build --rm --no-cache -t ${IMAGE_NAME}:latest .
remove-docker-image: ## removes docekr image
sudo docker image rm ${IMAGE_NAME}:latest
run-docker-container: ## starts docker container from image
sudo docker run --rm --name ${CONTAINER_NAME} -d -p 8080:8080 ${IMAGE_NAME}:latest
stop-docker-container: ## stops docker container
sudo docker stop ${CONTAINER_NAME}
remove-docker-container: ## removes docker container
sudo docker rm ${CONTAINER_NAME}
restart-docker-container: ## restarts docker container
sudo docker restart ${CONTAINER_NAME}
# change shell according to the container os
docker-container-shell: ## shell access to running container
sudo docker exec -it ${CONTAINER_NAME} /bin/sh
## Help Commands
.PHONY: help
help: ## shows this help
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-30s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment