Skip to content

Instantly share code, notes, and snippets.

@owulveryck
Last active Jan 3, 2022
Embed
What would you like to do?
app.yaml environment variables substitution

This is a code snippet that reads an "app.yaml" file on stdin; then it:

  • seek for env_variables entry in the file
  • for each key: value pair, it looks for an environment variables named value
  • if it exists, it substitute the value with value found in the environment.

Exemple:

cat app.yaml

runtime: nodejs12

service: default

handlers:
  - url: /fonts
    static_dir: build/fonts
  - url: /img
    static_dir: build/img
  - url: /static/js/(.*)
    static_files: build/static/js/\1
    upload: build/static/js/(.*)
  - url: /static/css/(.*)
    static_files: build/static/css/\1
    upload: build/static/css/(.*)
  - url: /static/media/(.*)
    static_files: build/static/media/\1
    upload: build/static/media/(.*)
  - url: /(.*\.(json|ico|svg|png))$
    static_files: build/\1
    upload: build/.*\.(json|ico|svg|png)$
  - url: /manifest.webmanifest
    static_files: build/manifest.webmanifest
    upload: build/manifest.webmanifest
  - url: /
    static_files: build/index.html
    upload: build/index.html
  - url: /.*
    static_files: build/index.html
    upload: build/index.html

env_variables:
  TERM: TERM
  BLABLA: BLABLA
  TOTO: TITI
export BLABLA=coucou
cat app.yaml | go run .
2022/01/03 17:41:47 variable TITI not found
runtime: nodejs12
service: default
handlers:
- url: /fonts
  static_dir: build/fonts
- url: /img
  static_dir: build/img
- url: /static/js/(.*)
  static_files: build/static/js/\1
  upload: build/static/js/(.*)
- url: /static/css/(.*)
  static_files: build/static/css/\1
  upload: build/static/css/(.*)
- url: /static/media/(.*)
  static_files: build/static/media/\1
  upload: build/static/media/(.*)
- url: /(.*\.(json|ico|svg|png))$
  static_files: build/\1
  upload: build/.*\.(json|ico|svg|png)$
- url: /manifest.webmanifest
  static_files: build/manifest.webmanifest
  upload: build/manifest.webmanifest
- url: /
  static_files: build/index.html
  upload: build/index.html
- url: /.*
  static_files: build/index.html
  upload: build/index.html
env_variables:
  TERM: xterm-256color
  BLABLA: coucou
  TOTO: ""
package main
import (
"log"
"os"
"gopkg.in/yaml.v2"
)
func main() {
dec := yaml.NewDecoder(os.Stdin)
content := yaml.MapSlice{}
err := dec.Decode(&content)
if err != nil {
log.Fatal(err)
}
for _, item := range content {
if item.Key.(string) == "env_variables" {
keyValue := item.Value.(yaml.MapSlice)
for i, v := range keyValue {
val, ok := os.LookupEnv(v.Value.(string))
if !ok {
log.Printf("variable %v not found", v.Value)
}
keyValue[i].Value = val
}
}
}
enc := yaml.NewEncoder(os.Stdout)
enc.Encode(content)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment