简体   繁体   中英

Golang use bash environment variables in Buffalo database.yml

I'm new to Go and Buffalo, and am attempting use my bash environment variables in my database.yaml

I attempted to do the following in my database.yaml, but it fails to interpret the value of my bash environment var localUser

user: ${localUser}

I set the localUser with the following bash

export localUser="username"
echo $localUser

username

Thanks for any help!!

Buffalo Pop's configuration, database.yml , supports the following syntax.

production:
  host: "localhost"
  user: {{ envOr "localUser" "defaultuser" }}

test:
  dialect: "mysql"
  url: {{ envOr "TEST_DATABASE_URL" "mysql://user:pass@(localhost:3306)/test" }}

The key is the envOr directive. As you can imagine, the production.user will be set as the value from the environment variable localUser if the value exists, but it will fall back to its default value "defaultuser" if there is no environment variable.

With this syntax, you can configure environment-specific values dynamically.

This is good for many situations such as container images that could be used in multiple different configurations. You can distribute (or publish) your application "image" with the default value, then you can run your "container" with specific environmental variables with the real values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM