简体   繁体   English

Golang 在 Buffalo database.yml 中使用 bash 环境变量

[英]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我是 Go 和 Buffalo 的新手,我正在尝试在我的数据库中使用我的 bash 环境变量。yaml

I attempted to do the following in my database.yaml, but it fails to interpret the value of my bash environment var localUser我试图在我的 database.yaml 中执行以下操作,但它无法解释我的 bash 环境localUser的值

user: ${localUser}

I set the localUser with the following bash我将 localUser 设置为以下 bash

export localUser="username"
echo $localUser

username用户名

Thanks for any help!!谢谢你的帮助!!

Buffalo Pop's configuration, database.yml , supports the following syntax. Buffalo Pop 的配置database.yml支持以下语法。

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.关键是envOr指令。 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.可以想象,如果值存在, production.user将设置为环境变量localUser的值,但如果没有环境变量,它将回退到默认值“defaultuser”。

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.您可以使用默认值分发(或发布)您的应用程序“图像”,然后您可以使用具有实际值的特定环境变量运行您的“容器”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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