Skip to content

Commit

Permalink
add version info
Browse files Browse the repository at this point in the history
  • Loading branch information
ranqiwei committed Jun 13, 2019
1 parent 47ac8e9 commit 456d249
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
SHELL := /bin/bash
BASEDIR = $(shell pwd)

#build with version info
versionDir = "apiserver/pkg/version"
gitTag = $(shell if [ "`git describe --tags --abbrev=0 2>/dev/null`" != ""];then git describe --tags --abbrev=0; else git log --pretty=format:'%n' -n 1;fi)
buildDate = $(shell TZ=Asia/Shanghai date +%FT%T%z)
gitCommit = $(shell git log --pretty=format:'%H' -n 1)
gitTreeState = $(shell if git status|grep -q 'clean';then echo clean;else echo dirty;fi)

ldflags = "-w -X ${versionDir}.gitTag=${gitTag} -X ${versionDir}.buildDate=${buildDate} -X ${versionDir}.gitCommit=${gitCommit} -X ${versionDir}.gitTreeState=${gitTreeState}"

all: gotool
@go build -v .
@go build -v -ldflags ${ldflags} .
clean:
rm -f apiserver
find . -name "[._]*.s[a-w][a-z]"|xargs -i rm -f {}
Expand Down
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package main

import (
"apiserver/config"
version2 "apiserver/pkg/version"
"apiserver/router"
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"

"apiserver/model"
"apiserver/router/middleware"
Expand All @@ -17,10 +21,24 @@ import (

var (
//配置文件
cfg = pflag.StringP("config", "c", "", "api config file path")
cfg = pflag.StringP("config", "c", "", "api config file path")
version = pflag.BoolP("version", "v", false, "show version info.")
)

func main() {
//version
pflag.Parse()
if *version {
v := version2.Get()
marshaled, err := json.MarshalIndent(&v, "", " ")
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Println(string(marshaled))
return
}

//viper初始化调用
if err := config.Init(*cfg); err != nil {
panic(err)
Expand Down
8 changes: 8 additions & 0 deletions pkg/version/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package version

var (
gitTag = ""
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState = "not a git tree" // state of git tree, either "clean" or "dirty"
buildDate = "1970-01-01T00:00:002" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)
1 change: 1 addition & 0 deletions pkg/version/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package version
32 changes: 32 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package version

import (
"fmt"
"runtime"
)

type Info struct {
GitTag string `json:"gitTag"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}

func (info Info) String() string {
return info.GitTag
}

func Get() Info {
return Info{
GitTag: gitTag,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

0 comments on commit 456d249

Please sign in to comment.