-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabout.go
269 lines (236 loc) · 8.91 KB
/
about.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cmd_toolkit
import (
"fmt"
"runtime/debug"
"sort"
"time"
"github.com/majohn-r/output"
)
// BuildInformation provides data about the build
type BuildInformation interface {
GoVersion() string
Dependencies() []string
MainVersion() string
Settings() []string
}
type buildInformation struct {
goVersion string
dependencies []string
mainVersion string
settings []string
}
// GoVersion returns the version of Go used to build the code
func (bi *buildInformation) GoVersion() string {
return bi.goVersion
}
// Dependencies returns an alphabetically sorted slice of dependency data (path and version for each dependency)
func (bi *buildInformation) Dependencies() []string {
return bi.dependencies
}
// MainVersion returns the git version of the main module
func (bi *buildInformation) MainVersion() string {
return bi.mainVersion
}
// Settings returns an alphabetically sorted slice of build settings used to build the code
func (bi *buildInformation) Settings() []string {
return bi.settings
}
// GetBuildData returns the build data, if any, obtained from the provided reader function
func GetBuildData(reader func() (*debug.BuildInfo, bool)) BuildInformation {
bi := &buildInformation{
goVersion: "unknown",
dependencies: []string{},
mainVersion: "unknown",
settings: []string{},
}
if reader == nil {
return bi
}
buildInfo, infoObtained := reader()
if !infoObtained || buildInfo == nil {
return bi
}
bi.goVersion = buildInfo.GoVersion
bi.dependencies = make([]string, len(buildInfo.Deps))
for k, d := range buildInfo.Deps {
bi.dependencies[k] = fmt.Sprintf("%s %s", d.Path, d.Version)
}
sort.Strings(bi.dependencies)
bi.mainVersion = buildInfo.Main.Version
bi.settings = make([]string, len(buildInfo.Settings))
for k, s := range buildInfo.Settings {
bi.settings[k] = fmt.Sprintf("%s: %s", s.Key, s.Value)
}
sort.Strings(bi.settings)
return bi
}
// the code in this file is for the "about" command, which is a common need for
// applications.
// Deprecated: use GetBuildData instead and call GoVersion() and Dependencies() on the result.
// InterpretBuildData interprets the output of calling buildInfoReader() into easily
// consumed forms; see https://github.com/majohn-r/cmd-toolkit/issues/17. for production
// callers, pass in debug.ReadBuildInfo
func InterpretBuildData(buildInfoReader func() (*debug.BuildInfo, bool)) (goVersion string, dependencies []string) {
bi := GetBuildData(buildInfoReader)
goVersion = bi.GoVersion()
dependencies = bi.Dependencies()
return
}
func finalYear(o output.Bus, timestamp string, initialYear int) int {
t, parseErr := time.Parse(time.RFC3339, timestamp)
if parseErr != nil {
o.ErrorPrintf("The build time %q cannot be parsed: %s.\n", timestamp, ErrorToString(parseErr))
o.Log(output.Error, "parse error", map[string]any{
"error": parseErr,
"value": timestamp,
})
return initialYear
}
return t.Year()
}
// FormatBuildDependencies returns build dependency data formatted nicely;
// see https://github.com/majohn-r/cmd-toolkit/issues/17
func FormatBuildDependencies(dependencies []string) []string {
formatted := make([]string, len(dependencies))
for index, dep := range dependencies {
formatted[index] = fmt.Sprintf(" - Dependency: %s", dep)
}
return formatted
}
// FormatGoVersion returns the formatted go version;
// see https://github.com/majohn-r/cmd-toolkit/issues/17cmdtoolkit.
func FormatGoVersion(version string) string {
return fmt.Sprintf(" - Go version: %s", version)
}
func formatCopyright(firstYear, lastYear int, owner string) string {
if lastYear <= firstYear {
return fmt.Sprintf("Copyright © %d %s", firstYear, owner)
}
return fmt.Sprintf("Copyright © %d-%d %s", firstYear, lastYear, owner)
}
type boxChars interface {
upperLeftCorner() rune
upperRightCorner() rune
lowerLeftCorner() rune
lowerRightCorner() rune
verticalLine() rune
horizontalLine() rune
}
type asciiBoxChars struct{}
func (asciiBoxChars) upperLeftCorner() rune { return '+' }
func (asciiBoxChars) upperRightCorner() rune { return '+' }
func (asciiBoxChars) lowerLeftCorner() rune { return '+' }
func (asciiBoxChars) lowerRightCorner() rune { return '+' }
func (asciiBoxChars) verticalLine() rune { return '|' }
func (asciiBoxChars) horizontalLine() rune { return '-' }
type curvedBoxChars struct{}
func (curvedBoxChars) upperLeftCorner() rune { return '╭' }
func (curvedBoxChars) upperRightCorner() rune { return '╮' }
func (curvedBoxChars) lowerLeftCorner() rune { return '╰' }
func (curvedBoxChars) lowerRightCorner() rune { return '╯' }
func (curvedBoxChars) verticalLine() rune { return '│' }
func (curvedBoxChars) horizontalLine() rune { return '─' }
type doubleLineBoxChars struct{}
func (doubleLineBoxChars) upperLeftCorner() rune { return '╔' }
func (doubleLineBoxChars) upperRightCorner() rune { return '╗' }
func (doubleLineBoxChars) lowerLeftCorner() rune { return '╚' }
func (doubleLineBoxChars) lowerRightCorner() rune { return '╝' }
func (doubleLineBoxChars) verticalLine() rune { return '║' }
func (doubleLineBoxChars) horizontalLine() rune { return '═' }
type heavyLineBoxChars struct{}
func (heavyLineBoxChars) upperLeftCorner() rune { return '┏' }
func (heavyLineBoxChars) upperRightCorner() rune { return '┓' }
func (heavyLineBoxChars) lowerLeftCorner() rune { return '┗' }
func (heavyLineBoxChars) lowerRightCorner() rune { return '┛' }
func (heavyLineBoxChars) verticalLine() rune { return '┃' }
func (heavyLineBoxChars) horizontalLine() rune { return '━' }
type lightLineBoxChars struct{}
func (lightLineBoxChars) upperLeftCorner() rune { return '┌' }
func (lightLineBoxChars) upperRightCorner() rune { return '┐' }
func (lightLineBoxChars) lowerLeftCorner() rune { return '└' }
func (lightLineBoxChars) lowerRightCorner() rune { return '┘' }
func (lightLineBoxChars) verticalLine() rune { return '│' }
func (lightLineBoxChars) horizontalLine() rune { return '─' }
// FlowerBoxStyle specifies a style of drawing flower box borders
type FlowerBoxStyle uint32
const (
// ASCIIFlowerBox uses ASCII characters ('+', '+', '+', '+', '-', and '|')
ASCIIFlowerBox = iota
// CurvedFlowerBox is uses light lines rounded corners ('╭', '╮', '╰', '╯', '─', and '│')
CurvedFlowerBox
// DoubleLinedFlowerBox uses double line characters ('╔', '╗', '╚', '╝', '═', and '║')
DoubleLinedFlowerBox
// HeavyLinedFlowerBox uses heavy lined characters ('┏', '┓', '┗', '┛', '━', and '┃')
HeavyLinedFlowerBox
// LightLinedFlowerBox uses heavy lined characters ('┌', '┐', '└', '┘', '─', and '│')
LightLinedFlowerBox
)
func getBoxChars(style FlowerBoxStyle) boxChars {
switch style {
case CurvedFlowerBox:
return curvedBoxChars{}
case DoubleLinedFlowerBox:
return doubleLineBoxChars{}
case HeavyLinedFlowerBox:
return heavyLineBoxChars{}
case LightLinedFlowerBox:
return lightLineBoxChars{}
default:
return asciiBoxChars{}
}
}
// StyledFlowerBox draws a box around the provided slice of strings in a specified style
func StyledFlowerBox(lines []string, style FlowerBoxStyle) []string {
maxRunesPerLine := 0
for _, s := range lines {
maxRunesPerLine = max(maxRunesPerLine, len([]rune(s)))
}
bc := getBoxChars(style)
headerRunes := make([]rune, maxRunesPerLine+4)
headerRunes[0] = bc.upperLeftCorner()
for i := 1; i < maxRunesPerLine+3; i++ {
headerRunes[i] = bc.horizontalLine()
}
headerRunes[maxRunesPerLine+3] = bc.upperRightCorner()
// size: 2 for horizontal lines + 1 for empty string at the end + 1 per line
formattedLines := make([]string, 3+len(lines))
formattedLines[0] = string(headerRunes)
for index, s := range lines {
formattedLines[index+1] = fmt.Sprintf(
"%c %s%*s %c",
bc.verticalLine(),
s,
maxRunesPerLine-len([]rune(s)),
"",
bc.verticalLine(),
)
}
footerRunes := make([]rune, maxRunesPerLine+4)
footerRunes[0] = bc.lowerLeftCorner()
for i := 1; i < maxRunesPerLine+3; i++ {
footerRunes[i] = bc.horizontalLine()
}
footerRunes[maxRunesPerLine+3] = bc.lowerRightCorner()
formattedLines[len(lines)+1] = string(footerRunes)
formattedLines[len(lines)+2] = ""
return formattedLines
}
func translateTimestamp(s string) string {
t, parseErr := time.Parse(time.RFC3339, s)
if parseErr != nil {
return s
}
// https://github.com/majohn-r/cmd-toolkit/issues/18
return t.Format("Monday, January 2 2006, 15:04:05 -0700")
}
// DecoratedAppName returns the app name with its version and build timestamp; see
// https://github.com/majohn-r/cmd-toolkit/issues/17
func DecoratedAppName(applicationName, applicationVersion, timestamp string) string {
return fmt.Sprintf("%s version %s, built on %s", applicationName, applicationVersion,
translateTimestamp(timestamp))
}
// Copyright returns an appropriately formatted copyright statement; see
// https://github.com/majohn-r/cmd-toolkit/issues/17
func Copyright(o output.Bus, first int, timestamp, owner string) string {
return formatCopyright(first, finalYear(o, timestamp, first), owner)
}