DOT ENV文件比较/验证
如果您在特定环境的给定目录中有多个.env
文件,例如dev.env
,staging.env
和prod.env
使用shell,是否可以检查每个文件是否具有相同的env var键,忽略值?
例如,给定以下文件:
dev.env
KEY_1=DEV_VALUE_1
KEY_2=DEV_VALUE_2
staging.env
KEY_1=STAGING_VALUE_1
KEY_2=STAGING_VALUE_2
prod.env
KEY_1=PROD_VALUE_1
KEY_2=PROD_VALUE_2
KEY_3=PROD_VALUE_3
任何人都可以建议awk
命令或类似的命令将返回由于prod.env
包含额外密钥的非零退出代码吗?这里的目标是验证所有.env
文件都在其声明的键中对齐,主要是在CI设置中。
If you have multiple .env
files in a given directory for specific environments e.g. dev.env
, staging.env
and prod.env
, using a shell, is it possible to check that each file has the same env var keys, ignoring the values?
For example, given the following files:
dev.env
KEY_1=DEV_VALUE_1
KEY_2=DEV_VALUE_2
staging.env
KEY_1=STAGING_VALUE_1
KEY_2=STAGING_VALUE_2
prod.env
KEY_1=PROD_VALUE_1
KEY_2=PROD_VALUE_2
KEY_3=PROD_VALUE_3
Can anyone suggest an awk
command or similar that would return a non-zero exit code due to prod.env
containing an extra key? The goal here is to validate that all .env
files are aligned in the keys they declare, primarily in a CI setting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,计算每个密钥的文件,并显示差异。
Sure, count the files for each key, and show discrepancies.
我会以按照方式
说明这项任务的GNU AWK:我将gnu
awk
告知字段分隔符是=
,并且该数组应以排序的索引进行遍历。在每行的起始行上,我要清除数组arr1
,然后,对于每个行,请提及键是数组的第1列arr1
,在处理文件后,我确实构建字符串<代码>密钥通过使用字段隔板的arr1
连接键。这里的遍历顺序很重要,因为它规定了2个带有不同顺序的键的文件将提供相同的字符串。然后,我将此字符串用作arr2
的钥匙,并且如果arr2
中有超过1个不同的键,我确实以非零代码退出,如要求所规定。(在GNU AWK 5.0.1中测试)
I would harness GNU AWK for this task following way
Explanation: I inform GNU
AWK
that field separator is=
and that array should be traversed with sorted indices. On starting line of each line I do clear arrayarr1
, then for each line mention key being value of 1st column of arrayarr1
, after processing file, I do build stringkeys
by concatenating keys ofarr1
using field separator. Here order of traverse is important as it provides that 2 files with same keys in different order will give same string. Then I use this string as key ofarr2
and if there more than 1 different keys inarr2
I do exit with non-zero code as stipulated by requirements.(tested in GNU Awk 5.0.1)
这是一个版本,不仅会通知您未对齐的键,还会向您显示它们的值是什么以及来自哪个 .env 文件:
Here's a version that not only will inform you of mis-aligned keys, but also show you what their values are, and from which .env file :