Skip to content

Commit 4c830ea

Browse files
authored
Infer runtime / arch / os platform from runtime stdlib (#1)
- Allow to minimize the flags needs to be injected - Correctes when building inside docker container (should not infer from host) - Added GoVersion
1 parent 9c0b4c6 commit 4c830ea

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
# version
2-
Go module to maintain an applicaiton version
1+
# Version
32

4-
## Usage
5-
During build Add the following linker arguments to inject the version:
3+
Go module to maintain an application version
4+
5+
## Install
6+
7+
```sh
8+
go get -u github.com/v3io/version-go
69
```
7-
-ldflags="-X github.com/v3io/version/version.gitCommit=$(GIT_COMMIT) \
8-
-X github.com/v3io/version/version.label=$(GIT_TAG) \
9-
-X github.com/v3io/version/version.os=$(GOOS) \
10-
-X github.com/v3io/version/version.arch=$(GOARCH)"
10+
11+
## Usage
12+
13+
During build, add the following linker arguments to inject the version:
14+
15+
```sh
16+
-ldflags="-X github.com/v3io/version-go/version.gitCommit=$(GIT_COMMIT) \
17+
-X github.com/v3io/version-go/version.label=$(GIT_TAG)
1118
```
1219
1320
In your application, you can then:
21+
1422
```go
1523
import "github.com/v3io/version-go"
1624

version.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ package version
22

33
import (
44
"fmt"
5+
"runtime"
56
)
67

78
type Info struct {
89
Label string `json:"label"`
910
GitCommit string `json:"git_commit"`
1011
OS string `json:"os"`
1112
Arch string `json:"arch"`
13+
GoVersion string `json:"go_version"`
1214
}
1315

1416
// these global variables are initialized by the build process if the build target
1517
// is a standalone binary
1618
var (
1719
label = "latest"
1820
gitCommit = ""
19-
os = ""
20-
arch = ""
21+
os = runtime.GOOS
22+
arch = runtime.GOARCH
23+
goVersion = runtime.Version()
2124
)
2225

2326
// Get returns the version information
@@ -29,6 +32,7 @@ func Get() *Info {
2932
GitCommit: gitCommit,
3033
OS: os,
3134
Arch: arch,
35+
GoVersion: goVersion,
3236
}
3337
}
3438

@@ -38,13 +42,15 @@ func Set(info *Info) {
3842
gitCommit = info.GitCommit
3943
os = info.OS
4044
arch = info.Arch
45+
goVersion = info.GoVersion
4146
}
4247

4348
// String will print out the info
4449
func (s *Info) String() string {
45-
return fmt.Sprintf("Label: %s, Git commit: %s, OS: %s, Arch: %s",
50+
return fmt.Sprintf("Label: %s, Git commit: %s, OS: %s, Arch: %s, Go version: %s",
4651
s.Label,
4752
s.GitCommit,
4853
s.OS,
49-
s.Arch)
54+
s.Arch,
55+
s.GoVersion)
5056
}

0 commit comments

Comments
 (0)