fixed config loading on fresh machines, moved to XDG /home/keegan/.config directory

main
KeeganForelight 1 year ago
parent 9086b80994
commit 7c3bb7e26c

@ -1 +1 @@
0bbb9b59233e7c3a6b58764d03788b82
e832d011a7759af5da2bf569d9d999ac

@ -21,17 +21,28 @@ func NewCoordinator(config *viper.Viper, ch chan error) coordinator {
return server.NewCentralCoordinator(config, ch)
}
func NewConfig(fname string) *viper.Viper {
return config.LoadConfig(fname)
func LoadConfig(file, path, ext string) (*viper.Viper, error) {
return config.LoadConfig(file, path, ext)
}
func main() {
// lets get this bread
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
// config file
conf := NewConfig("server")
userHome, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configPath := fmt.Sprintf("%s/.config/FRMS", userHome)
configFile := "server"
configExt := "yaml"
conf, err := LoadConfig(configFile, configPath, configExt)
if err != nil {
panic(err)
}
errCh := make(chan error)

@ -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…
Cancel
Save