Skip to content

Commit

Permalink
feat: Add support for version and add Makefile
Browse files Browse the repository at this point in the history
Signed-off-by: Ashok Pon Kumar <ashokponkumar@gmail.com>
  • Loading branch information
ashokponkumar committed Jan 20, 2022
1 parent 2cc1643 commit 20a212b
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 18 deletions.
50 changes: 38 additions & 12 deletions .gitignore
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/
155 changes: 155 additions & 0 deletions Makefile
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}"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.3.0
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
Expand Down Expand Up @@ -752,6 +754,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
59 changes: 59 additions & 0 deletions info/versioninfo.go
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"`
}
29 changes: 23 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (
"strconv"
"time"

"github.com/ashokponkumar/github-org-stats/info"
"github.com/google/go-github/v39/github"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/oauth2"

"gopkg.in/yaml.v3"
)

var (
Expand All @@ -53,11 +56,11 @@ func init() {
token = os.Getenv("GITHUB_TOKEN")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
client, err := getClient(ctx, token, githubBaseURL)
if err != nil {
return err
logrus.Fatalf("%s", err)
}
repos, err := repos(ctx, client, org)
if err != nil {
Expand Down Expand Up @@ -89,16 +92,30 @@ func init() {
logrus.Infof("Organization : %s", org)
logrus.Infof("No of stars : %d", totalStars)
logrus.Infof("No of forks : %d", totalForks)
return nil
},
}

rootCmd.Flags().StringVarP(&token, tokenC, "t", "", "github personal access token (default $GITHUB_TOKEN)")
rootCmd.Flags().StringVarP(&org, orgC, "o", "", "github organisation name")

rootCmd.MarkFlagRequired(orgC)

rootCmd.Flags().StringVar(&githubBaseURL, "github-base-url", "", "Github base url, if it is not github.com")

long := false
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version information",
Long: "Print the version information",
Run: func(*cobra.Command, []string) {
if !long {
fmt.Println(info.GetVersion())
return
}
v := info.GetVersionInfo()
ver, _ := yaml.Marshal(v)
fmt.Println(string(ver))
},
}
versionCmd.Flags().BoolVarP(&long, "long", "l", false, "Print the version details.")
rootCmd.AddCommand(versionCmd)
}

func main() {
Expand Down

0 comments on commit 20a212b

Please sign in to comment.