feat: add config loader
This commit is contained in:
parent
217ed6924d
commit
0b0da3b030
43
internal/pkg/config/config.go
Normal file
43
internal/pkg/config/config.go
Normal file
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user