-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (91 loc) · 2.36 KB
/
Copy pathmain.go
File metadata and controls
107 lines (91 loc) · 2.36 KB
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
102
103
104
105
106
107
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/klev-dev/klev-api-go"
"github.com/klev-dev/klev-api-go/clients"
)
var klient *clients.Clients
func main() {
rootCmd := root()
rootCmd.AddCommand(paths())
rootCmd.AddCommand(publish())
rootCmd.AddCommand(consume())
rootCmd.AddCommand(getByOffset())
rootCmd.AddCommand(receive())
rootCmd.AddCommand(cleanup())
rootCmd.AddCommand(logsRoot())
rootCmd.AddCommand(offsetsRoot())
rootCmd.AddCommand(tokensRoot())
rootCmd.AddCommand(ingressWebhooksRoot())
rootCmd.AddCommand(egressWebhooksRoot())
rootCmd.AddCommand(filtersRoot())
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func root() *cobra.Command {
cmd := &cobra.Command{
Use: "klev",
Short: "cli to interact with klev",
}
authtoken := cmd.PersistentFlags().String("authtoken", "", "token to use for authorization")
base := cmd.PersistentFlags().String("base-url", "", "base url to talk to")
cmd.PersistentFlags().MarkHidden("base-url")
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
var auth string
if token := *authtoken; token != "" {
auth = token
} else if token := os.Getenv("KLEV_TOKEN"); token != "" {
auth = token
} else {
return fmt.Errorf("authtoken is missing. pass with with '--authtoken' or via KLEV_TOKEN env variable. get it from https://dash.klev.dev")
}
cfg := klev.NewConfig(auth)
if cmd.Flags().Changed("base-url") {
cfg.BaseURL = *base
} else if base := os.Getenv("KLEV_URL"); base != "" {
cfg.BaseURL = base
}
klient = clients.New(cfg)
return nil
}
return cmd
}
func paths() *cobra.Command {
return &cobra.Command{
Use: "paths",
Short: "get paths in klev; validate token",
RunE: func(cmd *cobra.Command, args []string) error {
out, err := klient.Paths.Get(cmd.Context())
return output(out, err)
},
}
}
func output(v any, err error) error {
if err != nil {
return outputErr(err)
}
return outputValue(v)
}
func outputErr(err error) error {
if err := klev.GetError(err); err != nil {
if err := outputValueTo(os.Stderr, err); err != nil {
return err
}
os.Exit(1)
}
return err
}
func outputValue(v any) error {
return outputValueTo(os.Stdout, v)
}
func outputValueTo(w io.Writer, v any) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(v)
}