Skip to content

Commit

Permalink
perf: 调整配置读取逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Dec 15, 2023
1 parent 772af8c commit 1083b41
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
42 changes: 25 additions & 17 deletions cmd/parse/koanf.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package parse

import (
"errors"
"os"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/opentdp/go-helper/filer"
"github.com/opentdp/go-helper/logman"

"tdp-cloud/cmd/args"
)

// 配置文件路径
Expand All @@ -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
}

// 从文件读取参数
Expand All @@ -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")
}

// 序列化参数信息
Expand All @@ -73,4 +79,6 @@ func (c *Config) WriteYaml() {
logman.Fatal("write config failed", "error", err)
}

return err

}
6 changes: 4 additions & 2 deletions cmd/parse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

func (c *Config) Server() {

debug := os.Getenv("TDP_DEBUG")
args.Debug = debug == "1" || debug == "true"

// 读取默认配置

mp := map[string]any{
Expand All @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/parse/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

func (c *Config) Worker() {

debug := os.Getenv("TDP_DEBUG")
args.Debug = debug == "1" || debug == "true"

// 读取默认配置

mp := map[string]any{
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/subset/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/subset/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1083b41

Please sign in to comment.