-
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.
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 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
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,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} ` |
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,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 = "" | ||
) |
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,3 @@ | ||
module github.com/xwi88/version | ||
|
||
go 1.14 |
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,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, | ||
} | ||
} |