Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validate-config command #39

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ linters:
- ifshort
- gci
- depguard
- mnd
35 changes: 32 additions & 3 deletions cmd/cosmos-node-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
version = "unknown"
)

func Execute(configPath string) {
func ExecuteMain(configPath string) {
appConfig, err := config.GetConfig(configPath)
if err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
Expand All @@ -37,15 +37,37 @@ func Execute(configPath string) {
}
}

func ExecuteValidateConfig(configPath string) {
appConfig, err := config.GetConfig(configPath)
if err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config!")
}

if err = appConfig.Validate(); err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Provided config is invalid!")
}

logger.GetDefaultLogger().Info().Msg("Provided config is valid.")
}

func main() {
var ConfigPath string

rootCmd := &cobra.Command{
Use: "cosmos-node-exporter",
Use: "cosmos-node-exporter --config [config path]",
Long: "A Prometheus scraper to return data about fullnode sync status and present upgrades.",
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)
},
}

Expand All @@ -54,6 +76,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")
}
Expand Down
Loading