你可以使用godotenv根据构建命令切换env文件。

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

How can I switch between env files based on the build command using godotenv?

问题

我计划使用godotenv为我的项目设置不同的环境,但我不确定如何在dev.envuat.envprod.env之间切换文件。

我希望能够在我的Docker命令中传递一个值,例如RUN go build -o my-project --prod .,然后让godotenv选择相应的环境文件,比如在这种情况下是prod.env(假设这是正确的方式)。

另外,我该如何确保其他环境文件不会包含在特定环境的构建中。

英文:

I'm planning to use godotenv to setup different environments for my project but I am not sure how to switch between files like dev.env, uat.env, prod.env

I want to be able to just pass a value in my Docker command like RUN go build -o my-project --prod . and have godotenv pickup the relative env file - in this case prod.env (assuming this is the correct way.

Also, how can I make sure that the other env files don't get included in the build of a particular env.

答案1

得分: 2

我建议你按照Go命令行文档中建议的使用-X标志。

-X importpath.name=value
将importpath中名为name的字符串变量的值设置为value。
只有在变量在源代码中声明为未初始化或初始化为常量字符串表达式时,此选项才有效。如果初始化程序进行函数调用或引用其他变量,则-X将不起作用。
请注意,在Go 1.5之前,此选项需要两个单独的参数。

这样,你就可以在任何引用它位置的.env文件中调用它。

例如:go build -ldflags="-X 'package_path.variable_name=new_value'"

go build -ldflags "-X 'my/main/config.Version=v1.0.0'" -o $(MY_BIN) $(MY_SRC)
英文:

I will advice you use the -X flag as suggested by Go Documentation on Command Line

-X importpath.name=value
	Set the value of the string variable in importpath named name to value.
	This is only effective if the variable is declared in the source code either uninitialized or initialized to a constant string expression. -X will not work if the initializer makes a function call or refers to other variables.
	Note that before Go 1.5 this option took two separate arguments.

Such that you can then call any of your .env file referencing it location.

E.g. go build -ldflags="-X 'package_path.variable_name=new_value'"

That is

go build -ldflags "-X 'my/main/config.Version=v1.0.0'" -o $(MY_BIN) $(MY_SRC)

答案2

得分: 0

在构建阶段硬编码环境似乎有些奇怪。您不希望为每个环境构建一个不同的镜像,这是浪费资源的。

该模块的文档建议采用更好的方法:

已存在的环境优先于后加载的环境。

管理多个环境(例如开发、测试、生产)的惯例是创建一个名为{YOURAPP}_ENV的环境,并按以下顺序加载环境:

env := os.Getenv("FOO_ENV")
if "" == env {
  env = "development"
}

godotenv.Load(".env." + env + ".local")
if "test" != env {
  godotenv.Load(".env.local")
}
godotenv.Load(".env." + env)
godotenv.Load() // The Original .env
英文:

Hard coding your environment at the build stage seems odd to me. You don't want to build a diff image per env, that's wasteful.

The module documentation suggests a better approach:

Existing envs take precedence of envs that are loaded later.

The convention for managing multiple environments (i.e. development, test, production) is to create an env named {YOURAPP}_ENV and load envs in this order:

env := os.Getenv("FOO_ENV")
if "" == env {
  env = "development"
}

godotenv.Load(".env." + env + ".local")
if "test" != env {
  godotenv.Load(".env.local")
}
godotenv.Load(".env." + env)
godotenv.Load() // The Original .env

huangapple
  • 本文由 发表于 2021年9月9日 00:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/69106797.html
匿名

发表评论

匿名网友

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

确定