diff --git a/zconf/env.go b/zconf/env.go new file mode 100644 index 00000000..db78c219 --- /dev/null +++ b/zconf/env.go @@ -0,0 +1,46 @@ +/** + * User: coder.sdp@gmail.com + * Date: 2023/9/8 + * Time: 14:18 + */ + +package zconf + +import ( + "os" + "path/filepath" +) + +const ( + // EnvConfigFilePathKey (Set configuration file path export ZINX_CONFIG_FILE_PATH = xxxxxxzinx.json) + // (设置配置文件路径 export ZINX_CONFIG_FILE_PATH = xxx/xxx/zinx.json) + EnvConfigFilePathKey = "ZINX_CONFIG_FILE_PATH" + EnvDefaultConfigFilePath = "/conf/zinx.json" +) + +var env = new(zEnv) + +type zEnv struct { + configFilePath string +} + +func init() { + configFilePath := os.Getenv(EnvConfigFilePathKey) + if configFilePath == "" { + pwd, err := os.Getwd() + if err != nil { + panic(err) + } + configFilePath = filepath.Join(pwd, EnvDefaultConfigFilePath) + } + var err error + configFilePath, err = filepath.Abs(configFilePath) + if err != nil { + panic(err) + } + env.configFilePath = configFilePath +} + +func GetConfigFilePath() string { + return env.configFilePath +} diff --git a/zconf/globalobj.go b/zconf/globalobj.go index 86816794..55480a51 100644 --- a/zconf/globalobj.go +++ b/zconf/globalobj.go @@ -14,8 +14,6 @@ import ( "time" "github.com/aceld/zinx/zlog" - "github.com/aceld/zinx/zutils/commandline/args" - "github.com/aceld/zinx/zutils/commandline/uflag" ) const ( @@ -119,7 +117,7 @@ func PathExists(path string) (bool, error) { // and updates the fields of the "Config" structure accordingly. // If the configuration file does not exist, it prints an error message to the log and returns. func (g *Config) Reload() { - confFilePath := args.Args.ConfigFile + confFilePath := GetConfigFilePath() if confFileExists, _ := PathExists(confFilePath); confFileExists != true { // The configuration file may not exist, @@ -127,7 +125,7 @@ func (g *Config) Reload() { // (配置文件不存在也需要用默认参数初始化日志模块配置) g.InitLogConfig() - zlog.Ins().ErrorF("Config File %s is not exist!!", confFilePath) + zlog.Ins().ErrorF("Config File %s is not exist!! \n You can set configFile by setting the environment variable %s, like export %s = xxx/xxx/zinx.conf ", confFilePath, EnvConfigFilePathKey, EnvConfigFilePathKey) return } @@ -188,18 +186,9 @@ func init() { pwd = "." } - args.InitConfigFlag( - pwd+"/conf/zinx.json", - "The configuration file defaults to /conf/zinx.json if it is not set.", - ) - // Note: Prevent errors like "flag provided but not defined: -test.paniconexit0" from occurring in go test. // (防止 go test 出现"flag provided but not defined: -test.paniconexit0"等错误) testing.Init() - uflag.Parse() - - // after parsing - args.FlagHandle() // Initialize the GlobalObject variable and set some default values. // (初始化GlobalObject变量,设置一些默认值) diff --git a/zutils/commandline/args/args.go b/zutils/commandline/args/args.go deleted file mode 100644 index 2219d137..00000000 --- a/zutils/commandline/args/args.go +++ /dev/null @@ -1,49 +0,0 @@ -package args - -import ( - "os" - "path/filepath" - - "github.com/aceld/zinx/zutils/commandline/uflag" -) - -type args struct { - ExeAbsDir string - ExeName string - ConfigFile string -} - -var ( - Args = args{} - isInit = false -) - -func init() { - exe := os.Args[0] - - pwd, err := os.Getwd() - if err != nil { - panic(err) - } - - Args.ExeAbsDir = pwd - Args.ExeName = filepath.Base(exe) -} - -func InitConfigFlag(defaultValue string, tips string) { - if isInit { - return - } - isInit = true - - uflag.StringVar(&Args.ConfigFile, "c", defaultValue, tips) - return -} - -func FlagHandle() { - filePath, err := filepath.Abs(Args.ConfigFile) - if err != nil { - panic(err) - } - Args.ConfigFile = filePath -} diff --git a/zutils/commandline/uflag/uflag.go b/zutils/commandline/uflag/uflag.go deleted file mode 100644 index 55514544..00000000 --- a/zutils/commandline/uflag/uflag.go +++ /dev/null @@ -1,133 +0,0 @@ -package uflag - -import ( - "flag" - "strconv" - "sync" - "time" -) - -var flagNames = &FlagNames{m: make(map[string]int)} - -type FlagNames struct { - m map[string]int - lock sync.RWMutex -} - -func (fl *FlagNames) Set(key string, val int) { - fl.lock.Lock() - defer fl.lock.Unlock() - fl.m[key] = val -} - -func (fl *FlagNames) Get(key string) (val int, isOk bool) { - fl.lock.RLock() - defer fl.lock.RUnlock() - val, isOk = fl.m[key] - return -} - -func flagName(expect string) (actual string) { - num, ok := flagNames.Get(expect) - if !ok { - flagNames.Set(expect, 1) - return expect - } - - flagNames.Set(expect, num+1) - return expect + strconv.Itoa(num) -} - -func BoolVar(p *bool, expectName string, defaultValue bool, usage string) (actualName string) { - actualName = flagName(expectName) - flag.BoolVar(p, actualName, defaultValue, usage) - return -} - -func Bool(expectName string, defaultValue bool, usage string) (p *bool, actualName string) { - actualName = flagName(expectName) - return flag.Bool(actualName, defaultValue, usage), actualName -} - -func IntVar(p *int, expectName string, defaultValue int, usage string) (actualName string) { - actualName = flagName(expectName) - flag.IntVar(p, actualName, defaultValue, usage) - return -} - -func Int(expectName string, defaultValue int, usage string) (p *int, actualName string) { - actualName = flagName(expectName) - return flag.Int(actualName, defaultValue, usage), actualName -} - -func Int64Var(p *int64, expectName string, defaultValue int64, usage string) (actualName string) { - actualName = flagName(expectName) - flag.Int64Var(p, actualName, defaultValue, usage) - return -} - -func Int64(expectName string, defaultValue int64, usage string) (p *int64, actualName string) { - actualName = flagName(expectName) - return flag.Int64(actualName, defaultValue, usage), actualName -} - -func UintVar(p *uint, expectName string, defaultValue uint, usage string) (actualName string) { - actualName = flagName(expectName) - flag.UintVar(p, actualName, defaultValue, usage) - return -} - -func Uint(expectName string, defaultValue uint, usage string) (p *uint, actualName string) { - actualName = flagName(expectName) - return flag.Uint(actualName, defaultValue, usage), actualName -} - -func Uint64Var(p *uint64, expectName string, defaultValue uint64, usage string) (actualName string) { - actualName = flagName(expectName) - flag.Uint64Var(p, actualName, defaultValue, usage) - return -} - -func Uint64(expectName string, defaultValue uint64, usage string) (p *uint64, actualName string) { - actualName = flagName(expectName) - return flag.Uint64(actualName, defaultValue, usage), actualName -} - -func StringVar(p *string, expectName string, defaultValue string, usage string) (actualName string) { - actualName = flagName(expectName) - flag.StringVar(p, actualName, defaultValue, usage) - return -} - -func String(expectName string, defaultValue string, usage string) (p *string, actualName string) { - actualName = flagName(expectName) - return flag.String(actualName, defaultValue, usage), actualName -} - -func Float64Var(p *float64, expectName string, defaultValue float64, usage string) (actualName string) { - actualName = flagName(expectName) - flag.Float64Var(p, actualName, defaultValue, usage) - return -} - -func Float64(expectName string, defaultValue float64, usage string) (p *float64, actualName string) { - actualName = flagName(expectName) - return flag.Float64(actualName, defaultValue, usage), actualName -} - -func DurationVar(p *time.Duration, expectName string, defaultValue time.Duration, - usage string) (actualName string) { - - actualName = flagName(expectName) - flag.DurationVar(p, actualName, defaultValue, usage) - return -} - -func Duration(expectName string, defaultValue time.Duration, usage string) (p *time.Duration, actualName string) { - actualName = flagName(expectName) - return flag.Duration(actualName, defaultValue, usage), actualName -} - -func Parse() { - flag.Parse() -}