forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_logs.go
58 lines (48 loc) · 1.15 KB
/
list_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
package cmd
import (
"fmt"
"github.com/bpicode/fritzctl/assert"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/logger"
"github.com/fatih/color"
"github.com/mitchellh/cli"
)
var (
blue = color.New(color.Bold, color.FgBlue)
)
type listLogsCommand struct {
}
func (cmd *listLogsCommand) Help() string {
return "List the log statements/events from the FRITZ!Box. Logs may be subject to log rotation by the FRITZ!Box."
}
func (cmd *listLogsCommand) Synopsis() string {
return "list recent FRITZ!BOX logs"
}
func (cmd *listLogsCommand) Run(args []string) int {
c := clientLogin()
f := fritz.New(c)
logs, err := f.ListLogs()
assert.NoError(err, "cannot obtain logs:", err)
logger.Success("Obtained log messages:")
printLogs(logs)
return 0
}
func printLogs(logs *fritz.MessageLog) {
for _, m := range logs.Messages {
printLog(&m)
}
}
func printLog(m *fritz.Message) {
text := (*m)[0]
if len(text) >= 17 {
blue.Print(text[:17])
fmt.Println(text[17:])
} else {
fmt.Println(text)
}
}
// ListLogs is a factory creating commands for commands listing logs.
func ListLogs() (cli.Command, error) {
p := listLogsCommand{}
return &p, nil
}