英文:
Golang use bash environment variables in Buffalo database.yml
问题
我是你的中文翻译助手,以下是翻译好的内容:
我对Go和Buffalo还不熟悉,我试图在我的database.yaml文件中使用我的bash环境变量。
我尝试在database.yaml中使用以下代码,但它无法解释我的bash环境变量localUser
的值:
user: ${localUser}
我使用以下bash命令设置了localUser变量:
export localUser="username"
echo $localUser
username
感谢任何帮助!
英文:
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!!
答案1
得分: 1
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" }}
关键是envOr
指令。
可以想象,如果存在环境变量localUser
,则production.user
将被设置为该值,但如果没有环境变量,则会回退到默认值"defaultuser"。
使用这种语法,您可以动态配置特定于环境的值。
这对于许多情况非常有用,例如可以在多个不同配置中使用的容器映像。您可以使用默认值分发(或发布)应用程序"image",然后可以使用具有实际值的特定环境变量运行"container"。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论