-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.38 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"strings"
"github.com/vic3lord/bufile/config"
"github.com/vic3lord/bufile/route"
)
var (
configFile = flag.String("config", "bufile.json", "Path to config file")
apply = flag.Bool("apply", false, "Apply the generated routes")
)
func run(ctx context.Context, cfg config.Config, w io.Writer) error {
var errs error
opts := route.Options{
IncludeServiceName: cfg.IncludeServiceName,
}
for _, mod := range cfg.Modules {
err := route.Generate(ctx, mod, w, opts)
if err != nil {
moderr := fmt.Errorf("generate routes for module %q: %w", mod, err)
errs = errors.Join(errs, moderr)
}
}
return errs
}
func main() {
flag.Parse()
cfg, err := config.Parse(*configFile)
if err != nil {
slog.Error("Parse config", slog.String("err", err.Error()))
os.Exit(1)
}
var sb strings.Builder
ctx := context.Background()
if err := run(ctx, cfg, &sb); err != nil {
slog.Error("Generate routes", slog.String("err", err.Error()))
os.Exit(1)
}
if *apply {
cmd := exec.Command("kubectl", "apply", "-f", "-")
cmd.Stdin = strings.NewReader(sb.String())
out, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Apply routes", slog.String("output", string(out)), slog.String("err", err.Error()))
os.Exit(1)
}
fmt.Print(string(out))
os.Exit(0)
}
fmt.Print(sb.String())
}