Add support env variables in config file #143
This commit is contained in:
@@ -2,6 +2,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -30,6 +31,8 @@ func Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
|
data = []byte(shell.ReplaceEnvVars(string(data)))
|
||||||
|
|
||||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||||
println("ERROR: " + err.Error())
|
println("ERROR: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
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
|
||||||
|
i := strings.IndexByte(key, ':')
|
||||||
|
if i > 0 {
|
||||||
|
key, def = key[:i], key[i+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
value, exists := os.LookupEnv(key)
|
||||||
|
if exists {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if def != "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
return match
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user