From e37f79cd2efc808f92df2d6616b365ec507f6f9b Mon Sep 17 00:00:00 2001 From: sczembor Date: Tue, 14 Jan 2025 12:26:20 +0100 Subject: [PATCH] create a helper function to prepare env Signed-off-by: sczembor --- cmd/native-relayer/cli/start.go | 24 ++---------------------- cmd/native-relayer/cli/utils.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/native-relayer/cli/start.go b/cmd/native-relayer/cli/start.go index e65f2e1..7934b39 100644 --- a/cmd/native-relayer/cli/start.go +++ b/cmd/native-relayer/cli/start.go @@ -11,13 +11,11 @@ import ( "github.com/block-vision/sui-go-sdk/sui" "github.com/btcsuite/btcd/rpcclient" "github.com/gonative-cc/relayer/dal" - "github.com/gonative-cc/relayer/env" "github.com/gonative-cc/relayer/ika" "github.com/gonative-cc/relayer/ika2btc" "github.com/gonative-cc/relayer/native" "github.com/gonative-cc/relayer/native2ika" "github.com/gonative-cc/relayer/nbtc" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -26,27 +24,9 @@ var startCmd = &cobra.Command{ Use: "start", Short: "Starts the relayer", Run: func(cmd *cobra.Command, args []string) { - flags := cmd.Root().PersistentFlags() - lvl, err := flags.GetString("log-level") + config, err := prepareEnv(cmd) if err != nil { - log.Error().Err(err).Msg("Error getting log level") - os.Exit(1) - } - logLvl, err := zerolog.ParseLevel(lvl) - if err != nil { - log.Error().Err(err).Msg("Error parsing log level") - os.Exit(1) - } - env.InitLogger(logLvl) - configFile, err := flags.GetString("config") - if err != nil { - log.Error().Err(err).Msg("Error getting config file path") - os.Exit(1) - } - - config, err := loadConfig(configFile) - if err != nil { - log.Error().Err(err).Msg("Error loading config") + log.Error().Err(err).Msg("Error preparing env") os.Exit(1) } db, err := dal.NewDB(config.DB.File) diff --git a/cmd/native-relayer/cli/utils.go b/cmd/native-relayer/cli/utils.go index 8a57603..6fe13c5 100644 --- a/cmd/native-relayer/cli/utils.go +++ b/cmd/native-relayer/cli/utils.go @@ -3,7 +3,10 @@ package cli import ( "fmt" + "github.com/gonative-cc/relayer/env" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" + "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -30,3 +33,25 @@ func loadConfig(configFile string) (*Config, error) { return &config, nil } + +func prepareEnv(cmd *cobra.Command) (*Config, error) { + flags := cmd.Root().PersistentFlags() + lvl, err := flags.GetString("log-level") + if err != nil { + return nil, fmt.Errorf("error getting log level: %w", err) + } + logLvl, err := zerolog.ParseLevel(lvl) + if err != nil { + return nil, fmt.Errorf("error parsing log level: %w", err) + } + env.InitLogger(logLvl) + configFile, err := flags.GetString("config") + if err != nil { + return nil, fmt.Errorf("error getting config file path: %w", err) + } + config, err := loadConfig(configFile) + if err != nil { + return nil, fmt.Errorf("error loading config: %w", err) + } + return config, nil +}