From 91e411c43e33133dc4dc4add02a84bb4153aa4d3 Mon Sep 17 00:00:00 2001 From: jinzhu <1992053348@qq.com> Date: Sat, 5 Nov 2022 19:26:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AB=AF=E5=8F=A3=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console/commands/orm.go | 38 ++++++++++++++++++++++++++--------- console/commands/orm/mysql.go | 2 +- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/console/commands/orm.go b/console/commands/orm.go index 7a581ec..aefa581 100644 --- a/console/commands/orm.go +++ b/console/commands/orm.go @@ -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) + } } } diff --git a/console/commands/orm/mysql.go b/console/commands/orm/mysql.go index 5df23e5..38d4c44 100644 --- a/console/commands/orm/mysql.go +++ b/console/commands/orm/mysql.go @@ -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 {