This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
324 lines (304 loc) · 8.15 KB
/
cmd.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Package cmd uses the Cobra package to provide and handle all the interactions
// with the command-line interface in Linux.
package cmd
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"text/template"
"time"
"github.com/Defacto2/df2/pkg/conf"
"github.com/Defacto2/df2/pkg/database"
"github.com/carlmjohnson/versioninfo"
"github.com/gookit/color"
"go.uber.org/zap"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var (
ErrNoOutput = errors.New("no output command used")
ErrPointer = errors.New("pointer value cannot be nil")
)
// ProgData is used for holding the version flag template data.
type ProgData struct {
Database string
Ansilove string
Webp string
Magick string
Netpbm string
PngQuant string
Arj string
File string
Lha string
UnRar string
UnZip string
ZipInfo string
Version string
Revision string
LastCommit string
Path string
Platform string
GoVer string
GoOS string
Docker string
Title string
Cmd string
}
const (
About = Title + " is the program to manage, maintain and optimise " + Domain + "."
Author = "Ben Garrett" // Author is the primary programmer of this program.
Domain = "defacto2.net" // Domain of the website.
Program = "df2" // Program command.
Title = "The Defacto2 tool" // Title of this program.
URL = "https://github.com/Defacto2/df2" // URL of the program repository.
unknown = "unknown"
)
// Arch returns this program's system architecture.
func Arch() string {
switch strings.ToLower(runtime.GOARCH) {
case "amd64":
return "Intel/AMD 64"
case "arm":
return "ARM 32"
case "arm64":
return "ARM 64"
case "i386":
return "x86"
case "wasm":
return "WebAssembly"
}
return runtime.GOARCH
}
// Brand prints the byte ASCII logo to the stdout.
func Brand(w io.Writer, l *zap.SugaredLogger, b []byte) error {
if l == nil {
return fmt.Errorf("l %w", ErrPointer)
}
if w == nil {
w = io.Discard
}
logo := string(b)
if len(logo) == 0 {
return nil
}
nw := bufio.NewWriter(w)
if _, err := fmt.Fprintf(nw, "%s\n\n", logo); err != nil {
l.Warnf("Could not print the brand logo: %s.", err)
}
nw.Flush()
return nil
}
// Commit returns a formatted, git commit description for this repository,
// including tag version and date.
func Commit(version string) string {
s := ""
c := versioninfo.Short()
if version != "" {
s = fmt.Sprintf("%s ", Vers(version))
} else if c != "" {
s += fmt.Sprintf("version %s, ", c)
}
if s == "" {
return unknown
}
return strings.TrimSpace(s)
}
// Copyright returns the copyright years and author of this program.
func Copyright() string {
const initYear = 2020
t := versioninfo.LastCommit
if t.Year() < initYear {
t = time.Now()
}
s := fmt.Sprintf("© %d", initYear)
if t.Year() > initYear {
s += "-" + t.Local().Format("06")
}
s += fmt.Sprintf(" Defacto2 & %s", Author)
return s
}
// ExeTmpl returns the template for the -version flag.
func ExeTmpl() string {
const tmpl = `
┬───── {{.Title}} ─────┬─────────────────────────────┬
│ │ │
│ requirements │ recommended │
│ │ │
│ database {{.Database}} │ arj {{.Arj}} │
│ │ file magic {{.File}} │
│ ansilove {{.Ansilove}} │ lhasa {{.Lha}} │
│ webp lib {{.Webp}} │ unrar {{.UnRar}} │
│ imagemagick {{.Magick}} │ unzip {{.UnZip}} │
│ netpbm {{.Netpbm}} │ zipinfo {{.ZipInfo}} │
│ pngquant {{.PngQuant}} │ │
│ │ │
┴─────────────────────────────┴─────────────────── {{.Cmd}} ─────┴
version {{.Version}}
path {{.Path}}
commit {{.Revision}}
date {{.LastCommit}}
go v{{.GoVer}} {{.GoOS}}{{.Docker}}
`
return tmpl
}
// LastCommit returns the time and date of the last repo commit.
func LastCommit() string {
d := versioninfo.LastCommit
if d.IsZero() {
return unknown
}
return d.Local().Format("2006 Jan 2 15:04")
}
// OS returns this program's operating system.
func OS() string {
t := cases.Title(language.English)
os := strings.Split(runtime.GOOS, "/")[0]
switch os {
case "darwin":
return "macOS"
case "freebsd":
return "FreeBSD"
case "js":
return "JS"
case "netbsd":
return "NetBSD"
case "openbsd":
return "OpenBSD"
}
return t.String(os)
}
// Vers returns a formatted version.
// The version string is generated by GoReleaser.
func Vers(version string) string {
const alpha, beta, dev = "\u03b1", "β", "0.0.0"
if version == "" || version == dev {
return fmt.Sprintf("v%s %slpha (developer build)", dev, alpha)
}
const next = "-next"
if strings.HasSuffix(version, next) {
return fmt.Sprintf("v%s %seta", strings.TrimSuffix(version, next), beta)
}
return version
}
// ProgInfo returns the response for the -version flag.
func ProgInfo(logr *zap.SugaredLogger, cfg conf.Config, version string) (string, error) {
if logr == nil {
return "", fmt.Errorf("logr %w", ErrPointer)
}
bin, err := conf.BinPath()
if err != nil {
bin = fmt.Sprint(err)
}
l, err := check(logr, cfg)
if err != nil {
return "", err
}
data := ProgData{
Database: colorize(l["db"]),
Ansilove: colorize(l["ansilove"]),
Webp: colorize(l["cwebp"]),
Magick: colorize(l["convert"]),
Netpbm: colorize(l["pnmtopng"]),
PngQuant: colorize(l["pngquant"]),
Arj: colorize(l["arj"]),
File: colorize(l["file"]),
Lha: colorize(l["lha"]),
UnRar: colorize(l["unrar"]),
UnZip: colorize(l["unzip"]),
ZipInfo: colorize(l["zipinfo"]),
Version: Commit(version),
Revision: versioninfo.Revision,
LastCommit: LastCommit(),
Path: bin,
GoVer: strings.Replace(runtime.Version(), "go", "", 1),
GoOS: OS(),
Docker: dockerBuild(),
Title: color.Bold.Sprint(color.Primary.Sprint(Title)),
Cmd: color.Primary.Sprint("df2"),
}
tmpl, err := template.New("checks").Parse(ExeTmpl())
if err != nil {
return "", fmt.Errorf("proginfo tmpl: %w", err)
}
var b bytes.Buffer
if err := tmpl.Execute(&b, data); err != nil {
return "", fmt.Errorf("proginfo exec: %w", err)
}
return b.String(), nil
}
type lookups = map[string]string
// check looks up the collection of dependencies and database connection.
func check(logr *zap.SugaredLogger, cfg conf.Config) (lookups, error) {
if logr == nil {
return nil, fmt.Errorf("logr %w", ErrPointer)
}
const (
disconnect = "disconnect"
ok = "ok"
miss = "missing"
db = "db"
)
l := lookups{
"db": ok,
"ansilove": miss,
"cwebp": miss,
"convert": miss,
"pnmtopng": miss,
"pngquant": miss,
"arj": miss,
"file": miss,
"lha": miss,
"unrar": miss,
"unzip": miss,
"zipinfo": miss,
}
bug, err := database.ConnDebug(cfg)
if err != nil {
logr.Error(err)
l[db] = disconnect
}
if bug != "" {
logr.Info(bug)
l[db] = disconnect
}
for file := range l {
if file == db {
continue
}
if _, err := exec.LookPath(file); err == nil {
l[file] = ok
}
}
return l, nil
}
// colorize applies color to s.
func colorize(s string) string {
const (
disconnect = "disconnect"
ok = "ok"
miss = "missing"
)
padding := 11
switch s {
case ok:
padding = 9
return color.Success.Sprint("okay") + strings.Repeat(" ", padding-len(s))
case miss:
return color.Error.Sprint(miss) + strings.Repeat(" ", padding-len(s))
case disconnect:
return color.Error.Sprint("disconnect") + strings.Repeat(" ", padding-len(s))
}
return ""
}
func dockerBuild() string {
if _, ok := os.LookupEnv("DF2_HOST"); ok {
return " in a Docker container"
}
return ""
}