From dedeab7ef483bcaceb8fcd4884407760e1f290a6 Mon Sep 17 00:00:00 2001 From: xwi88 <278810732@qq.com> Date: Mon, 25 May 2020 11:53:05 +0800 Subject: [PATCH] init version info --- .gitignore | 6 ++++++ README.md | 33 +++++++++++++++++++++++++++++ base.go | 16 ++++++++++++++ go.mod | 3 +++ version.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 base.go create mode 100644 go.mod create mode 100644 version.go diff --git a/.gitignore b/.gitignore index 66fd13c..f4c979c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,9 @@ # Dependency directories (remove the comment below to include it) # vendor/ +.idea + +build +test +#local test use +Makefile diff --git a/README.md b/README.md index 1b8d367..38bc93f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,35 @@ # version go build with version + +## usage + +```makefile +# Makefile to build the command lines and tests in this project. +# This Makefile doesn't consider Windows Environment. If you use it in Windows, please be careful. + +SHELL := /bin/bash + +#BASEDIR = $(shell pwd) +BASEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) + +# add following lines before go build! +versionDir = github.com/xwi88/version + +gitBranch = $(shell git symbolic-ref --short -q HEAD) + +ifeq ($(gitBranch),) +gitTag = $(shell git describe --always --tags --abbrev=0) +endif + +buildTime = $(shell date "+%FT%T%z") +gitCommit = $(shell git rev-parse HEAD) +gitTreeState = $(shell if git status|grep -q 'clean';then echo clean; else echo dirty; fi) + +# -ldflags flags accept a space-separated list of arguments to pass to an underlying tool during the build. +ldflags="-X ${versionDir}.gitBranch=${gitBranch} -X ${versionDir}.gitTag=${gitTag} \ + -X ${versionDir}.buildTime=${buildTime} -X ${versionDir}.gitCommit=${gitCommit} \ + -X ${versionDir}.gitTreeState=${gitTreeState}" + +``` + +`go build -ldflags ${ldflags} ` diff --git a/base.go b/base.go new file mode 100644 index 0000000..01f00b8 --- /dev/null +++ b/base.go @@ -0,0 +1,16 @@ +// Copyright 2020 xwi88. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +// Build info for your project with arguments +package version + +var ( + gitBranch string + gitTag string + 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" + buildTime = "" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') + buildTimeLayout = "2006-01-02T15:04:05Z0700" // ref time.RFC3339: 2006-01-02T15:04:05Z07:00 + execTime = "" +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..316eeb6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/xwi88/version + +go 1.14 diff --git a/version.go b/version.go new file mode 100644 index 0000000..2ec4bda --- /dev/null +++ b/version.go @@ -0,0 +1,61 @@ +// Copyright 2020 xwi88. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +// Build and get info for your project +package version + +import ( + "encoding/json" + "fmt" + "runtime" + "strings" + "time" +) + +// Info contains version information. +type Info struct { + GitBranch string `json:"git_branch"` + GitTag string `json:"git_tag"` + GitCommit string `json:"git_commit"` + GitTreeState string `json:"git_tree_state"` + BuildTime string `json:"build_time"` + Compiler string `json:"compiler"` + GoVersion string `json:"go_version"` + Platform string `json:"platform"` + ExecTime string `json:"exec_time"` +} + +// String returns info as a human-friendly json version string. +func (info Info) String() string { + vv, _ := json.Marshal(info) + return fmt.Sprintf("%v", string(vv)) +} + +// String returns info as a human-friendly indent json version string. +func (info Info) StringWithIndent() string { + vv, _ := json.MarshalIndent(info, "", " ") + return fmt.Sprintf("%v", string(vv)) +} + +// Get get the version info +func Get() Info { + loc, _ := time.LoadLocation("Local") + buildTimeNow := time.Now().In(loc) + execTime = buildTimeNow.Format(buildTimeLayout) + + if len(buildTime) == 0 || strings.Trim(buildTime, "") == "" || strings.Trim(buildTime, "''") == "" { + buildTime = buildTimeLayout + } + return Info{ + GitBranch: gitBranch, + GitTag: gitTag, + GitCommit: gitCommit, + GitTreeState: gitTreeState, + BuildTime: buildTime, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + ExecTime: execTime, + } +}