Skip to content

Commit

Permalink
fix: 端口默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
ctfang committed Nov 5, 2022
1 parent 2990f54 commit 91e411c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
38 changes: 28 additions & 10 deletions console/commands/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,38 @@ func SetEnv(fileContext []byte) []byte {
}
nS := arr2[1]
st, et := GetBrackets(nS, '"', '"')
key := strings.Trim(nS[st:et+1], "\"")
nS = nS[et+1:]
st, et = GetBrackets(nS, '"', '"')
var val string
if et > 0 {
val = nS[st : et+1]
val = strings.Trim(val, "\"")
key := nS[st+1 : et]
nS = strings.TrimSpace(nS[et+1:])
nS = strings.Trim(nS, ")") // 得到 ,"val" or ,val

// 尝试获取默认值
val := ""
valIsStr := false
if len(nS) > 2 && nS[0:1] == "," {
nS = strings.TrimSpace(nS[1:])
nS = strings.TrimSpace(nS)
if nS[0:1] == "\"" {
// 使用双引号括起来的就是字符串
valIsStr = true
st, et = GetBrackets(nS, '"', '"')
val = nS[st+1 : et]
} else {
val = nS
}
}
envVal := os.Getenv(key)
if envVal != "" {

envVal, has := os.LookupEnv(key)
if has {
val = envVal
}

str = strings.Replace(str, s, arr2[0]+": "+val, 1)
if !valIsStr {
// 默认情况, 把值粘贴到yaml, 类型自动识别
str = strings.Replace(str, s, arr2[0]+": "+val, 1)
} else {
// 如果有默认值, 根据默认值识别类型
str = strings.Replace(str, s, arr2[0]+": \""+val+"\"", 1)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion console/commands/orm/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func NewDb(conf map[interface{}]interface{}) *DB {
"%s:%s@tcp(%s)/%s?interpolateParams=true",
config.GetString("username", "root"),
config.GetString("password", "123456"),
config.GetString("host", "localhost:"+config.GetString("port", "3306")),
config.GetString("host", "localhost")+":"+config.GetString("port", "3306"),
config.GetString("database", "demo"),
))
if err != nil {
Expand Down

0 comments on commit 91e411c

Please sign in to comment.