-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
164 lines (138 loc) · 4.03 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"os"
"strings"
"time"
"github.com/cactus/go-statsd-client/v5/statsd"
"github.com/charmbracelet/log"
"github.com/pkg/errors"
"github.com/streamdal/streamdal/apps/cli/cmd"
"github.com/streamdal/streamdal/apps/cli/config"
"github.com/streamdal/streamdal/apps/cli/console"
"github.com/streamdal/streamdal/apps/cli/manage"
"github.com/streamdal/streamdal/apps/cli/telemetry"
"github.com/streamdal/streamdal/apps/cli/types"
"github.com/streamdal/streamdal/apps/cli/util"
)
var (
VERSION = "unset"
)
func main() {
// Read CLI args
cfg := config.New(VERSION)
logger := log.Default()
if cfg.EnableFileLogging {
f, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatalf("unable to open log file: %s", err)
}
util.RedirectStdErr(f)
logger.SetOutput(f)
logger.SetFormatter(log.JSONFormatter)
}
if cfg.Debug {
logger.SetLevel(log.DebugLevel)
logger.SetReportCaller(true)
}
var t statsd.Statter
if !cfg.TelemetryDisable {
statsdClient, err := statsd.NewClientWithConfig(&statsd.ClientConfig{
Address: cfg.TelemetryAddress,
Prefix: "streamdal",
UseBuffered: true,
FlushInterval: time.Second,
TagFormat: statsd.InfixSemicolon,
})
if err != nil {
log.Errorf("unable to create new statsd client: %s", err)
t = &telemetry.DummyTelemetry{}
} else {
t = statsdClient
}
} else {
t = &telemetry.DummyTelemetry{}
}
// Send telemetry
_ = t.Gauge(types.GaugeArgsNum, int64(len(cfg.KongContext.Args)), 1.0, cfg.GetStatsdTags()...)
_ = t.Inc(types.CounterExecTotal, 1, 1.0, cfg.GetStatsdTags()...)
command := cfg.KongContext.Command()
switch {
case command == "cli":
handleCLICommand(cfg, logger, t)
case strings.HasPrefix(command, "manage"):
handleManageCommand(cfg, logger, t)
default:
util.ReportErrorAndExit(t, cfg, errors.Errorf("unknown command: %s", command))
}
}
func handleCLICommand(cfg *config.Config, logger *log.Logger, t statsd.Statter) {
if cfg == nil {
util.ReportErrorAndExit(t, cfg, errors.New("config cannot be nil"))
}
if logger == nil {
util.ReportErrorAndExit(t, cfg, errors.New("logger cannot be nil"))
}
if t == nil {
util.ReportErrorAndExit(t, cfg, errors.New("telemetry cannot be nil"))
}
// Initialize console components
ui, err := console.New(&console.Options{
Config: cfg,
Logger: logger,
})
if err != nil {
util.ReportErrorAndExit(t, cfg, errors.Wrap(err, "unable to initialize console"))
}
// Initialize cmd which houses business logic
c, err := cmd.New(&cmd.Options{
Config: cfg,
Console: ui,
Logger: logger,
Telemetry: t,
})
if err != nil {
util.ReportErrorAndExit(t, cfg, errors.Wrap(err, "unable to initialize cmd"))
}
// Do the dance
if err := c.Run(); err != nil {
util.ReportErrorAndExit(t, cfg, errors.Wrap(err, "error during cmd run"))
}
}
func handleManageCommand(cfg *config.Config, logger *log.Logger, t statsd.Statter) {
if cfg == nil {
util.ReportErrorAndExit(t, cfg, errors.New("config cannot be nil"))
}
if logger == nil {
util.ReportErrorAndExit(t, cfg, errors.New("logger cannot be nil"))
}
if t == nil {
util.ReportErrorAndExit(t, cfg, errors.New("telemetry cannot be nil"))
}
m, err := manage.New(cfg, logger, t)
if err != nil {
util.ReportErrorAndExit(t, cfg, errors.Wrap(err, "unable to initialize manage"))
}
switch cfg.KongContext.Command() {
case "manage create pipeline":
err = m.CreatePipeline(cfg)
case "manage create wasm":
err = m.CreateWasm(cfg)
case "manage get pipeline":
err = m.GetPipeline(cfg)
case "manage get wasm":
err = m.GetWasm(cfg)
case "manage delete pipeline":
err = m.DeletePipeline(cfg)
case "manage delete wasm":
err = m.DeleteWasm(cfg)
case "manage update pipeline":
err = m.UpdatePipeline(cfg)
case "manage update wasm":
err = m.UpdateWasm(cfg)
default:
util.ReportErrorAndExit(t, cfg, errors.Errorf("unknown manage command: %s", cfg.KongContext.Command()))
}
if err != nil {
util.ReportErrorAndExit(t, cfg, errors.Wrap(err, "error during manage command"))
}
}