-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
136 lines (118 loc) · 3.2 KB
/
build.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Package build provides details of the built binary
// The details are set using ldflags.
//
// The ldflags can be set manually for testing locally:
// `go build -ldflags "-X github.com/srimaln91/go-make.version=$(git describe --tags) -X github.com/srimaln91/go-make.date=$(date -u +%Y-%m-%d-%H:%M:%S-%Z)"`
package build
import (
"bytes"
"encoding/json"
"fmt"
"os"
"runtime"
"github.com/olekukonko/tablewriter"
)
// Details represents known data for a given build
type Details struct {
Version string `json:"version,omitempty"`
GoRuntime string `json:"go_runtime,omitempty"`
GitCommit string `json:"git_commit,omitempty"`
OSArch string `json:"os_arch,omitempty"`
Date string `json:"date,omitempty"`
}
type Printer interface {
Print(details Details) error
}
var version, date, gitCommit, osArch string
var (
PRINTER_TABLE = TablePrinter{Writer: os.Stdout}
PRINTER_STRING = StringPrinter{Writer: os.Stdout}
PRINTER_SINGLE_LINE = SingleLinePrinter{Writer: os.Stdout}
)
/*
String returns build details as a string with formatting
suitable for console output.
Ex:
Build Details:
Version: v0.5.0
Go Runtime: go1.12.9
Git Commit: bc2e7ce8edc4aa85cc258890e0e4381630cbf5f8
OS/Arch: linux/amd64
Built On: 2019-10-05-12:17:29-UTC
*/
func String() string {
return fmt.Sprintf(`
Build Details:
Version: %s
Go Runtime: %s
Git Commit: %s
OS/Arch: %s
Built on: %s
`,
version,
runtime.Version(),
gitCommit,
osArch,
date,
)
}
/*
Table returns build details as a table
Suitable for console output
Ex:
+----------------+------------+------------------------------------------+-------------+-------------------------+
| BINARY VERSION | GO VERSION | GIT COMMIT | OS/ARCH | BUILT |
+----------------+------------+------------------------------------------+-------------+-------------------------+
| v0.5.0-dirty | go1.12.9 | bc2e7ce8edc4aa85cc258890e0e4381630cbf5f8 | linux/amd64 | 2019-10-05-12:17:29-UTC |
+----------------+------------+------------------------------------------+-------------+-------------------------+
*/
func Table() string {
tableBuffer := new(bytes.Buffer)
data := []string{
version,
runtime.Version(),
gitCommit,
osArch,
date,
}
table := tablewriter.NewWriter(tableBuffer)
table.SetHeader([]string{"Binary Version", "Go Version", "Git Commit", "OS/Arch", "Built"})
table.Append(data)
table.Render()
return tableBuffer.String()
}
// JSON returns build details as a JSON string
func JSON() ([]byte, error) {
return json.Marshal(Data())
}
// Data returns build details as a struct
func Data() Details {
return Details{
Version: version,
GoRuntime: runtime.Version(),
GitCommit: gitCommit,
OSArch: osArch,
Date: date,
}
}
// CheckVersion checks --version os argument and prints the binary build details in the console
func CheckVersion(printer ...Printer) {
if len(printer) == 0 {
printer = append(printer, StringPrinter{
Writer: os.Stdout,
})
}
// Check OS arguments
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
case "--version", "-v":
for _, p := range printer {
err := p.Print(Data())
if err != nil {
os.Exit(1)
}
}
os.Exit(0)
}
}
}