Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init version project #1

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Go Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# example binary
examples
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# version
# version
package version defines the utility information for versioning binary

## Usage

Import this package in your application:

```go
import _ "github.com/qiniu/version"
```

Build it with:

```shell
go build -ldflags "-X 'github.com/qiniu/version.BuildDate=$(date)'" .
```

Then run, it will output like:
5 changes: 5 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// package version defines the information for versioning binary
// usage:
//
// import _ "github.com/CarlJi/version"
package version
11 changes: 11 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

_ "github.com/qiniu/version"
)

func main() {
fmt.Println("Hello World!")
}
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/qiniu/version

go 1.19
119 changes: 119 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package version

import (
"fmt"
"os"
"runtime"
"runtime/debug"
"sync"
)

const (
unknownProperty = "N/A"
)

// Information of versioning
var (
// GoVersion is the version of the Go toolchain that built the binary(for example, "go1.19.2").
GoVersion = unknownProperty
// GitCommit shows the revision identifier for the current commit or checkout.
GitCommit = unknownProperty
// GitCommitDate shows the time associated with GitCommit, in RFC3339 format
GitCommitDate = unknownProperty
// GitTreeState shows "dirty" indicating the source tree had local modifications, otherwise it is invisible.
GitTreeState = unknownProperty
// GitTag shows latest tag if injected by go -ldflags. otherwise it is invisible by default.
GitTag = unknownProperty
// BuildDate shows the built time for the associated binary
BuildDate = unknownProperty
// Platform composes with GOARCH and GOOS
Platform = unknownProperty
// Compiler shows the toolchain flag used (typically "gc")
Compiler = unknownProperty
)

func init() {
// usages: <command> version or <command> --version
if len(os.Args) > 1 && (os.Args[1] == "version" || os.Args[1] == "--version") {
Version()
os.Exit(0)
}

// TODO: expose version information via exporter as needed
}

var once sync.Once

// Version prints the information of versioning
func Version() {
once.Do(func() {
collectFromBuildInfo()
collectFromRuntime()
})

format := "%s:\t%s\n"
xprintf(format, "Go version", GoVersion)
xprintf(format, "Git commit", GitCommit)
xprintf(format, "Commit date", GitCommitDate)

if GitTreeState != unknownProperty {
xprintf(format, "Git tree state", GitTreeState)
}

xprintf(format, "Built date", BuildDate)
xprintf(format, "OS/Arch", Platform)
xprintf(format, "Compiler", Compiler)

if GitTag != unknownProperty {
xprintf(format, "Git tag", GitTag)
}

}

// collectFromBuildInfo tries to set the build information embedded in the running binary via Go module.
// It doesn't override data if were already set by Go -ldflags.
func collectFromBuildInfo() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}

for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
if GitCommit == unknownProperty && kv.Value != "" {
GitCommit = kv.Value
}
case "vcs.time":
if GitCommitDate == unknownProperty && kv.Value != "" {
GitCommitDate = kv.Value
}

case "vcs.modified":
if GitTreeState == unknownProperty && kv.Value != "" {
GitTreeState = "dirty"
}
}
}
}

// collectFromRuntime tries to set the build information embedded in the running binary via go runtime.
// It doesn't override data if were already set by Go -ldflags.
func collectFromRuntime() {
if GoVersion == unknownProperty {
GoVersion = runtime.Version()
}

if Platform == unknownProperty {
Platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
}

if Compiler == unknownProperty {
Compiler = runtime.Compiler
}
}

// xprintf prints a message to standard output.
func xprintf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}