配置导出工具优化及示例
This commit is contained in:
parent
defeac306a
commit
3f36a3cddc
|
@ -0,0 +1,14 @@
|
||||||
|
// Code generated by minotaur-config-export. DO NOT EDIT.
|
||||||
|
package configs
|
||||||
|
// SystemConfig 系统
|
||||||
|
type SystemConfig struct {
|
||||||
|
Addr string // 监听地址
|
||||||
|
Finish string // 启动完成
|
||||||
|
}
|
||||||
|
|
||||||
|
// WelcomeConfig 欢迎词
|
||||||
|
type WelcomeConfig struct {
|
||||||
|
Id int // ID
|
||||||
|
Text string // 内容
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
// Code generated by minotaur-config-export. DO NOT EDIT.
|
||||||
|
package configs
|
||||||
|
import (
|
||||||
|
jsonIter "github.com/json-iterator/go"
|
||||||
|
"github.com/kercylan98/minotaur/utils/log"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var json = jsonIter.ConfigCompatibleWithStandardLibrary
|
||||||
|
var (
|
||||||
|
ISystemConfig *SystemConfig
|
||||||
|
iSystemConfig *SystemConfig
|
||||||
|
IWelcomeConfig map[int]*WelcomeConfig
|
||||||
|
iWelcomeConfig map[int]*WelcomeConfig
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadConfig(handle func(filename string, config any) error) {
|
||||||
|
var err error
|
||||||
|
iSystemConfig = new(SystemConfig)
|
||||||
|
if err = handle("SystemConfig.json", iSystemConfig); err != nil {
|
||||||
|
log.Error("Config", zap.String("Name", "SystemConfig"), zap.Bool("Invalid", true), zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
iWelcomeConfig = make(map[int]*WelcomeConfig)
|
||||||
|
if err = handle("WelcomeConfig.json", &iWelcomeConfig); err != nil {
|
||||||
|
log.Error("Config", zap.String("Name", "WelcomeConfig"), zap.Bool("Invalid", true), zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Refresh() {
|
||||||
|
ISystemConfig = iSystemConfig
|
||||||
|
IWelcomeConfig = iWelcomeConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultLoad(filepath string) {
|
||||||
|
LoadConfig(func(filename string, config any) error {
|
||||||
|
bytes, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(bytes, &config)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,29 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kercylan98/minotaur/config"
|
||||||
|
"github.com/kercylan98/minotaur/examples/simple-server-config/config/configs"
|
||||||
|
"github.com/kercylan98/minotaur/planner/configexport"
|
||||||
|
"github.com/kercylan98/minotaur/utils/log"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
workdir = "./examples/simple-server-config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
export()
|
||||||
|
config.Init(filepath.Join(workdir, "config", "json"), configs.LoadConfig, configs.Refresh)
|
||||||
|
config.Load()
|
||||||
|
config.Refresh()
|
||||||
|
log.Info("Config", zap.Any("SystemConfig", configs.ISystemConfig))
|
||||||
|
log.Info("Config", zap.Any("WelcomeConfig", configs.IWelcomeConfig))
|
||||||
|
}
|
||||||
|
|
||||||
|
func export() {
|
||||||
|
c := configexport.New(filepath.Join(workdir, "config", "系统配置.xlsx"))
|
||||||
|
c.ExportGo("", filepath.Join(workdir, "config", "configs"))
|
||||||
|
c.ExportServer("", filepath.Join(workdir, "config", "json"))
|
||||||
|
}
|
|
@ -39,7 +39,7 @@ func (slf *ConfigExport) ExportClient(prefix, outputDir string) {
|
||||||
if len(prefix) > 0 {
|
if len(prefix) > 0 {
|
||||||
config.Prefix = fmt.Sprintf("%s.", prefix)
|
config.Prefix = fmt.Sprintf("%s.", prefix)
|
||||||
}
|
}
|
||||||
if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.%s.json", prefix, config.Name)), config.JsonClient()); err != nil {
|
if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s%s.json", prefix, config.Name)), config.JsonClient()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ func (slf *ConfigExport) ExportServer(prefix, outputDir string) {
|
||||||
if len(prefix) > 0 {
|
if len(prefix) > 0 {
|
||||||
config.Prefix = fmt.Sprintf("%s.", prefix)
|
config.Prefix = fmt.Sprintf("%s.", prefix)
|
||||||
}
|
}
|
||||||
if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.%s.json", prefix, config.Name)), config.JsonServer()); err != nil {
|
if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s%s.json", prefix, config.Name)), config.JsonServer()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,29 +9,29 @@ import (
|
||||||
|
|
||||||
var json = jsonIter.ConfigCompatibleWithStandardLibrary
|
var json = jsonIter.ConfigCompatibleWithStandardLibrary
|
||||||
var (
|
var (
|
||||||
GameIndexConfig map[int]map[string]*IndexConfig
|
IIndexConfig map[int]map[string]*IndexConfig
|
||||||
gameIndexConfig map[int]map[string]*IndexConfig
|
iIndexConfig map[int]map[string]*IndexConfig
|
||||||
GameEasyConfig *EasyConfig
|
IEasyConfig *EasyConfig
|
||||||
gameEasyConfig *EasyConfig
|
iEasyConfig *EasyConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadConfig(handle func(filename string, config any) error) {
|
func LoadConfig(handle func(filename string, config any) error) {
|
||||||
var err error
|
var err error
|
||||||
gameIndexConfig = make(map[int]map[string]*IndexConfig)
|
iIndexConfig = make(map[int]map[string]*IndexConfig)
|
||||||
if err = handle("server.IndexConfig.json", &gameIndexConfig); err != nil {
|
if err = handle("server.IndexConfig.json", &iIndexConfig); err != nil {
|
||||||
log.Error("Config", zap.String("Name", "IndexConfig"), zap.Bool("Invalid", true), zap.Error(err))
|
log.Error("Config", zap.String("Name", "IndexConfig"), zap.Bool("Invalid", true), zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
gameEasyConfig = new(EasyConfig)
|
iEasyConfig = new(EasyConfig)
|
||||||
if err = handle("server.EasyConfig.json", gameEasyConfig); err != nil {
|
if err = handle("server.EasyConfig.json", iEasyConfig); err != nil {
|
||||||
log.Error("Config", zap.String("Name", "EasyConfig"), zap.Bool("Invalid", true), zap.Error(err))
|
log.Error("Config", zap.String("Name", "EasyConfig"), zap.Bool("Invalid", true), zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Refresh() {
|
func Refresh() {
|
||||||
GameIndexConfig = gameIndexConfig
|
IIndexConfig = iIndexConfig
|
||||||
GameEasyConfig = gameEasyConfig
|
IEasyConfig = iEasyConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultLoad(filepath string) {
|
func DefaultLoad(filepath string) {
|
||||||
|
|
|
@ -37,21 +37,21 @@ var json = jsonIter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
var (
|
var (
|
||||||
{{range $index, $config := .Configs}}
|
{{range $index, $config := .Configs}}
|
||||||
Game{{$config.Name}} {{$config.GetVariable}}
|
I{{$config.Name}} {{$config.GetVariable}}
|
||||||
game{{$config.Name}} {{$config.GetVariable}}
|
i{{$config.Name}} {{$config.GetVariable}}
|
||||||
{{end}}
|
{{end}}
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadConfig(handle func(filename string, config any) error) {
|
func LoadConfig(handle func(filename string, config any) error) {
|
||||||
var err error
|
var err error
|
||||||
{{range $index, $config := .Configs}}
|
{{range $index, $config := .Configs}}
|
||||||
game{{$config.Name}} = {{$config.GetVariableGen}}
|
i{{$config.Name}} = {{$config.GetVariableGen}}
|
||||||
{{if eq $config.IndexCount 0}}
|
{{if eq $config.IndexCount 0}}
|
||||||
if err = handle("{{$config.Prefix}}{{$config.Name}}.json", game{{$config.Name}}); err != nil {
|
if err = handle("{{$config.Prefix}}{{$config.Name}}.json", i{{$config.Name}}); err != nil {
|
||||||
log.Error("Config", zap.String("Name", "{{$config.Name}}"), zap.Bool("Invalid", true), zap.Error(err))
|
log.Error("Config", zap.String("Name", "{{$config.Name}}"), zap.Bool("Invalid", true), zap.Error(err))
|
||||||
}
|
}
|
||||||
{{else}}
|
{{else}}
|
||||||
if err = handle("{{$config.Prefix}}{{$config.Name}}.json", &game{{$config.Name}}); err != nil {
|
if err = handle("{{$config.Prefix}}{{$config.Name}}.json", &i{{$config.Name}}); err != nil {
|
||||||
log.Error("Config", zap.String("Name", "{{$config.Name}}"), zap.Bool("Invalid", true), zap.Error(err))
|
log.Error("Config", zap.String("Name", "{{$config.Name}}"), zap.Bool("Invalid", true), zap.Error(err))
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -60,7 +60,7 @@ func LoadConfig(handle func(filename string, config any) error) {
|
||||||
|
|
||||||
func Refresh() {
|
func Refresh() {
|
||||||
{{range $index, $config := .Configs}}
|
{{range $index, $config := .Configs}}
|
||||||
Game{{$config.Name}} = game{{$config.Name}}
|
I{{$config.Name}} = i{{$config.Name}}
|
||||||
{{end}}
|
{{end}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue