fixed config loading on fresh machines, moved to XDG /home/keegan/.config directory
parent
9086b80994
commit
7c3bb7e26c
@ -1 +1 @@
|
||||
0bbb9b59233e7c3a6b58764d03788b82
|
||||
e832d011a7759af5da2bf569d9d999ac
|
||||
|
@ -0,0 +1,52 @@
|
||||
// package Config wraps the viper library to setup/manage files for FRMS
|
||||
package config
|
||||
|
||||
import (
|
||||
"FRMS/internal/pkg/logging"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// LoadConfig loads the file at path/file into a viper object
|
||||
// Expects config file to be yaml
|
||||
func LoadConfig(file, path, ext string) (*viper.Viper, error) {
|
||||
|
||||
logging.Debug(logging.DStart, "CON Loading config for %s", file)
|
||||
|
||||
config := viper.New()
|
||||
|
||||
configFile := fmt.Sprintf("%s/%s.%s", path, file, ext)
|
||||
|
||||
config.SetConfigName(file)
|
||||
config.AddConfigPath(path)
|
||||
config.SetConfigType(ext)
|
||||
|
||||
// Sets env vars
|
||||
config.AutomaticEnv()
|
||||
|
||||
// create config directory if it doesn't exist
|
||||
if err := os.MkdirAll(path, 0750); err != nil && !os.IsExist(err) {
|
||||
return config, err
|
||||
}
|
||||
|
||||
// attempt to create an empty config incase it doesn't exist
|
||||
if err := config.SafeWriteConfigAs(configFile); err != nil {
|
||||
// if error thrown because file exists, fine to ignore
|
||||
if _, ok := err.(viper.ConfigFileAlreadyExistsError); !ok {
|
||||
return config, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := config.ReadInConfig(); err != nil {
|
||||
fmt.Printf("read error %v\n", config)
|
||||
return config, err
|
||||
}
|
||||
|
||||
fmt.Printf("Found %s\n", config.ConfigFileUsed())
|
||||
logging.Debug(logging.DStart, "CON Loaded configs from %#V", config.ConfigFileUsed())
|
||||
|
||||
// returning config object
|
||||
return config, nil
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue