Skip to content

Commit

Permalink
create a helper function to prepare env
Browse files Browse the repository at this point in the history
Signed-off-by: sczembor <[email protected]>
  • Loading branch information
sczembor committed Jan 14, 2025
1 parent 43e67d5 commit e37f79c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
24 changes: 2 additions & 22 deletions cmd/native-relayer/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions cmd/native-relayer/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

0 comments on commit e37f79c

Please sign in to comment.