-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
88 lines (77 loc) · 2.13 KB
/
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
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
82
83
84
85
86
87
88
// Copyright © 2017,2020 Pennock Tech, LLC.
// All rights reserved, except as granted under license.
// Licensed per file LICENSE.txt
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
const ProjectName = "smtpdane"
// may be updated by the linker on the link command-line when compiling
var (
Version string = "0.5.2-dev"
Commit = ""
CompileDate = ""
BuiltBy = ""
)
func version() {
fmt.Printf("%s version %s\n", ProjectName, Version)
if Commit != "" {
fmt.Printf("%s commit %s\n", ProjectName, Commit)
}
if CompileDate != "" {
fmt.Printf("%s compile-date: %s\n", ProjectName, CompileDate)
}
if BuiltBy != "" {
fmt.Printf("%s built-by: %s\n", ProjectName, BuiltBy)
}
fmt.Printf("%s: Golang: Runtime: %s\n", ProjectName, runtime.Version())
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
fmt.Printf("%s: no repo version details available\n", ProjectName)
return
}
type versionLine struct {
path, version, sum string
replaced bool
}
lines := make([]versionLine, 0, 10)
addVersion := func(p, v, sum string, replaced bool) {
lines = append(lines, versionLine{p, v, sum, replaced})
}
m := &buildInfo.Main
topVersion := m.Version
if Version != "" {
topVersion = Version
}
addVersion(m.Path, topVersion, m.Sum, m.Replace != nil)
for m.Replace != nil {
m = m.Replace
addVersion(m.Path, m.Version, m.Sum, m.Replace != nil)
}
for _, m := range buildInfo.Deps {
addVersion(m.Path, m.Version, m.Sum, m.Replace != nil)
for m.Replace != nil {
m = m.Replace
addVersion(m.Path, m.Version, m.Sum, m.Replace != nil)
}
}
headers := []string{"Path", "Version", "Checksum", "Replaced"}
maxP, maxV, maxS := len(headers[0]), len(headers[1]), len(headers[2])
for _, l := range lines {
if len(l.path) > maxP {
maxP = len(l.path)
}
if len(l.version) > maxV {
maxV = len(l.version)
}
if len(l.sum) > maxS {
maxS = len(l.sum)
}
}
fmt.Printf("%-*s %-*s %-*s %s\n", maxP, headers[0], maxV, headers[1], maxS, headers[2], headers[3])
for _, l := range lines {
fmt.Printf("%-*s %-*s %-*s %v\n", maxP, l.path, maxV, l.version, maxS, l.sum, l.replaced)
}
}