| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/errors" |
| 8 | "github.com/git-lfs/git-lfs/v3/tr" |
| 9 | "github.com/rubyist/tracerx" |
| 10 | "github.com/spf13/cobra" |
| 11 | ) |
| 12 | |
| 13 | func logsCommand(cmd *cobra.Command, args []string) { |
| 14 | for _, path := range sortedLogs() { |
| 15 | Print(path) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func logsLastCommand(cmd *cobra.Command, args []string) { |
| 20 | logs := sortedLogs() |
| 21 | if len(logs) < 1 { |
| 22 | Print(tr.Tr.Get("No logs to show")) |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | logsShowCommand(cmd, logs[len(logs)-1:]) |
| 27 | } |
| 28 | |
| 29 | func logsShowCommand(cmd *cobra.Command, args []string) { |
| 30 | if len(args) == 0 { |
| 31 | Print(tr.Tr.Get("Supply a log name.")) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | name := args[0] |
| 36 | by, err := os.ReadFile(filepath.Join(cfg.LocalLogDir(), name)) |
| 37 | if err != nil { |
| 38 | Exit(tr.Tr.Get("Error reading log: %s", name)) |
| 39 | } |
| 40 | |
| 41 | tracerx.Printf("Reading log: %s", name) |
| 42 | os.Stdout.Write(by) |
| 43 | } |
| 44 | |
| 45 | func logsClearCommand(cmd *cobra.Command, args []string) { |
| 46 | err := os.RemoveAll(cfg.LocalLogDir()) |
| 47 | if err != nil { |
| 48 | Panic(err, tr.Tr.Get("Error clearing %s", cfg.LocalLogDir())) |
| 49 | } |
| 50 | |
| 51 | Print(tr.Tr.Get("Cleared %s", cfg.LocalLogDir())) |
| 52 | } |
| 53 | |
| 54 | func logsBoomtownCommand(cmd *cobra.Command, args []string) { |
| 55 | tracerx.Printf("Sample trace message") |
| 56 | err := errors.Wrap(errors.New(tr.Tr.Get("Sample wrapped error message")), tr.Tr.Get("Sample error message")) |
| 57 | Panic(err, tr.Tr.Get("Sample panic message")) |
| 58 | } |
| 59 | |
| 60 | func sortedLogs() []string { |
| 61 | fileinfos, err := os.ReadDir(cfg.LocalLogDir()) |
| 62 | if err != nil { |
| 63 | return []string{} |
| 64 | } |
| 65 | |
| 66 | names := make([]string, 0, len(fileinfos)) |
| 67 | for _, info := range fileinfos { |
| 68 | if info.IsDir() { |
| 69 | continue |
| 70 | } |
| 71 | names = append(names, info.Name()) |
| 72 | } |
| 73 | |
| 74 | return names |
| 75 | } |
| 76 | |
| 77 | func init() { |
| 78 | RegisterCommand("logs", logsCommand, func(cmd *cobra.Command) { |
| 79 | cmd.AddCommand( |
| 80 | NewCommand("last", logsLastCommand), |
| 81 | NewCommand("show", logsShowCommand), |
| 82 | NewCommand("clear", logsClearCommand), |
| 83 | NewCommand("boomtown", logsBoomtownCommand), |
| 84 | ) |
| 85 | }) |
| 86 | } |