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
/
logs.go
226 lines (202 loc) · 5.02 KB
/
logs.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
// Package logs handles errors and user feedback.
package logs
import (
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/Defacto2/df2/pkg/logs/internal/terminal"
"github.com/gookit/color"
gap "github.com/muesli/go-app-paths"
)
var ErrNoArg = errors.New("no arguments were provided")
const (
GapUser string = "df2" // GapUser is the configuration and logs subdirectory name.
Filename string = "errors.log" // Filename is the default error log filename.
dmode os.FileMode = 0o700
fmode os.FileMode = 0o600
flags int = log.Ldate | log.Ltime | log.LUTC
newmode int = os.O_APPEND | os.O_CREATE | os.O_WRONLY
)
var (
panicErr = false //nolint:gochecknoglobals
quiet = false //nolint:gochecknoglobals
)
// Panic enables or disables panicking when Danger is used.
func Panic(b bool) {
panicErr = b
}
// Quiet stops most writing to the standard output.
func Quiet(b bool) {
quiet = b
}
func IsQuiet() bool {
return quiet
}
// Arg returns instructions for invalid command arguments and exits with an error code.
func Arg(arg string, exit bool, args ...string) error {
if args == nil {
return fmt.Errorf("arg: %w", ErrNoArg)
}
fmt.Printf("%s %s %s\n",
color.Warn.Sprint("invalid command"),
color.Bold.Sprintf("\"%s %s\"", arg, args[0]),
color.Warn.Sprint("\nplease use one of the Available Commands shown above"))
if !exit {
return nil
}
os.Exit(1)
return nil
}
// Danger logs the error to stdout, but continues the program.
func Danger(err error) {
switch panicErr {
case true:
log.Println(fmt.Sprintf("error type %T\t: %v", err, err))
log.Panic(err)
default:
log.Printf("%s %s", color.Danger.Sprint("!"), err)
}
}
// Fatal logs error to stdout and exits with an error code.
func Fatal(err error) {
save(Filename, err)
switch panicErr {
case true:
log.Println(fmt.Sprintf("error type: %T\tmsg: %v", err, err))
log.Panic(err)
default:
log.Fatal(color.Danger.Sprint("ERROR: "), err)
}
}
// Filepath is the absolute path and filename of the error log file.
func Filepath(filename string) string {
fp, err := gap.NewScope(gap.User, GapUser).LogPath(filename)
if err != nil {
h, err := os.UserHomeDir()
if err != nil {
Log(err)
}
return path.Join(h, filename)
}
return fp
}
// Log the error to stdout, but continue the program.
func Log(err error) {
save(Filename, err)
Danger(err)
}
// Path returns the named file or directory path with all missing elements marked in red.
func Path(name string) string {
a := strings.Split(name, "/")
var p, s string
for i, e := range a {
if e == "" {
s = "/"
continue
}
p = strings.Join(a[0:i+1], "/")
if _, err := os.Stat(p); os.IsNotExist(err) {
s = filepath.Join(s, color.Danger.Sprint(e))
} else {
s = filepath.Join(s, e)
}
}
return fmt.Sprint(s)
}
// Print obeys the --quiet flag
// or formats using the default formats for its operands and writes to standard output.
func Print(a ...any) {
if quiet {
return
}
if _, err := fmt.Print(a...); err != nil {
fatalLog(err)
}
}
// Printcr obeys the --quiet flag
// or otherwise erases the current line and writes to standard output.
func Printcr(a ...any) {
if quiet {
return
}
if _, err := fmt.Printf("\r%s\r", strings.Repeat(" ", int(terminal.Size()))); err != nil {
fatalLog(err)
}
if _, err := fmt.Print(a...); err != nil {
fatalLog(err)
}
}
// Printf obeys the --quiet flag
// or formats according to a format specifier and writes to standard output.
func Printf(format string, a ...any) {
if quiet {
return
}
if _, err := fmt.Printf(format, a...); err != nil {
fatalLog(err)
}
}
// Println obeys the --quiet flag or formats using the default formats for its operands and writes to standard output.
func Println(a ...any) {
if quiet {
return
}
if _, err := fmt.Println(a...); err != nil {
fatalLog(err)
}
}
// Printcrf obeys the --quiet flag
// or otherwise erases the current line and formats according to a format specifier.
func Printcrf(format string, a ...any) {
if quiet {
return
}
if _, err := fmt.Printf("\r%s\r%s",
strings.Repeat(" ", int(terminal.Size())),
fmt.Sprintf(format, a...)); err != nil {
fatalLog(err)
}
}
// Simulate prints the --simulate=false flag info.
func Simulate() {
Println(color.Notice.Sprint("use the --simulate=false flag to apply these fixes"))
}
func fatal(err error) {
log.Printf("%s %s", color.Danger.Sprint("!"), err)
os.Exit(1)
}
func fatalLog(err error) {
save(Filename, err)
fatal(err)
}
// save an error to the logs.
// path is available for unit tests.
func save(filename string, err error) (ok bool) {
if err == nil {
return false
}
// use UTC date and times in the log file
log.SetFlags(flags)
f := Filepath(filename)
p := filepath.Dir(f)
if _, err1 := os.Stat(p); os.IsNotExist(err1) {
if err2 := os.MkdirAll(p, dmode); err != nil {
fatal(err2)
}
} else if err1 != nil {
fatal(err1)
}
file, err1 := os.OpenFile(f, newmode, fmode)
if err1 != nil {
fatal(err1)
}
defer file.Close()
log.SetOutput(file)
log.Print(err)
log.SetOutput(os.Stderr)
return true
}