diff --git a/cmd/parse/koanf.go b/cmd/parse/koanf.go index 1f4aec3e..b9b38d9f 100644 --- a/cmd/parse/koanf.go +++ b/cmd/parse/koanf.go @@ -1,6 +1,7 @@ package parse import ( + "errors" "os" "github.com/knadh/koanf/parsers/yaml" @@ -8,8 +9,6 @@ import ( "github.com/knadh/koanf/v2" "github.com/opentdp/go-helper/filer" "github.com/opentdp/go-helper/logman" - - "tdp-cloud/cmd/args" ) // 配置文件路径 @@ -24,26 +23,27 @@ type Config struct { Override bool } -func (c *Config) Init() *Config { - - debug := os.Getenv("TDP_DEBUG") - args.Debug = debug == "1" || debug == "true" - - c.Parser = yaml.Parser() - c.Koanf = koanf.NewWithConf(koanf.Conf{ - StrictMerge: true, - Delim: ".", - }) +func NewConfig() *Config { - return c + return &Config{ + Parser: yaml.Parser(), + Koanf: koanf.NewWithConf(koanf.Conf{ + StrictMerge: true, + Delim: ".", + }), + } } -func (c *Config) ReadYaml() { +func (c *Config) ReadYaml() error { + + if YamlFile == "" { + return errors.New("config file not set") + } // 不存在则忽略 if _, err := os.Stat(YamlFile); os.IsNotExist(err) { - return + return err } // 从文件读取参数 @@ -52,13 +52,19 @@ func (c *Config) ReadYaml() { logman.Fatal("read config failed", "error", err) } + return err + } -func (c *Config) WriteYaml() { +func (c *Config) WriteYaml() error { + + if YamlFile == "" { + return errors.New("config file not set") + } // 是否强制覆盖 if !c.Override && filer.Exists(YamlFile) { - return + return errors.New("config file not exist") } // 序列化参数信息 @@ -73,4 +79,6 @@ func (c *Config) WriteYaml() { logman.Fatal("write config failed", "error", err) } + return err + } diff --git a/cmd/parse/server.go b/cmd/parse/server.go index de68301b..b4631005 100644 --- a/cmd/parse/server.go +++ b/cmd/parse/server.go @@ -13,6 +13,9 @@ import ( func (c *Config) Server() { + debug := os.Getenv("TDP_DEBUG") + args.Debug = debug == "1" || debug == "true" + // 读取默认配置 mp := map[string]any{ @@ -25,8 +28,7 @@ func (c *Config) Server() { // 读取配置文件 - if YamlFile != "" { - c.ReadYaml() + if c.ReadYaml() == nil { for k, v := range mp { c.Koanf.Unmarshal(k, v) } diff --git a/cmd/parse/worker.go b/cmd/parse/worker.go index a308b087..22cb0fee 100644 --- a/cmd/parse/worker.go +++ b/cmd/parse/worker.go @@ -13,6 +13,9 @@ import ( func (c *Config) Worker() { + debug := os.Getenv("TDP_DEBUG") + args.Debug = debug == "1" || debug == "true" + // 读取默认配置 mp := map[string]any{ @@ -24,8 +27,7 @@ func (c *Config) Worker() { // 读取配置文件 - if YamlFile != "" { - c.ReadYaml() + if c.ReadYaml() == nil { for k, v := range mp { c.Koanf.Unmarshal(k, v) } diff --git a/cmd/subset/server.go b/cmd/subset/server.go index 56265be8..f796597f 100644 --- a/cmd/subset/server.go +++ b/cmd/subset/server.go @@ -28,8 +28,8 @@ func serverFlag() *FlagSet { func serverExec(act string) { - c := parse.Config{} - c.Init().Server() + c := parse.NewConfig() + c.Server() if act == "" || act == "start" { c.WriteYaml() diff --git a/cmd/subset/worker.go b/cmd/subset/worker.go index a48dfba6..026fbca7 100644 --- a/cmd/subset/worker.go +++ b/cmd/subset/worker.go @@ -28,8 +28,8 @@ func workerFlag() *FlagSet { func workerExec(act string) { - c := parse.Config{} - c.Init().Worker() + c := parse.NewConfig() + c.Worker() if act == "" || act == "start" { c.WriteYaml()