-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
66 lines (55 loc) · 1.38 KB
/
command.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
package main
import (
"context"
"github.com/0xThiebaut/mdeproxy/lib"
"github.com/spf13/cobra"
"io"
"log"
"os"
"os/signal"
)
var client lib.Client
var output io.WriteCloser = os.Stdout
func main() {
var cookie string
var xsrf string
var path string
cmd := cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if len(path) > 0 {
if output, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644); err != nil {
return err
}
}
client, err = lib.New(cookie, xsrf)
return err
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
return output.Close()
},
}
cmd.PersistentFlags().StringVar(&cookie, "cookie", cookie, "cookie for the Defender API proxy")
if err := cmd.MarkPersistentFlagRequired("cookie"); err != nil {
log.Fatal(err)
}
cmd.PersistentFlags().StringVar(&xsrf, "xsrf", xsrf, "XSRF token for the Defender API proxy")
if err := cmd.MarkPersistentFlagRequired("xsrf"); err != nil {
log.Fatal(err)
}
cmd.PersistentFlags().StringVar(&path, "output", path, "output file")
sub, err := timeline()
if err != nil {
log.Fatal(err)
}
cmd.AddCommand(sub)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-c
cancel()
}()
if err = cmd.ExecuteContext(ctx); err != nil {
log.Fatal(err)
}
}