/
api.go
72 lines (64 loc) · 1.74 KB
/
api.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
package main
import (
"fmt"
"github.com/go-faster/errors"
"github.com/spf13/cobra"
"github.com/go-faster/oteldb/internal/otelbench"
"github.com/go-faster/oteldb/internal/otelbotapi"
)
func newPingCommand(cfg *apiSettings) *cobra.Command {
cmd := &cobra.Command{
Use: "ping",
Short: "Send ping request to otelbench api",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cfg.Client()
if err != nil {
return errors.Wrap(err, "failed to create otelbotapi client")
}
if err := client.Ping(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to ping otelbench api")
}
fmt.Println("Ping successful")
return nil
},
}
return cmd
}
func newStatusCommand(cfg *apiSettings) *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Get status of otelbench api",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cfg.Client()
if err != nil {
return errors.Wrap(err, "failed to create otelbotapi client")
}
status, err := client.GetStatus(cmd.Context())
if err != nil {
return errors.Wrap(err, "failed to get status of otelbench api")
}
fmt.Println("Status:", status.Status)
return nil
},
}
return cmd
}
type apiSettings struct {
httpAddr string
}
func (a *apiSettings) Client() (*otelbotapi.Client, error) {
return otelbotapi.NewClient(a.httpAddr, otelbench.EnvSecuritySource())
}
func newAPICommand() *cobra.Command {
cfg := &apiSettings{}
cmd := &cobra.Command{
Use: "api",
Short: "Commands to interact with otelbench api",
}
cmd.PersistentFlags().StringVar(&cfg.httpAddr, "http-addr", "https://bot.oteldb.tech", "http address of otelbench api")
cmd.AddCommand(
newPingCommand(cfg),
newStatusCommand(cfg),
)
return cmd
}