Substitutes the values of environment variables. (based on golang template)
3.7K
Substitutes the values of environment variables. (support go text/template and posix envsubst)
echo 'Current Login User is {{.USER}}' | envgod
Current Login User is allex
# with envsubst
echo 'Hi {{.USER}}, CWD: $PWD' | envgod -env-subst
Hi allex, CWD: /Users/allex/Dev/go/envgod
# lower-case variable been reserved
export foo='foo'
echo 'Hi {{.USER}}, CWD: $PWD, $foo' | envgod -env-subst
Hi allex, CWD: /Users/allex/Dev/go/envgod, $foo
Parse json enviroment varaiable
export FOO_CFG='{"foo":1,"bar":{"baz":[1,2,3,4,5]}}'
echo '{{- $config := jsonparse .FOO_CFG -}}{{- yamlencode $config -}}' | envgod
# output
---
bar:
baz:
- 1
- 2
- 3
- 4
- 5
foo: 1
cat <<'EOF' | envgod
{{- $config := jsonparse .CFG -}}
{{- range $k, $v := $config -}}
{{ $k }}: {{ $v }}
{{ end -}}
EOF
# output
---
bar: map[baz:[1 2 3 4 5]]
foo: 1
For a more complex example use for ngx configure render:
export NGX_PORT=8080
export NGX_VALID_REFERERS="none server_names ~10.58.4 192.168.*"
export NGX_FOO_UPSTREAM="10.58.4.3:3000,10.58.4.5:3000"
cat <<-'EOR' | envgod -env-subst
# nginx.conf
upstream portal.srv {
{{- $servers := split .NGX_FOO_UPSTREAM "," -}}
{{ range $servers }}
{{ if . }}server {{.}};{{- end -}}
{{ end }}
}
server {
listen ${NGX_PORT:-8080};
charset ${CHARSET:-UTF-8};
server_tokens off;
root $PWD; // root $${PWD}
{{ if .NGX_VALID_REFERERS -}}
valid_referers {{ .NGX_VALID_REFERERS }};
if ($invalid_referer) {
return 403;
}
{{- end }}
location / {
proxy_pass http://portal.srv
}
}
EOR
output
# nginx.conf
upstream portal.srv {
server 10.58.4.3:3000;
server 10.58.4.5:3000;
}
server {
listen 8080;
charset UTF-8;
server_tokens off;
root /var/www/envgod; // root ${PWD}
valid_referers none server_names ~10.58.4 192.168.*;
if ($invalid_referer) {
return 403;
}
location / {
proxy_pass http://portal.srv
}
}
Based on functions of text/templete
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators. (see: strings.Split)
{{$paths := split .PATH ":"}}
{{range $paths}}{{if .}}[{{.}}]{{end}}
{{ value | default "default value" }}
If value is ""(the empty string), the output will be "default value".
Strips leading and trailing whitespace.
{{ value | trim }}
base64encode: {{ base64encode "test" }}
base64decode: {{ base64encode "hello" | base64decode }}
{{ $config := yamldecode "{ foo: 1, bar: { baz: [ 1,2,3,4] } }" -}}
---
{{ yamlencode $config }}
{{ $ldcfg := loadfile "./test/test-config.yml" -}}
---
{{ yamlencode $ldcfg }}
{{ jsonstringify $ldcfg true | printf "# tojson\n%s" }}
Content type
Image
Digest
sha256:e685a86d9…
Size
1.6 MB
Last updated
10 months ago
docker pull tdio/envgod