-
Notifications
You must be signed in to change notification settings - Fork 15
/
history.go
63 lines (53 loc) · 1.28 KB
/
history.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
package cmd
import (
"context"
"encoding/json"
"os"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
)
var historyCount int
var followMode bool
// historyCmd represents the history command
var historyCmd = &cobra.Command{
Use: "history",
Short: "show the hostname of the current history process",
Long: `Show the hostname of the current history process.`,
RunE: func(cmd *cobra.Command, args []string) error {
well.Go(func(ctx context.Context) error {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if followMode {
recordCh, err := storage.WatchRecords(ctx, int64(historyCount))
if err != nil {
return err
}
for r := range recordCh {
err := enc.Encode(r)
if err != nil {
return err
}
}
return nil
}
records, err := storage.GetRecords(ctx, int64(historyCount))
if err != nil {
return err
}
for _, r := range records {
err = enc.Encode(r)
if err != nil {
return err
}
}
return nil
})
well.Stop()
return well.Wait()
},
}
func init() {
historyCmd.Flags().IntVarP(&historyCount, "count", "n", 0, "limit the number of operations to show")
historyCmd.Flags().BoolVarP(&followMode, "follow", "f", false, "show operations continuously")
rootCmd.AddCommand(historyCmd)
}