From 3d924e808ba33f67bd93babcf7df1f390a615c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Thu, 29 Dec 2022 14:32:48 -0500 Subject: [PATCH] Added config --- config/config.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..a8eac74 --- /dev/null +++ b/config/config.go @@ -0,0 +1,55 @@ +package config + +import ( + "strings" + + "github.com/spf13/viper" +) + +const ( + StatusOnline int8 = iota + StatusOffline = 2 + StatusNoNewSyncs = 3 +) + +var VERSION string + +type ServiceStatus int8 + +type Config struct { + Debug string + + Systems []struct { + Type string + Config map[string]interface{} + } +} + +func Load() (Config, error) { + viper.SetDefault("Debug", "true") + + viper.SetConfigName("gobbs.toml") + viper.SetConfigType("toml") + viper.AddConfigPath("/etc/") + viper.AddConfigPath("$XDG_CONFIG_HOME/") + viper.AddConfigPath("$HOME/.config/") + viper.AddConfigPath("$HOME/") + viper.AddConfigPath(".") + + viper.SetEnvPrefix("gobbs") + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { + return Config{}, err + } + } + + var config Config + if err := viper.Unmarshal(&config); err != nil { + return Config{}, err + } + + return config, nil +}