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
/
main.go
167 lines (155 loc) · 3.59 KB
/
main.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
/*
Copyright © 2021 Ben Garrett <code.by.ben@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"text/template"
"time"
"github.com/Defacto2/df2/lib/cmd"
"github.com/Defacto2/df2/lib/database"
"github.com/gookit/color" //nolint:misspell
)
// goreleaser generated ldflags containers.
var (
version = "0.0.0"
commit = "unset" // nolint: gochecknoglobals
date = "unset" // nolint: gochecknoglobals
)
func main() {
// go flag lib
fs := flag.NewFlagSet("df2", flag.ContinueOnError)
ver := fs.Bool("version", false, "version and information for this program")
v := fs.Bool("v", false, "alias for version")
fs.Usage = func() {
// disable go flag help
}
if err := fs.Parse(os.Args[1:]); err != nil {
log.Print(err)
}
if *ver || *v {
app()
info()
return
}
// cobra flag lib
cmd.Execute()
}
func app() {
type Data struct {
Version string
}
const verTmp = `
df2 tool version: {{.Version}}
`
data := Data{
Version: color.Primary.Sprint(version),
}
tmpl, err := template.New("ver").Parse(verTmp)
if err != nil {
log.Fatal(err)
}
var b bytes.Buffer
if err := tmpl.Execute(&b, data); err != nil {
log.Fatal(err)
}
fmt.Println(b.String())
}
func info() {
type Data struct {
Database string
Ansilove string
Webp string
Commit string
Date string
Path string
}
const exeTmp = ` ┌── requirements ──────────┐
│ database: {{.Database}} │
│ ansilove: {{.Ansilove}} │
│ webp lib: {{.Webp}} │
└──────────────────────────┘
commit: {{.Commit}}
date: {{.Date}}
path: {{.Path}}
`
const (
disconnect = "disconnect"
ok = "ok"
miss = "missing"
)
p := func(s string) string {
switch s {
case ok:
const padding = 10
return color.Success.Sprint("okay") + strings.Repeat(" ", padding-len(s))
case miss:
const padding = 12
return color.Error.Sprint(miss) + strings.Repeat(" ", padding-len(s))
case disconnect:
const padding = 10
return color.Error.Sprint("disconnected") + strings.Repeat(" ", padding-len(s))
}
return ""
}
a, w, d, bin := miss, miss, disconnect, ""
if err := database.ConnectInfo(); err == "" {
d = ok
}
if _, err := exec.LookPath("ansilove"); err == nil {
a = ok
}
if _, err := exec.LookPath("cwebp"); err == nil {
w = ok
}
bin, err := self()
if err != nil {
bin = fmt.Sprint(err)
}
data := Data{
Database: p(d),
Ansilove: p(a),
Webp: p(w),
Commit: commit,
Date: localBuild(date),
Path: bin,
}
tmpl, err := template.New("checks").Parse(exeTmp)
if err != nil {
log.Fatal(err)
}
var b bytes.Buffer
if err := tmpl.Execute(&b, data); err != nil {
log.Fatal(err)
}
fmt.Println(b.String())
}
func localBuild(date string) string {
t, err := time.Parse(time.RFC3339, date)
if err != nil {
return date
}
return t.Local().Format("2006 Jan 2, 15:04 MST")
}
func self() (string, error) {
exe, err := os.Executable()
if err != nil {
return "", fmt.Errorf("self error: %w", err)
}
return exe, nil
}