mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-09-27 04:36:12 +08:00
33 lines
486 B
Go
33 lines
486 B
Go
package shell
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func ReplaceEnvVars(text string) string {
|
|
re := regexp.MustCompile(`\${([^}{]+)}`)
|
|
return re.ReplaceAllStringFunc(text, func(match string) string {
|
|
key := match[2 : len(match)-1]
|
|
|
|
var def string
|
|
var dok bool
|
|
|
|
if i := strings.IndexByte(key, ':'); i > 0 {
|
|
key, def = key[:i], key[i+1:]
|
|
dok = true
|
|
}
|
|
|
|
if value, vok := os.LookupEnv(key); vok {
|
|
return value
|
|
}
|
|
|
|
if dok {
|
|
return def
|
|
}
|
|
|
|
return match
|
|
})
|
|
}
|