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

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年9月28日 04:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/73873551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定