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 {