Skip to content

Commit

Permalink
feat: add validate-config command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed May 15, 2024
1 parent f00965d commit 605c253
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ linters:
- depguard
- goimports
- nosnakecase
- mnd
36 changes: 33 additions & 3 deletions cmd/grafana-interacter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,48 @@ import (

var version = "unknown"

func Execute(configPath string) {
func ExecuteMain(configPath string) {
if configPath == "" {
logger.GetDefaultLogger().Fatal().Msg("Cannot start without config")
}

config := pkg.LoadConfig(configPath)
if err := config.Validate(); err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Error validating config")
}

newApp := app.NewApp(config, version)
newApp.Start()
}

func ExecuteValidateConfig(configPath string) {
config := pkg.LoadConfig(configPath)

if err := config.Validate(); err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Error validating config")
}

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

func main() {
var configPath string

rootCmd := &cobra.Command{
Use: "grafana-interacter",
Use: "grafana-interacter --config [config path]",
Long: "A Telegram bot to interact with your Grafana, Prometheus and Alertmanager instances.",
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 @@ -37,6 +60,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
4 changes: 0 additions & 4 deletions pkg/load_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@ func LoadConfig(path string) *configPkg.Config {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not set default settings")
}

if err := config.Validate(); err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Error validating config")
}

return config
}

0 comments on commit 605c253

Please sign in to comment.