Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use env replace flag #280

Merged
merged 6 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.