github.com/Finschia/finschia-sdk@v0.48.1/server/cmd/execute.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	ostcfg "github.com/Finschia/ostracon/config"
     7  	ostcli "github.com/Finschia/ostracon/libs/cli"
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/Finschia/finschia-sdk/client"
    11  	"github.com/Finschia/finschia-sdk/client/flags"
    12  	"github.com/Finschia/finschia-sdk/server"
    13  )
    14  
    15  const (
    16  	envPrefix = "LBM"
    17  )
    18  
    19  // Execute executes the root command of an application. It handles creating a
    20  // server context object with the appropriate server and client objects injected
    21  // into the underlying stdlib Context. It also handles adding core CLI flags,
    22  // specifically the logging flags. It returns an error upon execution failure.
    23  func Execute(rootCmd *cobra.Command, defaultHome string) error {
    24  	// Create and set a client.Context on the command's Context. During the pre-run
    25  	// of the root command, a default initialized client.Context is provided to
    26  	// seed child command execution with values such as AccountRetriver, Keyring,
    27  	// and a Tendermint RPC. This requires the use of a pointer reference when
    28  	// getting and setting the client.Context. Ideally, we utilize
    29  	// https://github.com/spf13/cobra/pull/1118.
    30  	srvCtx := server.NewDefaultContext()
    31  	ctx := context.Background()
    32  	ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
    33  	ctx = context.WithValue(ctx, server.ServerContextKey, srvCtx)
    34  
    35  	rootCmd.PersistentFlags().String(flags.FlagLogLevel, ostcfg.DefaultPackageLogLevels(), "The logging level by modules (debug|info|error|none)")
    36  	rootCmd.PersistentFlags().String(flags.FlagLogFormat, ostcfg.LogFormatPlain, "The logging format (json|plain)")
    37  
    38  	executor := ostcli.PrepareBaseCmd(rootCmd, envPrefix, defaultHome)
    39  	return executor.ExecuteContext(ctx)
    40  }