go-bot/config/config.go
liyp 13483b9643 feat(config): 添加OpenAI API配置并优化打印配置函数
在config.toml中添加了OPENAI_API_KEY、OPENAI_BaseURL和MODEL配置项,以支持OpenAI API的集成。
同时,优化了PrintConfig函数,使其能够递归打印嵌套的配置结构,提高了配置管理的可读性和易用性。
2024-06-30 21:56:34 +08:00

50 lines
1.1 KiB
Go

package config
import (
"fmt"
"reflect"
"sync"
"github.com/BurntSushi/toml"
)
var (
config map[string]interface{}
once sync.Once
)
func loadConfig() {
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
panic(err)
}
}
func GetConfig() map[string]interface{} {
once.Do(loadConfig)
// print(config)
return config
}
func PrintConfig(m map[string]interface{}, indent string) {
for key, value := range m {
switch v := value.(type) {
case map[string]interface{}:
fmt.Printf("%s%s (type: %s):\n", indent, key, reflect.TypeOf(v))
PrintConfig(v, indent+" ")
case []interface{}:
fmt.Printf("%s%s:\n", indent, key)
for i, item := range v {
switch itemValue := item.(type) {
case map[string]interface{}:
fmt.Printf("%s [%d] (type: %s):\n", indent, i, reflect.TypeOf(itemValue))
PrintConfig(itemValue, indent+" ")
default:
fmt.Printf("%s [%d] (type: %s): %v\n", indent, i, reflect.TypeOf(itemValue), item)
}
}
default:
fmt.Printf("%s%s (type: %s): %v\n", indent, key, reflect.TypeOf(value), value)
}
}
}