From 6a60113145f9dd028e02864686821fe44d08e6db Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 15 May 2024 14:14:55 +0300 Subject: [PATCH] feat: add validate-config command --- cmd/missed-blocks-checker.go | 38 +++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/cmd/missed-blocks-checker.go b/cmd/missed-blocks-checker.go index 3a0bf50..0420c65 100644 --- a/cmd/missed-blocks-checker.go +++ b/cmd/missed-blocks-checker.go @@ -2,6 +2,7 @@ package main import ( "main/pkg" + configPkg "main/pkg/config" "main/pkg/fs" "main/pkg/logger" "os" @@ -24,21 +25,45 @@ func (fs *OsFS) Create(path string) (fs.File, error) { return os.Create(path) } -func Execute(configPath string) { +func ExecuteMain(configPath string) { filesystem := &OsFS{} app := pkg.NewApp(configPath, filesystem, version) app.Start() } +func ExecuteValidateConfig(configPath string) { + filesystem := &OsFS{} + + config, err := configPkg.GetConfig(configPath, filesystem) + if err != nil { + logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config!") + } + + if err := config.Validate(); err != nil { + logger.GetDefaultLogger().Fatal().Err(err).Msg("Config is invalid!") + } + + logger.GetDefaultLogger().Info().Msg("Provided config is valid.") +} + func main() { var ConfigPath string rootCmd := &cobra.Command{ - Use: "missed-blocks-checker", + Use: "missed-blocks-checker --config [config path]", Long: "Monitors validators' missed blocks on Cosmos chains.", Version: version, Run: func(cmd *cobra.Command, args []string) { - Execute(ConfigPath) + ExecuteMain(ConfigPath) + }, + } + + validateConfigCmd := &cobra.Command{ + Use: "validate-config --config [config path]", + Long: "Validate config.", + Version: version, + Run: func(cmd *cobra.Command, args []string) { + ExecuteValidateConfig(ConfigPath) }, } @@ -47,6 +72,13 @@ func main() { logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not set flag as required") } + validateConfigCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path") + if err := validateConfigCmd.MarkPersistentFlagRequired("config"); err != nil { + logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not set flag as required") + } + + rootCmd.AddCommand(validateConfigCmd) + if err := rootCmd.Execute(); err != nil { logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not start application") }