diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go new file mode 100644 index 0000000..44be0c5 --- /dev/null +++ b/internal/pkg/config/config.go @@ -0,0 +1,43 @@ +package config + +import ( + "github.com/spf13/viper" +) + +const ( + DEFAULT_CONFIG_FILE_NAME = "weapp" + DEFAULT_CONFIG_FILE_TYPE = "yaml" +) + +var defaultPaths = []string{ + ".", + "./conf", + "./config", + "../conf", + "../config", + "/conf", + "/config", + "${HOME}/config", + "/", +} + +func AutoLoad() error { + return LoadConfig(DEFAULT_CONFIG_FILE_NAME, DEFAULT_CONFIG_FILE_TYPE) +} + +func LoadConfig(fileName, filetype string, paths ...string) error { + for _, path := range defaultPaths { + viper.AddConfigPath(path) + } + + for _, path := range paths { + viper.AddConfigPath(path) + } + viper.SetConfigName(fileName) + viper.SetConfigType(filetype) + if err := viper.ReadInConfig(); err != nil { + return err + } + + return nil +}