-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
44 lines (38 loc) · 873 Bytes
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cmd
import (
"fmt"
"os"
"runtime/debug"
"github.com/spf13/cobra"
)
var Version FullVersion
var (
versionCmd = &cobra.Command{
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
os.Stdout.Write([]byte(Version.ToString() + "\n"))
},
}
)
type FullVersion struct {
Version string
Commit string
Date string
BuiltBy string
}
func (v FullVersion) ToString() string {
result := v.Version
if v.Commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, v.Commit)
}
if v.Date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, v.Date)
}
if v.BuiltBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, v.BuiltBy)
}
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
}
return result
}