Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created March 18, 2018 00:18
Show Gist options
  • Save vinicius73/6aefa4291fe52f2870cb24712c959b96 to your computer and use it in GitHub Desktop.
Save vinicius73/6aefa4291fe52f2870cb24712c959b96 to your computer and use it in GitHub Desktop.
GoLang Makefile project
GOBIN=$(shell pwd)/bin
PROD_VERSION=$(shell cat ./VERSION)
BUILD_DIR=$(shell pwd)
SRC_DIR=$(BUILD_DIR)/src
GONAME=$(shell basename "$(PWD)")
BINFILE=${GOBIN}/${GONAME}
GOFILES=.
PID=/tmp/go-$(GONAME).pid
GOARCH = amd64
# Enviroment
PROD?=?
ifeq ($(PROD),1)
VERSION=$(PROD_VERSION)
else
VERSION=$(PROD_VERSION)-dev
endif
COMMIT=$(shell git rev-parse HEAD)
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS = -ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.branch=${BRANCH}"
build:
@echo "Building $(GONAME) to ./bin"
@echo $(shell go version)
- go build ${LDFLAGS} -o $(BINFILE) ${GOFILES}
get:
- dep ensure -update
# watch:
# @$(MAKE) restart &
# @fswatch -o '$(SRC_DIR)' -e 'bin/.*' -m inotify_monitor -v | xargs -n1 -I{} make restart
restart: stop clean build start
serve: stop clean build
$(BINFILE)
start:
@echo "Starting $(BINFILE)"
$(BINFILE) & echo $$! > $(PID)
stop:
@echo "Stopping $(BINFILE) if it's running"
@-kill `[[ -f $(PID) ]] && cat $(PID)` 2>/dev/null || true
clear:
@clear
clean:
@echo "Cleaning"
rm -f $(BINFILE)
.PHONY: build get watch start stop restart clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment