Skip to content

Commit 20a212b

Browse files
committed
feat: Add support for version and add Makefile
Signed-off-by: Ashok Pon Kumar <[email protected]>
1 parent 2cc1643 commit 20a212b

File tree

6 files changed

+279
-18
lines changed

6 files changed

+279
-18
lines changed

.gitignore

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
# Binaries for programs and plugins
2-
*.exe
3-
*.exe~
4-
*.dll
5-
*.so
6-
*.dylib
1+
# Copyright 2022 Ashok Pon Kumar
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
714

8-
# Test binary, built with `go test -c`
9-
*.test
15+
.vscode
16+
demo_tmp
17+
sample_tmp
18+
work_dir
19+
m2k
20+
m2k_bin
21+
colleted_md
22+
.DS_Store
23+
_dist
24+
bin
25+
.m2k
26+
coverage.txt
27+
.jekyll-cache/
28+
_site/
29+
node_modules
30+
target/
1031

11-
# Output of the go coverage tool, specifically when used with LiteIDE
12-
*.out
32+
# brew bottling creates .brew_home directory
33+
# add this to avoid git tree start becoming dirty
34+
.brew_home/
35+
36+
__pycache__/
37+
*.pyc
38+
.bundle/
39+
vendor/
40+
.ruby-version
1341

14-
# Dependency directories (remove the comment below to include it)
15-
# vendor/

Makefile

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Copyright 2022 Ashok Pon Kumar
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
BINNAME ?= github-org-stats
16+
ORGNAME ?= ashokponkumar
17+
BINDIR := $(CURDIR)/bin
18+
DISTDIR := $(CURDIR)/_dist
19+
TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64
20+
21+
GOPATH = $(shell go env GOPATH)
22+
GOX = $(GOPATH)/bin/gox
23+
GOTEST = ${GOPATH}/bin/gotest
24+
GOLANGCILINT = $(GOPATH)/bin/golangci-lint
25+
GOLANGCOVER = $(GOPATH)/bin/goveralls
26+
27+
PKG := ./...
28+
LDFLAGS := -w -s
29+
30+
SRC = $(shell find . -type f -name '*.go' -print)
31+
ASSETS = $(shell find assets -type f -name '*' -print)
32+
ARCH = $(shell uname -p)
33+
GIT_COMMIT = $(shell git rev-parse HEAD)
34+
GIT_SHA = $(shell git rev-parse --short HEAD)
35+
GIT_TAG = $(shell git tag --points-at | tail -n 1)
36+
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
37+
HAS_UPX = $(shell command -v upx >/dev/null && echo true || echo false)
38+
39+
GOGET := cd / && GO111MODULE=on go install
40+
41+
ifdef VERSION
42+
BINARY_VERSION = $(VERSION)
43+
endif
44+
BINARY_VERSION ?= ${GIT_TAG}
45+
ifneq ($(BINARY_VERSION),)
46+
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/info.version=${BINARY_VERSION}
47+
VERSION ?= $(BINARY_VERSION)
48+
endif
49+
VERSION ?= latest
50+
51+
VERSION_METADATA = unreleased
52+
ifneq ($(GIT_TAG),)
53+
VERSION_METADATA =
54+
endif
55+
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.buildmetadata=${VERSION_METADATA}
56+
57+
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.gitCommit=${GIT_COMMIT}
58+
LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.gitTreeState=${GIT_DIRTY}
59+
LDFLAGS += -extldflags "-static"
60+
61+
# HELP
62+
# This will output the help for each task
63+
.PHONY: help
64+
help: ## This help.
65+
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
66+
67+
# -- Build --
68+
69+
.PHONY: build
70+
build: get $(BINDIR)/$(BINNAME) ## Build go code
71+
72+
$(BINDIR)/$(BINNAME): $(SRC) $(ASSETS)
73+
go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(BINNAME) .
74+
ifeq ($(HAS_UPX),true)
75+
@echo 'upx detected. compressing binary...'
76+
upx $(BINDIR)/$(BINNAME)
77+
else
78+
@echo 'For smaller binaries, please install upx:'
79+
@echo 'MacOS: brew install upx'
80+
@echo 'Linux: sudo apt-get install upx'
81+
endif
82+
mkdir -p $(GOPATH)/bin/
83+
cp $(BINDIR)/$(BINNAME) $(GOPATH)/bin/
84+
85+
.PHONY: get
86+
get: go.mod
87+
go mod download
88+
89+
.PHONY: generate
90+
generate:
91+
go generate ${PKG}
92+
93+
# -- Test --
94+
95+
.PHONY: test
96+
test: ## Run tests
97+
go test -run . $(PKG) -race
98+
99+
${GOTEST}:
100+
${GOGET} github.com/rakyll/[email protected]
101+
102+
.PHONY: test-verbose
103+
test-verbose: ${GOTEST}
104+
gotest -run . $(PKG) -race -v
105+
106+
${GOLANGCOVER}:
107+
${GOGET} github.com/mattn/[email protected]
108+
109+
.PHONY: test-coverage
110+
test-coverage: ${GOLANGCOVER} ## Run tests with coverage
111+
go test -run . $(PKG) -coverprofile=coverage.txt -covermode=atomic
112+
113+
${GOLANGCILINT}:
114+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.31.0
115+
116+
.PHONY: test-style
117+
test-style: ${GOLANGCILINT}
118+
${GOLANGCILINT} run --timeout 3m
119+
120+
# -- CI --
121+
122+
.PHONY: ci
123+
ci: clean build test test-style ## Run CI routine
124+
125+
# -- Release --
126+
127+
$(GOX):
128+
${GOGET} github.com/mitchellh/[email protected]
129+
130+
.PHONY: build-cross
131+
build-cross: $(GOX) clean
132+
CGO_ENABLED=0 $(GOX) -parallel=3 -output="$(DISTDIR)/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' -ldflags '$(LDFLAGS)' ./
133+
134+
.PHONY: dist
135+
dist: clean build-cross ## Build distribution
136+
ifeq ($(HAS_UPX),true)
137+
@echo 'upx detected. compressing binary...'
138+
upx $(shell find . -type f -name '$(BINNAME)')
139+
else
140+
@echo 'For smaller binaries, please install upx:'
141+
@echo 'MacOS: brew install upx'
142+
@echo 'Linux: sudo apt-get install upx'
143+
endif
144+
145+
.PHONY: clean
146+
clean:
147+
rm -rf $(BINDIR) $(DISTDIR)
148+
go clean -cache
149+
150+
.PHONY: info
151+
info: ## Get version info
152+
@echo "Version: ${VERSION}"
153+
@echo "Git Tag: ${GIT_TAG}"
154+
@echo "Git Commit: ${GIT_COMMIT}"
155+
@echo "Git Tree State: ${GIT_DIRTY}"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/sirupsen/logrus v1.8.1
88
github.com/spf13/cobra v1.3.0
99
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
10+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
1011
)
1112

1213
require (

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
244244
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
245245
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
246246
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
247+
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
247248
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
248249
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
250+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
249251
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
250252
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
251253
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
@@ -752,6 +754,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
752754
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
753755
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
754756
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
757+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
755758
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
756759
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
757760
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

info/versioninfo.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 Ashok Pon Kumar
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package info
18+
19+
import (
20+
"runtime"
21+
)
22+
23+
var (
24+
version = "v0.1.0"
25+
buildmetadata = ""
26+
gitCommit = ""
27+
gitTreeState = ""
28+
)
29+
30+
// GetVersion returns the semver string of the version
31+
func GetVersion() string {
32+
if buildmetadata == "" {
33+
return version
34+
}
35+
return version + "+" + buildmetadata
36+
}
37+
38+
// GetVersionInfo returns version info
39+
func GetVersionInfo() VersionInfo {
40+
v := VersionInfo{
41+
Version: GetVersion(),
42+
GitCommit: gitCommit,
43+
GitTreeState: gitTreeState,
44+
GoVersion: runtime.Version(),
45+
}
46+
return v
47+
}
48+
49+
// VersionInfo describes the compile time information.
50+
type VersionInfo struct {
51+
// Version is the current semver.
52+
Version string `yaml:"version,omitempty"`
53+
// GitCommit is the git sha1.
54+
GitCommit string `yaml:"gitCommit,omitempty"`
55+
// GitTreeState is the state of the git tree.
56+
GitTreeState string `yaml:"gitTreeState,omitempty"`
57+
// GoVersion is the version of the Go compiler used.
58+
GoVersion string `yaml:"goVersion,omitempty"`
59+
}

main.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ import (
2525
"strconv"
2626
"time"
2727

28+
"github.com/ashokponkumar/github-org-stats/info"
2829
"github.com/google/go-github/v39/github"
2930
"github.com/sirupsen/logrus"
3031
"github.com/spf13/cobra"
3132
"golang.org/x/oauth2"
33+
34+
"gopkg.in/yaml.v3"
3235
)
3336

3437
var (
@@ -53,11 +56,11 @@ func init() {
5356
token = os.Getenv("GITHUB_TOKEN")
5457
}
5558
},
56-
RunE: func(cmd *cobra.Command, args []string) error {
59+
Run: func(cmd *cobra.Command, args []string) {
5760
ctx := context.Background()
5861
client, err := getClient(ctx, token, githubBaseURL)
5962
if err != nil {
60-
return err
63+
logrus.Fatalf("%s", err)
6164
}
6265
repos, err := repos(ctx, client, org)
6366
if err != nil {
@@ -89,16 +92,30 @@ func init() {
8992
logrus.Infof("Organization : %s", org)
9093
logrus.Infof("No of stars : %d", totalStars)
9194
logrus.Infof("No of forks : %d", totalForks)
92-
return nil
9395
},
9496
}
95-
9697
rootCmd.Flags().StringVarP(&token, tokenC, "t", "", "github personal access token (default $GITHUB_TOKEN)")
9798
rootCmd.Flags().StringVarP(&org, orgC, "o", "", "github organisation name")
98-
9999
rootCmd.MarkFlagRequired(orgC)
100-
101100
rootCmd.Flags().StringVar(&githubBaseURL, "github-base-url", "", "Github base url, if it is not github.com")
101+
102+
long := false
103+
versionCmd := &cobra.Command{
104+
Use: "version",
105+
Short: "Print the version information",
106+
Long: "Print the version information",
107+
Run: func(*cobra.Command, []string) {
108+
if !long {
109+
fmt.Println(info.GetVersion())
110+
return
111+
}
112+
v := info.GetVersionInfo()
113+
ver, _ := yaml.Marshal(v)
114+
fmt.Println(string(ver))
115+
},
116+
}
117+
versionCmd.Flags().BoolVarP(&long, "long", "l", false, "Print the version details.")
118+
rootCmd.AddCommand(versionCmd)
102119
}
103120

104121
func main() {

0 commit comments

Comments
 (0)