Skip to content

Commit

Permalink
feat: add ExecuteWithContext for root command
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Jan 7, 2024
1 parent f26d00c commit ea8005f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/akash/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 125 in cmd/akash/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/akash/cmd/root.go#L107-L125

Added lines #L107 - L125 were not covered by tests
}

func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
debugCmd := debug.Cmd()
debugCmd.AddCommand(ConvertBech32Cmd())
Expand Down

0 comments on commit ea8005f

Please sign in to comment.