-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (69 loc) · 1.61 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
//go:generate go tool buf generate
//go:generate go tool mockery
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"runtime/debug"
"syscall"
"github.com/spf13/cobra"
"github.com/davidsbond/orca/cmd/controller"
"github.com/davidsbond/orca/cmd/workflow"
"github.com/davidsbond/orca/internal/log"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT)
defer cancel()
var version string
info, ok := debug.ReadBuildInfo()
if ok {
version = info.Main.Version
}
var (
logLevel string
)
cmd := &cobra.Command{
Use: "orca",
Short: "A simple distributed workflow orchestrator",
Version: version,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: parseLogLevel(logLevel),
}))
ctx = log.ToContext(ctx, logger)
cmd.SetContext(ctx)
},
}
cmd.AddCommand(
controller.Command(),
workflow.Command(),
)
flags := cmd.PersistentFlags()
flags.StringVar(&logLevel, "log-level", "error", "Log level (debug, info, warn, error)")
if err := cmd.ExecuteContext(ctx); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func parseLogLevel(input string) slog.Level {
switch input {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelError
}
}