Skip to content

Commit

Permalink
init version info
Browse files Browse the repository at this point in the history
  • Loading branch information
xwi88 committed May 25, 2020
1 parent 8814924 commit dedeab7
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.idea

build
test
#local test use
Makefile
33 changes: 33 additions & 0 deletions README.md
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} `
16 changes: 16 additions & 0 deletions base.go
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 = ""
)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/xwi88/version

go 1.14
61 changes: 61 additions & 0 deletions version.go
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,
}
}

0 comments on commit dedeab7

Please sign in to comment.