feat: update configuration loading to use viper and add environment variable overrides

This commit is contained in:
mlogclub
2026-06-13 10:40:09 +08:00
parent 0b64864aeb
commit daf1a176c1
5 changed files with 94 additions and 14 deletions
+11 -5
View File
@@ -3,9 +3,9 @@ package config
import (
"agent-desk/internal/pkg/enums"
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
"github.com/spf13/viper"
)
type Config struct {
@@ -199,13 +199,19 @@ type WxWorkConfig struct {
}
func Load(path string) (*Config, error) {
b, err := os.ReadFile(path)
if err != nil {
v := viper.New()
v.SetConfigFile(path)
v.SetConfigType("yaml")
v.SetEnvPrefix("AGENT_DESK")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
return nil, err
}
cfg := &Config{}
if err := yaml.Unmarshal(b, cfg); err != nil {
if err := v.Unmarshal(cfg); err != nil {
return nil, err
}
return cfg, nil