From 64859069ed82d1ccf3f941b43698e5532cfdd9b8 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Tue, 9 Aug 2022 17:43:29 +0300 Subject: [PATCH] Align buildinfo with master --- buildinfo.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 26 ---------------- 2 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 buildinfo.go diff --git a/buildinfo.go b/buildinfo.go new file mode 100644 index 000000000..74e6782dd --- /dev/null +++ b/buildinfo.go @@ -0,0 +1,84 @@ +package main + +import ( + "crypto/sha256" + "encoding/base64" + "encoding/binary" + "fmt" + "io" + "runtime/debug" + "sort" + "strconv" + "time" +) + +var version = "dev" // has to be set by ldflags + +const ( + buildInfoModuleStart byte = iota + buildInfoModuleFinish + buildInfoModuleDelimeter +) + +func getVersion() string { + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return version + } + + date := time.Now() + commit := "" + goVersion := buildInfo.GoVersion + dirtySuffix := "" + + for _, setting := range buildInfo.Settings { + switch setting.Key { + case "vcs.time": + date, _ = time.Parse(time.RFC3339, setting.Value) + case "vcs.revision": + commit = setting.Value + case "vcs.modified": + if dirty, _ := strconv.ParseBool(setting.Value); dirty { + dirtySuffix = " [dirty]" + } + } + } + + hasher := sha256.New() + + checksumModule := func(mod *debug.Module) { + hasher.Write([]byte{buildInfoModuleStart}) + + io.WriteString(hasher, mod.Path) //nolint: errcheck + hasher.Write([]byte{buildInfoModuleDelimeter}) + + io.WriteString(hasher, mod.Version) //nolint: errcheck + hasher.Write([]byte{buildInfoModuleDelimeter}) + + io.WriteString(hasher, mod.Sum) //nolint: errcheck + + hasher.Write([]byte{buildInfoModuleFinish}) + } + + io.WriteString(hasher, buildInfo.Path) //nolint: errcheck + + binary.Write(hasher, binary.LittleEndian, uint64(1+len(buildInfo.Deps))) //nolint: errcheck + + sort.Slice(buildInfo.Deps, func(i, j int) bool { + return buildInfo.Deps[i].Path > buildInfo.Deps[j].Path + }) + + checksumModule(&buildInfo.Main) + + for _, module := range buildInfo.Deps { + checksumModule(module) + } + + return fmt.Sprintf("%s (%s: %s on %s%s, modules checksum %s)", + version, + goVersion, + date.Format(time.RFC3339), + commit, + dirtySuffix, + base64.StdEncoding.EncodeToString(hasher.Sum(nil))) +} diff --git a/main.go b/main.go index 841556716..7734812b5 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,6 @@ package main import ( "math/rand" "os" - "runtime/debug" - "strings" "time" "github.com/9seconds/mtg/cli" @@ -12,8 +10,6 @@ import ( kingpin "gopkg.in/alecthomas/kingpin.v2" ) -var version = "dev" // has to be set by ldflags - var ( app = kingpin.New("mtg", "Simple MTPROTO proxy.") @@ -161,25 +157,3 @@ func main() { } } } - -func getVersion() string { - if version != "dev" { - return version - } - - info, ok := debug.ReadBuildInfo() - if !ok { - return version - } - - builder := strings.Builder{} - builder.WriteString(info.Main.Version) - - if info.Main.Sum != "" { - builder.WriteString(" (checksum: ") - builder.WriteString(info.Main.Sum) - builder.WriteRune(')') - } - - return builder.String() -}