Sign inSign up

tdio/envgod

By tdio

•Updated 10 months ago

Substitutes the values of environment variables. (based on golang template)

Image
0

3.7K

tdio/envgod repository overview

⁠envgod

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
  }
}

⁠Builtin Template Functions

Based on functions of text/templete⁠

⁠split

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}}
⁠default
  1. If the given string is ""(empty string), uses the given default argument.
  2. If the given array/slice/map is empty, uses the given default argument.
  3. If the given boolean value is false, uses the given default argument.
  • supported value types : string, array, slice, map, boolean
  • supported argument types : all
{{ value | default "default value" }}

If value is ""(the empty string), the output will be "default value".

⁠trim

Strips leading and trailing whitespace.

  • supported value types : string
{{ value | trim }}

⁠More helper functions enhancement

⁠base64encode/base64decode
base64encode: {{ base64encode "test" }}
base64decode: {{ base64encode "hello" | base64decode }}
⁠yamlencode / yamldecode
{{ $config := yamldecode "{ foo: 1, bar: { baz: [ 1,2,3,4] } }" -}}
---

{{ yamlencode $config }}
⁠loadfile/yamlencode/yamldecode/jsonstringify

{{ $ldcfg := loadfile "./test/test-config.yml" -}}

---

{{ yamlencode $ldcfg }}
{{ jsonstringify $ldcfg true | printf "# tojson\n%s" }}

Tag summary

Content type

Image

Digest

sha256:e685a86d9…

Size

1.6 MB

Last updated

10 months ago

docker pull tdio/envgod