You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package config
|
|
|
|
/*
|
|
Load.go contains methods to load values from config, flags and env.
|
|
*/
|
|
|
|
import (
|
|
"FRMS/internal/pkg/logging"
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func LoadConfig(fname string) *viper.Viper {
|
|
// Demarshalls a given filename into the struct
|
|
// returns nil if successful
|
|
config := viper.New()
|
|
configPath := "$HOME/FRMS/internal/configs"
|
|
logging.Debug(logging.DStart, "Loading config for %s", fname)
|
|
config.SetConfigName(fname)
|
|
config.SetConfigType("yaml")
|
|
//viper.AddConfigPath("/etc/frms/config")
|
|
config.AddConfigPath(configPath)
|
|
// struct and env vars
|
|
|
|
// Sets env vars
|
|
config.AutomaticEnv()
|
|
|
|
// reading
|
|
if err := config.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
// no config file found
|
|
fmt.Printf("No config file found! creating empty one at %s.\n", configPath)
|
|
if err = config.WriteConfigAs(configPath); err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
logging.Debug(logging.DStart, "CON Loaded configs from %v", config.ConfigFileUsed())
|
|
|
|
// returning config object
|
|
return config
|
|
}
|