From 8463474a69466b4782eada51eb240e5595cf1567 Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Sun, 7 Jan 2024 13:42:39 -0500 Subject: [PATCH] feat: add ExecuteWithContext for root command (#1910) Signed-off-by: Artur Troian --- cmd/akash/cmd/root.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/akash/cmd/root.go b/cmd/akash/cmd/root.go index b214557850..7e6138d944 100644 --- a/cmd/akash/cmd/root.go +++ b/cmd/akash/cmd/root.go @@ -103,6 +103,28 @@ func Execute(rootCmd *cobra.Command, envPrefix string) error { return executor.ExecuteContext(ctx) } +// Execute executes the root command. +func ExecuteWithCtx(ctx context.Context, rootCmd *cobra.Command, envPrefix string) error { + // Create and set a client.Context on the command's Context. During the pre-run + // of the root command, a default initialized client.Context is provided to + // seed child command execution with values such as AccountRetriver, Keyring, + // and a Tendermint RPC. This requires the use of a pointer reference when + // getting and setting the client.Context. Ideally, we utilize + // https://github.com/spf13/cobra/pull/1118. + srvCtx := sdkserver.NewDefaultContext() + + ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{}) + ctx = context.WithValue(ctx, sdkserver.ServerContextKey, srvCtx) + + rootCmd.PersistentFlags().String(flags.FlagLogLevel, zerolog.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic)") + rootCmd.PersistentFlags().String(flags.FlagLogFormat, tmcfg.LogFormatPlain, "The logging format (json|plain)") + rootCmd.PersistentFlags().Bool(utilcli.FlagLogColor, false, "Pretty logging output. Applied only when log_format=plain") + rootCmd.PersistentFlags().String(utilcli.FlagLogTimestamp, "", "Add timestamp prefix to the logs (rfc3339|rfc3339nano|kitchen)") + + executor := tmcli.PrepareBaseCmd(rootCmd, envPrefix, app.DefaultHome) + return executor.ExecuteContext(ctx) +} + func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { debugCmd := debug.Cmd() debugCmd.AddCommand(ConvertBech32Cmd())