go-bot/config/config.go

51 lines
1.1 KiB
Go
Raw Normal View History

2024-04-01 09:42:29 +08:00
package config
import (
"fmt"
"reflect"
"sync"
2024-04-01 09:42:29 +08:00
"github.com/BurntSushi/toml"
)
2024-04-01 09:42:29 +08:00
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
2024-04-01 09:42:29 +08:00
}
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)
}
}
}