Skip to content

Instantly share code, notes, and snippets.

@sko00o
Last active June 12, 2020 07:42
Show Gist options
  • Save sko00o/2f1d5ce9595dcead03a65b927158eb8f to your computer and use it in GitHub Desktop.
Save sko00o/2f1d5ce9595dcead03a65b927158eb8f to your computer and use it in GitHub Desktop.
[MyGoProjectTemplate] #Go
PROJPATH := .
PROJECTNAME := demo
PROJECTVER := 0.0.1
FILES := README.md CHANGELOG.md
GO_ENV := CGO_ENABLED=0 GOPROXY=https://goproxy.io
GO_EXTRA_BUILD_ARGS := -trimpath

demo

some description.

Change Log

Product Name: demo (full project name)

Copyright: company name


v0.0.2 2020/01/01

  • feature: something.
  • bugfix: somthing.

v0.0.1 2019/12/31

  • init.
package main
import (
"time"
)
// set by the compiler
var timestamp, version, revision string
func main() {
if timestamp == "" {
timestamp = time.Now().String()
}
if revision == "" {
revision = "unknown"
}
if version == "" {
version = "dev"
}
// do your process.
}
include .env
# Go related variables.
GOBASE := $(shell pwd)
GOBIN := $(GOBASE)/build
PUBDIR := $(GOBASE)/dist
TESTOUT := $(GOBASE)/coverage.out
TIMESTAMP := "$(shell date --rfc-3339='seconds')"
PKGS := $(shell go list ./... | grep -v /vendor)
ifeq (,$(wildcard .git))
REVER := $(shell svnversion -cn | sed -e "s/.*://" -e "s/\([0-9]*\).*/\1/" | grep "[0-9]")
else
REVER := $(shell git describe --always | sed -e "s/^v//")
endif
PROJECTBASE := $(GOBASE)/$(PROJPATH)
PROJECTNAME ?= $(shell basename "$(PROJECTBASE)")
OUTFILE := "$(PROJECTNAME)_$(shell go env GOARCH)_v$(PROJECTVER)-$(shell date +%Y%m%d%H%M%S)"
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent
.PHONY: all help
all: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
## build: compiling from source
.PHONY: build
build: generate
@echo ┌ compiling from source
mkdir -p $(GOBIN)
$(GO_ENV) go build $(GO_EXTRA_BUILD_ARGS) \
-ldflags " \
-X 'main.timestamp="$(TIMESTAMP)"' \
-X 'main.version=$(PROJECTVER)' \
-X 'main.revision=$(REVER)' \
" \
-o $(GOBIN)/$(PROJECTNAME) \
$(PROJECTBASE)/main.go
@echo └ done
## compress: compressing for release
.PHONY: compress
compress: build
@echo ┌ compressing for release
mkdir -p $(PUBDIR)
tar czf $(PUBDIR)/$(OUTFILE).tar.gz \
-C $(GOBIN) $(PROJECTNAME) \
-C $(GOBASE) $(FILES)
@echo └ done
## clean: cleanup workspace
.PHONY: clean
clean:
@echo ┌ cleanup workspace
@rm -rf $(GOBIN)
@rm -rf $(PUBDIR)
@rm -f $(TESTOUT)
@echo └ done
## test: running tests
.PHONY: test
test: lint
@echo ┌ running tests
@rm -f $(TESTOUT)
@go test -p 1 -v \
-cover $(PKGS) \
-coverprofile $(TESTOUT)
@echo └ done
## lint: running code inspection
.PHONY: lint
lint:
@echo ┌ running code inspection
@for pkg in $(PKGS) ; do \
golint $$pkg ; \
done
@go vet $(PKGS)
@echo └ done
.PHONY: generate
generate:
go generate config/config.go
# shortcuts for development
.PHONY: requirements
requirements:
@echo ┌ setup development requirements
go install golang.org/x/lint/golint
@echo └ done
.PHONY: serve
serve: build
@echo start $(PROJECTNAME)
$(GOBIN)/$(PROJECTNAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment