-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter.go
89 lines (78 loc) · 1.89 KB
/
letter.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
package cmd
import (
"fmt"
"github.com/adamyordan/postbox/postbox"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strconv"
"time"
)
var (
letterCmd = &cobra.Command{
Use: "letter",
Short: "manage dumped requests",
}
letterViewCmd = &cobra.Command{
Use: "view",
Short: "view a request",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
id, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Fatalf("error parsing ID: %v", err)
}
view(uint64(id))
},
}
letterListCmd = &cobra.Command{
Use: "list",
Short: "list all dumped requests",
Run: func(cmd *cobra.Command, args []string) {
list()
},
}
letterClearCmd = &cobra.Command{
Use: "clear",
Short: "remove all dumped requests",
Run: func(cmd *cobra.Command, args []string) {
clear()
},
}
)
func init() {
rootCmd.AddCommand(letterCmd)
letterCmd.AddCommand(letterViewCmd)
letterCmd.AddCommand(letterListCmd)
letterCmd.AddCommand(letterClearCmd)
}
func view(id uint64) {
letter, err := postbox.Get(id)
if err != nil {
log.Fatalf("error getting request with id %d: %v", id, err)
}
displayLetter(letter)
}
func list() {
letters, err := postbox.List()
if err != nil {
log.Fatalf("error listing requests: %v", err)
}
fmt.Printf("Number of dumped requests: %d\n\n", len(letters))
for _, letter := range letters {
displayLetterShort(letter)
}
}
func clear() {
err := postbox.Clear()
if err != nil {
log.Fatalf("error clearing requests from db: %v", err)
}
log.Info("All stored requests cleared")
}
func displayLetter(letter *postbox.Letter) {
fmt.Printf("%-6s: %d\n%-6s: %s\n%-6s: %s\n\n%s\n", "id", letter.ID, "ipaddr", letter.Ipaddr, "time",
time.Unix(letter.Time, 0).String(), letter.Value)
}
func displayLetterShort(letter *postbox.Letter) {
fmt.Printf("[%d] %s (%s)\n", letter.ID, time.Unix(letter.Time, 0).String(), letter.Ipaddr)
}