代码拉取完成,页面将自动刷新
package envutil
import (
"os"
"regexp"
"strings"
)
// parse env value, allow:
// only key - "${SHELL}"
// with default - "${NotExist|defValue}"
// multi key - "${GOPATH}/${APP_ENV | prod}/dir"
// Notice:
// must add "?" - To ensure that there is no greedy match
// var envRegex = regexp.MustCompile(`\${[\w-| ]+}`)
var envRegex = regexp.MustCompile(`\${.+?}`)
// ParseEnvValue parse ENV var value from input string
func ParseEnvValue(val string) (newVal string) {
if strings.Index(val, "${") == -1 {
return val
}
var name, def string
return envRegex.ReplaceAllStringFunc(val, func(eVar string) string {
// eVar like "${NotExist|defValue}", first remove "${" and "}", then split it
ss := strings.SplitN(eVar[2:len(eVar)-1], "|", 2)
// with default value. ${NotExist|defValue}
if len(ss) == 2 {
name, def = strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
} else {
def = eVar // use raw value
name = strings.TrimSpace(ss[0])
}
// get value from ENV
eVal := os.Getenv(name)
if eVal == "" {
eVal = def
}
return eVal
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。