-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
101 lines (82 loc) · 2.11 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"fmt"
stdlog "log"
"os"
"os/signal"
"syscall"
kitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/alexeldeib/incli/client"
)
func main() {
if err := NewRootCommand().Execute(); err != nil {
fmt.Printf("%+#v\n", err)
os.Exit(1)
}
}
func setup() (context.Context, kitlog.Logger, *client.ClientWithResponses, error) {
ctx, cancel := context.WithCancel(context.Background())
logger := kitlog.NewLogfmtLogger(kitlog.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, level.AllowInfo())
logger = kitlog.With(logger, "timestamp", kitlog.DefaultTimestampUTC, "caller", kitlog.DefaultCaller)
stdlog.SetOutput(kitlog.NewStdlibAdapter(logger))
// Setup signal handling.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
<-sigc
cancel()
<-sigc
logger.Log("msg", "received second signal, exiting immediately")
os.Exit(1)
}()
apiKey := os.Getenv("INC_API_KEY")
if apiKey == "" {
return nil, nil, nil, fmt.Errorf("INC_API_KEY must be set")
}
cl, err := client.New(ctx, apiKey, "https://api.incident.io", "ace-cohere-cli")
if err != nil {
return nil, nil, nil, errors.Wrap(err, "creating client")
}
return ctx, logger, cl, nil
}
func NewRootCommand() *cobra.Command {
root := &cobra.Command{
Use: "inc",
}
root.AddCommand()
root.AddCommand(NewIncidentsCommand())
root.AddCommand(NewCatalogCommand())
return root
}
func NewCatalogCommand() *cobra.Command {
root := &cobra.Command{
Use: "catalog",
}
entries := &cobra.Command{
Use: "entries",
}
types := &cobra.Command{
Use: "types",
}
root.AddCommand()
root.AddCommand(entries)
root.AddCommand(types)
entries.AddCommand(NewGetCatalogEntriesCommand())
types.AddCommand(NewGetCatalogTypesCommand())
return root
}
func NewIncidentsCommand() *cobra.Command {
root := &cobra.Command{
Use: "incidents",
Aliases: []string{"inc"},
}
root.AddCommand()
root.AddCommand(NewGetIncidentCommand())
root.AddCommand(NewPatchIncidentsCommand())
return root
}