refactor: add serverv2.Execute helper (#20773)

This commit is contained in:
Julien Robert 2024-06-26 16:31:53 +02:00 committed by GitHub
parent 9c909a7238
commit 43a7b99f67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 13 deletions

View File

@ -2,10 +2,11 @@ package cmd
import (
"context"
"strings"
cmtcli "github.com/cometbft/cometbft/libs/cli"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
@ -26,12 +27,17 @@ func Execute(rootCmd *cobra.Command, envPrefix, defaultHome string) error {
ctx := CreateExecuteContext(context.Background())
rootCmd.PersistentFlags().String(flags.FlagLogLevel, zerolog.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic|disabled or '*:<level>,<key>:<level>')")
// NOTE: The default logger is only checking for the "json" value, any other value will default to plain text.
rootCmd.PersistentFlags().String(flags.FlagLogFormat, "plain", "The logging format (json|plain)")
rootCmd.PersistentFlags().Bool(flags.FlagLogNoColor, false, "Disable colored logs")
rootCmd.PersistentFlags().StringP(flags.FlagHome, "", defaultHome, "directory for config and data")
rootCmd.PersistentFlags().Bool(server.FlagTrace, false, "print out full stack trace on errors")
executor := cmtcli.PrepareBaseCmd(rootCmd, envPrefix, defaultHome)
return executor.ExecuteContext(ctx)
// update the global viper with the root command's configuration
viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()
return rootCmd.ExecuteContext(ctx)
}
// CreateExecuteContext returns a base Context with server and client context

View File

@ -7,14 +7,32 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
)
// Execute executes the root command of an application.
// It handles adding core CLI flags, specifically the logging flags.
func Execute(rootCmd *cobra.Command, envPrefix, defaultHome string) error {
rootCmd.PersistentFlags().String(FlagLogLevel, "info", "The logging level (trace|debug|info|warn|error|fatal|panic|disabled or '*:<level>,<key>:<level>')")
rootCmd.PersistentFlags().String(FlagLogFormat, "plain", "The logging format (json|plain)")
rootCmd.PersistentFlags().Bool(FlagLogNoColor, false, "Disable colored logs")
rootCmd.PersistentFlags().StringP(FlagHome, "", defaultHome, "directory for config and data")
// update the global viper with the root command's configuration
viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()
return rootCmd.Execute()
}
func Commands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logger log.Logger, components ...ServerComponent[transaction.Tx]) (CLIConfig, error) {
if len(components) == 0 {
return CLIConfig{}, errors.New("no components provided")
@ -90,8 +108,7 @@ func AddCommands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logg
return err
}
err = configHandle(server, home, cmd)
if err != nil {
if err = configHandle(server, home, cmd); err != nil {
return err
}
@ -109,14 +126,16 @@ func AddCommands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logg
// configHandle writes the default config to the home directory if it does not exist and sets the server context
func configHandle(s *Server, home string, cmd *cobra.Command) error {
configDir := filepath.Join(home, "config")
// we need to check app.toml as the config folder can already exist for the client.toml
if _, err := os.Stat(filepath.Join(home, "config", "app.toml")); os.IsNotExist(err) {
if err = s.WriteConfig(filepath.Join(home, "config")); err != nil {
if _, err := os.Stat(filepath.Join(configDir, "app.toml")); os.IsNotExist(err) {
if err = s.WriteConfig(configDir); err != nil {
return err
}
}
viper, err := ReadConfig(filepath.Join(home, "config"))
viper, err := ReadConfig(configDir)
if err != nil {
return err
}

View File

@ -46,7 +46,6 @@ var _ ServerComponent[transaction.Tx] = (*Server)(nil)
// Configs returns a viper instance of the config file
func ReadConfig(configPath string) (*viper.Viper, error) {
v := viper.New()
v.SetConfigType("toml")
v.SetConfigName("config")
v.AddConfigPath(configPath)

View File

@ -4,15 +4,14 @@ import (
"fmt"
"os"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/simapp/v2"
"cosmossdk.io/simapp/v2/simdv2/cmd"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" // TODO(@julienrbrt), no need to abstract this.
)
func main() {
rootCmd := cmd.NewRootCmd()
if err := svrcmd.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
if err := serverv2.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
fmt.Fprintln(rootCmd.OutOrStderr(), err)
os.Exit(1)
}