Improve config file handling by updating name and adding error checks

This commit is contained in:
Christian Galo 2025-05-11 05:06:28 -05:00
parent e5e9efd9b3
commit 058504a38f

View File

@ -64,14 +64,24 @@ func initConfig() {
// Search config in the current directory with name "member-console.yaml" // Search config in the current directory with name "member-console.yaml"
viper.AddConfigPath(".") viper.AddConfigPath(".")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.SetConfigName("member-console") viper.SetConfigName("mc-config") // Use "mc-config" as the config file name
} }
viper.SetEnvPrefix("WCMC") // set environment variable prefix viper.SetEnvPrefix("MC") // set environment variable prefix
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in. // If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found. This is always an error.
fmt.Fprintln(os.Stderr, "Error: Config file not found:", err)
os.Exit(1)
} else {
// Another error occurred (e.g., malformed YAML). This should be reported.
fmt.Fprintln(os.Stderr, "Error reading config file:", err)
os.Exit(1) // Exit for other config read errors
}
} else {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
} }
} }