-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for version and add Makefile
Signed-off-by: Ashok Pon Kumar <ashokponkumar@gmail.com>
- Loading branch information
1 parent
2cc1643
commit 20a212b
Showing
6 changed files
with
279 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
# Copyright 2022 Ashok Pon Kumar | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
.vscode | ||
demo_tmp | ||
sample_tmp | ||
work_dir | ||
m2k | ||
m2k_bin | ||
colleted_md | ||
.DS_Store | ||
_dist | ||
bin | ||
.m2k | ||
coverage.txt | ||
.jekyll-cache/ | ||
_site/ | ||
node_modules | ||
target/ | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
# brew bottling creates .brew_home directory | ||
# add this to avoid git tree start becoming dirty | ||
.brew_home/ | ||
|
||
__pycache__/ | ||
*.pyc | ||
.bundle/ | ||
vendor/ | ||
.ruby-version | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# Copyright 2022 Ashok Pon Kumar | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
BINNAME ?= github-org-stats | ||
ORGNAME ?= ashokponkumar | ||
BINDIR := $(CURDIR)/bin | ||
DISTDIR := $(CURDIR)/_dist | ||
TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64 | ||
|
||
GOPATH = $(shell go env GOPATH) | ||
GOX = $(GOPATH)/bin/gox | ||
GOTEST = ${GOPATH}/bin/gotest | ||
GOLANGCILINT = $(GOPATH)/bin/golangci-lint | ||
GOLANGCOVER = $(GOPATH)/bin/goveralls | ||
|
||
PKG := ./... | ||
LDFLAGS := -w -s | ||
|
||
SRC = $(shell find . -type f -name '*.go' -print) | ||
ASSETS = $(shell find assets -type f -name '*' -print) | ||
ARCH = $(shell uname -p) | ||
GIT_COMMIT = $(shell git rev-parse HEAD) | ||
GIT_SHA = $(shell git rev-parse --short HEAD) | ||
GIT_TAG = $(shell git tag --points-at | tail -n 1) | ||
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean") | ||
HAS_UPX = $(shell command -v upx >/dev/null && echo true || echo false) | ||
|
||
GOGET := cd / && GO111MODULE=on go install | ||
|
||
ifdef VERSION | ||
BINARY_VERSION = $(VERSION) | ||
endif | ||
BINARY_VERSION ?= ${GIT_TAG} | ||
ifneq ($(BINARY_VERSION),) | ||
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/info.version=${BINARY_VERSION} | ||
VERSION ?= $(BINARY_VERSION) | ||
endif | ||
VERSION ?= latest | ||
|
||
VERSION_METADATA = unreleased | ||
ifneq ($(GIT_TAG),) | ||
VERSION_METADATA = | ||
endif | ||
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.buildmetadata=${VERSION_METADATA} | ||
|
||
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.gitCommit=${GIT_COMMIT} | ||
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.gitTreeState=${GIT_DIRTY} | ||
LDFLAGS += -extldflags "-static" | ||
|
||
# HELP | ||
# This will output the help for each task | ||
.PHONY: help | ||
help: ## This help. | ||
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | ||
|
||
# -- Build -- | ||
|
||
.PHONY: build | ||
build: get $(BINDIR)/$(BINNAME) ## Build go code | ||
|
||
$(BINDIR)/$(BINNAME): $(SRC) $(ASSETS) | ||
go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(BINNAME) . | ||
ifeq ($(HAS_UPX),true) | ||
@echo 'upx detected. compressing binary...' | ||
upx $(BINDIR)/$(BINNAME) | ||
else | ||
@echo 'For smaller binaries, please install upx:' | ||
@echo 'MacOS: brew install upx' | ||
@echo 'Linux: sudo apt-get install upx' | ||
endif | ||
mkdir -p $(GOPATH)/bin/ | ||
cp $(BINDIR)/$(BINNAME) $(GOPATH)/bin/ | ||
|
||
.PHONY: get | ||
get: go.mod | ||
go mod download | ||
|
||
.PHONY: generate | ||
generate: | ||
go generate ${PKG} | ||
|
||
# -- Test -- | ||
|
||
.PHONY: test | ||
test: ## Run tests | ||
go test -run . $(PKG) -race | ||
|
||
${GOTEST}: | ||
${GOGET} github.com/rakyll/gotest@v0.0.6 | ||
|
||
.PHONY: test-verbose | ||
test-verbose: ${GOTEST} | ||
gotest -run . $(PKG) -race -v | ||
|
||
${GOLANGCOVER}: | ||
${GOGET} github.com/mattn/goveralls@v0.0.11 | ||
|
||
.PHONY: test-coverage | ||
test-coverage: ${GOLANGCOVER} ## Run tests with coverage | ||
go test -run . $(PKG) -coverprofile=coverage.txt -covermode=atomic | ||
|
||
${GOLANGCILINT}: | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.31.0 | ||
|
||
.PHONY: test-style | ||
test-style: ${GOLANGCILINT} | ||
${GOLANGCILINT} run --timeout 3m | ||
|
||
# -- CI -- | ||
|
||
.PHONY: ci | ||
ci: clean build test test-style ## Run CI routine | ||
|
||
# -- Release -- | ||
|
||
$(GOX): | ||
${GOGET} github.com/mitchellh/gox@v1.0.1 | ||
|
||
.PHONY: build-cross | ||
build-cross: $(GOX) clean | ||
CGO_ENABLED=0 $(GOX) -parallel=3 -output="$(DISTDIR)/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' -ldflags '$(LDFLAGS)' ./ | ||
|
||
.PHONY: dist | ||
dist: clean build-cross ## Build distribution | ||
ifeq ($(HAS_UPX),true) | ||
@echo 'upx detected. compressing binary...' | ||
upx $(shell find . -type f -name '$(BINNAME)') | ||
else | ||
@echo 'For smaller binaries, please install upx:' | ||
@echo 'MacOS: brew install upx' | ||
@echo 'Linux: sudo apt-get install upx' | ||
endif | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf $(BINDIR) $(DISTDIR) | ||
go clean -cache | ||
|
||
.PHONY: info | ||
info: ## Get version info | ||
@echo "Version: ${VERSION}" | ||
@echo "Git Tag: ${GIT_TAG}" | ||
@echo "Git Commit: ${GIT_COMMIT}" | ||
@echo "Git Tree State: ${GIT_DIRTY}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2022 Ashok Pon Kumar | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package info | ||
|
||
import ( | ||
"runtime" | ||
) | ||
|
||
var ( | ||
version = "v0.1.0" | ||
buildmetadata = "" | ||
gitCommit = "" | ||
gitTreeState = "" | ||
) | ||
|
||
// GetVersion returns the semver string of the version | ||
func GetVersion() string { | ||
if buildmetadata == "" { | ||
return version | ||
} | ||
return version + "+" + buildmetadata | ||
} | ||
|
||
// GetVersionInfo returns version info | ||
func GetVersionInfo() VersionInfo { | ||
v := VersionInfo{ | ||
Version: GetVersion(), | ||
GitCommit: gitCommit, | ||
GitTreeState: gitTreeState, | ||
GoVersion: runtime.Version(), | ||
} | ||
return v | ||
} | ||
|
||
// VersionInfo describes the compile time information. | ||
type VersionInfo struct { | ||
// Version is the current semver. | ||
Version string `yaml:"version,omitempty"` | ||
// GitCommit is the git sha1. | ||
GitCommit string `yaml:"gitCommit,omitempty"` | ||
// GitTreeState is the state of the git tree. | ||
GitTreeState string `yaml:"gitTreeState,omitempty"` | ||
// GoVersion is the version of the Go compiler used. | ||
GoVersion string `yaml:"goVersion,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters