diff --git a/.task/checksum/go-build b/.task/checksum/go-build index aed43fb..fb78ec6 100644 --- a/.task/checksum/go-build +++ b/.task/checksum/go-build @@ -1 +1 @@ -0bbb9b59233e7c3a6b58764d03788b82 +e832d011a7759af5da2bf569d9d999ac diff --git a/cmd/server/main.go b/cmd/server/main.go index 1c217ca..810c6dd 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go new file mode 100644 index 0000000..4338b37 --- /dev/null +++ b/internal/pkg/config/config.go @@ -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 +} diff --git a/internal/pkg/config/load.go b/internal/pkg/config/load.go deleted file mode 100644 index 22568ac..0000000 --- a/internal/pkg/config/load.go +++ /dev/null @@ -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 -}