forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_logs.go
85 lines (70 loc) · 1.84 KB
/
command_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
package commands
import (
"os"
"path/filepath"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/spf13/cobra"
)
func logsCommand(cmd *cobra.Command, args []string) {
for _, path := range sortedLogs() {
Print(path)
}
}
func logsLastCommand(cmd *cobra.Command, args []string) {
logs := sortedLogs()
if len(logs) < 1 {
Print(tr.Tr.Get("No logs to show"))
return
}
logsShowCommand(cmd, logs[len(logs)-1:])
}
func logsShowCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Print(tr.Tr.Get("Supply a log name."))
return
}
name := args[0]
by, err := os.ReadFile(filepath.Join(cfg.LocalLogDir(), name))
if err != nil {
Exit(tr.Tr.Get("Error reading log: %s", name))
}
Debug(tr.Tr.Get("Reading log: %s", name))
os.Stdout.Write(by)
}
func logsClearCommand(cmd *cobra.Command, args []string) {
err := os.RemoveAll(cfg.LocalLogDir())
if err != nil {
Panic(err, tr.Tr.Get("Error clearing %s", cfg.LocalLogDir()))
}
Print(tr.Tr.Get("Cleared %s", cfg.LocalLogDir()))
}
func logsBoomtownCommand(cmd *cobra.Command, args []string) {
Debug(tr.Tr.Get("Sample debug message"))
err := errors.Wrapf(errors.New(tr.Tr.Get("Sample wrapped error message")), tr.Tr.Get("Sample error message"))
Panic(err, tr.Tr.Get("Sample panic message"))
}
func sortedLogs() []string {
fileinfos, err := os.ReadDir(cfg.LocalLogDir())
if err != nil {
return []string{}
}
names := make([]string, 0, len(fileinfos))
for _, info := range fileinfos {
if info.IsDir() {
continue
}
names = append(names, info.Name())
}
return names
}
func init() {
RegisterCommand("logs", logsCommand, func(cmd *cobra.Command) {
cmd.AddCommand(
NewCommand("last", logsLastCommand),
NewCommand("show", logsShowCommand),
NewCommand("clear", logsClearCommand),
NewCommand("boomtown", logsBoomtownCommand),
)
})
}