@@ -2,6 +2,7 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "log/slog"
67 "os"
78 "os/signal"
@@ -13,49 +14,57 @@ import (
1314 "github.com/openmeterio/openmeter/cmd/jobs/billing"
1415 "github.com/openmeterio/openmeter/cmd/jobs/entitlement"
1516 "github.com/openmeterio/openmeter/cmd/jobs/internal"
17+ "github.com/openmeterio/openmeter/cmd/jobs/quickstart"
1618 "github.com/openmeterio/openmeter/pkg/log"
1719)
1820
19- var configFileName string
20-
21- var rootCmd = cobra.Command {
22- Use : "jobs" ,
23- SilenceUsage : true ,
24- SilenceErrors : true ,
25- }
26-
2721func main () {
22+ var configFileName string
23+
2824 defer log .PanicLogger (log .WithExit )
2925
3026 // Create os.Signal aware context.Context which will trigger context cancellation
3127 // upon receiving any of the listed signals.
3228 ctx , cancel := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGHUP , syscall .SIGTERM )
3329 defer cancel ()
3430
35- err := internal .InitializeApplication (ctx , configFileName )
36- if err != nil {
37- slog .Error ("failed to initialize application" , "error" , err )
31+ rootCmd := cobra.Command {
32+ Use : "jobs" ,
33+ SilenceUsage : true ,
34+ SilenceErrors : true ,
35+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
36+ err := internal .InitializeApplication (ctx , configFileName )
37+ if err != nil {
38+ slog .Error ("failed to initialize application" , "error" , err )
3839
39- // Call cleanup function is may not set yet
40- if internal .AppShutdown != nil {
41- internal .AppShutdown ()
42- }
40+ // Call cleanup function is may not set yet
41+ if internal .AppShutdown != nil {
42+ internal .AppShutdown ()
43+ }
4344
44- os .Exit (1 )
45- }
46- defer internal .AppShutdown ()
45+ return errors .New ("failed to initialize application" )
46+ }
4747
48- if err = rootCmd .ExecuteContext (ctx ); err != nil {
49- slog .Error ("failed to execute command" , "error" , err )
50- os .Exit (1 )
48+ return nil
49+ },
5150 }
52- }
5351
54- func init () {
5552 rootCmd .PersistentFlags ().StringVarP (& configFileName , "config" , "" , "config.yaml" , "config file (default is config.yaml)" )
5653 _ = viper .BindPFlag ("config" , rootCmd .PersistentFlags ().Lookup ("config" ))
5754
5855 rootCmd .AddCommand (versionCommand ())
5956 rootCmd .AddCommand (entitlement .RootCommand ())
6057 rootCmd .AddCommand (billing .Cmd )
58+ rootCmd .AddCommand (quickstart .Cmd )
59+
60+ defer func () {
61+ if internal .AppShutdown != nil {
62+ internal .AppShutdown ()
63+ }
64+ }()
65+
66+ if err := rootCmd .ExecuteContext (ctx ); err != nil {
67+ slog .Error ("failed to execute command" , "error" , err )
68+ os .Exit (1 )
69+ }
6170}
0 commit comments