Skip to content

Commit

Permalink
Merge pull request #280 from valiner/env
Browse files Browse the repository at this point in the history
use env replace flag
  • Loading branch information
aceld authored Sep 13, 2023
2 parents 812ac81 + d5a6062 commit 8ece66f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 195 deletions.
46 changes: 46 additions & 0 deletions zconf/env.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 2 additions & 13 deletions zconf/globalobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -119,15 +117,15 @@ 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,
// in which case the default parameters should be used to initialize the logging module configuration.
// (配置文件不存在也需要用默认参数初始化日志模块配置)
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
}

Expand Down Expand Up @@ -188,18 +186,9 @@ func init() {
pwd = "."
}

args.InitConfigFlag(
pwd+"/conf/zinx.json",
"The configuration file defaults to <exeDir>/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变量,设置一些默认值)
Expand Down
49 changes: 0 additions & 49 deletions zutils/commandline/args/args.go

This file was deleted.

133 changes: 0 additions & 133 deletions zutils/commandline/uflag/uflag.go

This file was deleted.

0 comments on commit 8ece66f

Please sign in to comment.