From 6aa774702928293c5ec82d309b3a2bd6fb6c485d Mon Sep 17 00:00:00 2001 From: jmxnzo Date: Wed, 15 Jan 2025 17:21:23 +0100 Subject: [PATCH] initializer: add Cobra for subcommands --- initializer/main.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/initializer/main.go b/initializer/main.go index 7235a49067..6eeae86c47 100644 --- a/initializer/main.go +++ b/initializer/main.go @@ -22,15 +22,36 @@ import ( "github.com/edgelesssys/contrast/internal/grpc/dialer" "github.com/edgelesssys/contrast/internal/logger" "github.com/edgelesssys/contrast/internal/meshapi" + + "github.com/edgelesssys/contrast/internal/constants" + "github.com/spf13/cobra" ) func main() { - if err := run(); err != nil { + if err := execute(); err != nil { os.Exit(1) } } -func run() (retErr error) { +func execute() error { + cmd := newRootCmd() + ctx := context.Background() + return cmd.ExecuteContext(ctx) +} + +func newRootCmd() *cobra.Command { + root := &cobra.Command{ + Use: "initializer", + Short: "initializer", + RunE: run, + SilenceUsage: true, + Version: constants.Version, + } + root.InitDefaultVersionFlag() + return root +} + +func run(cmd *cobra.Command, _ []string) (retErr error) { log, err := logger.Default() if err != nil { fmt.Fprintf(os.Stderr, "Error: creating logger: %v\n", err) @@ -49,7 +70,7 @@ func run() (retErr error) { return errors.New("COORDINATOR_HOST not set") } - ctx := context.Background() + ctx := cmd.Context() privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil {