feat: update framework

This commit is contained in:
Rogee
2024-11-29 15:03:58 +08:00
parent edd1cc20fa
commit fbb511a9cd
2 changed files with 31 additions and 39 deletions

View File

@@ -2,7 +2,6 @@ package config
import (
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
@@ -10,38 +9,23 @@ import (
"github.com/spf13/viper"
)
func Load(file, app string) (*viper.Viper, error) {
func Load(file string) (*viper.Viper, error) {
v := viper.NewWithOptions(viper.KeyDelimiter("_"))
v.AutomaticEnv()
if file == "" {
ext := filepath.Ext(file)
if ext == "" {
v.SetConfigType("toml")
v.SetConfigName(app + ".toml")
paths := []string{"."}
// execute path
execPath, err := os.Executable()
if err == nil {
paths = append(paths, filepath.Dir(execPath))
}
// home path
homePath, err := os.UserHomeDir()
if err == nil {
paths = append(paths, homePath, homePath+"/"+app, homePath+"/.config", homePath+"/.config/"+app)
}
paths = append(paths, "/etc", "/etc/"+app, "/usr/local/etc", "/usr/local/etc/"+app)
log.Println("try load config from paths:", paths)
for _, path := range paths {
v.AddConfigPath(path)
}
v.SetConfigFile(file)
} else {
v.SetConfigType(ext[1:])
v.SetConfigFile(file)
}
v.AddConfigPath(".")
err := v.ReadInConfig()
log.Println("use config file:", v.ConfigFileUsed())
log.Println("config file:", v.ConfigFileUsed())
if err != nil {
return nil, errors.Wrap(err, "config file read error")
}