Skip to content

Commit

Permalink
Align buildinfo with master
Browse files Browse the repository at this point in the history
  • Loading branch information
9seconds committed Aug 9, 2022
1 parent c1935b4 commit 6485906
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 26 deletions.
84 changes: 84 additions & 0 deletions buildinfo.go
Original file line number Diff line number Diff line change
@@ -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)))
}
26 changes: 0 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package main
import (
"math/rand"
"os"
"runtime/debug"
"strings"
"time"

"github.com/9seconds/mtg/cli"
"github.com/9seconds/mtg/config"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var version = "dev" // has to be set by ldflags

var (
app = kingpin.New("mtg", "Simple MTPROTO proxy.")

Expand Down Expand Up @@ -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()
}

0 comments on commit 6485906

Please sign in to comment.