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

Adds check to ensure config file exists. Fixes: #4513 #5387

Merged
merged 4 commits into from
Feb 3, 2025
Merged
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
56 changes: 36 additions & 20 deletions cmd/cli/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,42 +104,58 @@ func initConfig() {
viper.SetEnvPrefix("minder")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))

cfgFilePath := cli.GetRelevantCLIConfigPath(viper.GetViper())
if cfgFilePath != "" {
cfgFileData, err := config.GetConfigFileData(cfgFilePath)
if err != nil {
RootCmd.PrintErrln(err)
// Get the config flag value directly to ensure we catch explicitly specified configs
configFlag := viper.GetString("config")
if configFlag != "" {
// User explicitly specified a config file via --config flag
if _, err := os.Stat(configFlag); err != nil {
RootCmd.PrintErrln(fmt.Sprintf("Cannot find specified config file: %s", configFlag))
os.Exit(1)
}

keysWithNullValue := config.GetKeysWithNullValueFromYAML(cfgFileData, "")
if len(keysWithNullValue) > 0 {
RootCmd.PrintErrln("Error: The following configuration keys are missing values:")
for _, key := range keysWithNullValue {
RootCmd.PrintErrln("Null Value at: " + key)
}
os.Exit(1)
}

viper.SetConfigFile(cfgFilePath)
viper.SetConfigFile(configFlag)
} else {
// use defaults
// No config file specified, use defaults
viper.SetConfigName("config")
viper.AddConfigPath(".")
if cfgDirPath := cli.GetDefaultCLIConfigPath(); cfgDirPath != "" {
viper.AddConfigPath(cfgDirPath)
}
}

viper.SetConfigType("yaml")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
if configFlag != "" {
// If there's any error, we should fail
RootCmd.PrintErrln(fmt.Sprintf("Error reading config file %s: %v", configFlag, err))
os.Exit(1)
}

if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; use default values
// Only allow "config not found" when no config was explicitly specified
RootCmd.PrintErrln("No config file present, using default values.")
} else {
// Some other error occurred
RootCmd.Printf("Error reading config file: %s", err)
RootCmd.PrintErrln(fmt.Sprintf("Error reading config file: %s", err))
os.Exit(1)
}
}

// If we successfully read a config file, check for null values
if configFlag != "" {
cfgFileData, err := config.GetConfigFileData(configFlag)
if err != nil {
RootCmd.PrintErrln(fmt.Sprintf("Error reading config file data: %v", err))
os.Exit(1)
}

keysWithNullValue := config.GetKeysWithNullValueFromYAML(cfgFileData, "")
if len(keysWithNullValue) > 0 {
RootCmd.PrintErrln("Error: The following configuration keys are missing values:")
for _, key := range keysWithNullValue {
RootCmd.PrintErrln("Null Value at: " + key)
}
os.Exit(1)
}
}
}
Expand Down