~bigbes/ci-cacher

ref: d041811523ea4e4dc5aeb636ab3c252c19a3b5b8 ci-cacher/internal/version/version.go -rw-r--r-- 1.4 KiB
d0418115 — Eugene Blikh cacher: add 'docker download --pull' and silence cobra usage on errors 2 days ago
                                                                                
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package version

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

var version = "dev"

type Info struct {
	GitVersion   string
	GitCommit    string
	GitTreeState string
	BuildDate    string
	BuiltBy      string
	Clean        bool
	GoVersion    string
	Compiler     string
	ModuleSum    string
	Platform     string
}

func Get() Info {
	info := Info{
		GitVersion: version,
		GoVersion:  runtime.Version(),
		Compiler:   runtime.Compiler,
		Platform:   runtime.GOOS + "/" + runtime.GOARCH,
	}

	bi, ok := debug.ReadBuildInfo()
	if !ok {
		return info
	}

	info.ModuleSum = bi.Main.Sum
	info.Clean = true

	for _, s := range bi.Settings {
		switch s.Key {
		case "vcs.revision":
			info.GitCommit = s.Value
		case "vcs.time":
			info.BuildDate = s.Value
		case "vcs.modified":
			if s.Value == "true" {
				info.Clean = false
				info.GitTreeState = "dirty"
			} else {
				info.GitTreeState = "clean"
			}
		}
	}

	return info
}

func (i Info) String() string {
	return fmt.Sprintf(`GitVersion:    %s
GitCommit:     %s
GitTreeState:  %s
BuildDate:     %s
BuiltBy:       %s
Clean:         %t
GoVersion:     %s
Compiler:      %s
ModuleSum:     %s
Platform:      %s`,
		val(i.GitVersion), val(i.GitCommit), val(i.GitTreeState),
		val(i.BuildDate), val(i.BuiltBy), i.Clean,
		i.GoVersion, i.Compiler, val(i.ModuleSum), i.Platform,
	)
}

func val(s string) string {
	if s == "" {
		return "unknown"
	}
	return s
}