-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.go
52 lines (47 loc) · 1.23 KB
/
timeline.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
package main
import (
"encoding/json"
"github.com/0xThiebaut/mdeproxy/internal/times"
"github.com/spf13/cobra"
"time"
)
func timeline() (*cobra.Command, error) {
now := time.Now().UTC()
from := now.AddDate(0, -6, 0).Add(time.Minute).Format(times.Layout)
to := now.Format(times.Layout)
var machine string
command := &cobra.Command{
Use: "timeline",
Short: "Export a device's timeline",
RunE: func(cmd *cobra.Command, args []string) error {
f, err := time.Parse(times.Layout, from)
if err != nil {
return err
}
t, err := time.Parse(times.Layout, to)
if err != nil {
return err
}
encoder := json.NewEncoder(output)
events := client.Timeline(cmd.Context(), f, t, machine)
for {
select {
case <-cmd.Context().Done():
return cmd.Context().Err()
case event, ok := <-events:
if !ok {
return client.Error()
}
if err = encoder.Encode(event); err != nil {
return err
}
}
}
},
}
flags := command.PersistentFlags()
flags.StringVar(&from, "from", from, "start date")
flags.StringVar(&to, "to", to, "end date")
flags.StringVarP(&machine, "machine", "m", machine, "machine identifier")
return command, command.MarkPersistentFlagRequired("machine")
}