DOT ENV文件比较/验证

发布于 2025-01-20 04:13:57 字数 625 浏览 0 评论 0原文

如果您在特定环境的给定目录中有多个.env文件,例如dev.envstaging.envprod.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰沐子 2025-01-27 04:13:57

当然,计算每个密钥的文件,并显示差异。

#! /usr/bin/awk -f

{
    keys[$1]++
}

END {
    for( key in keys ) {
        if( n == 0 ) {
            n = keys[key]
        }

        if( keys[key] != n ) {
            printf "%s appears in %d of %d files\n", \
            key, keys[key], n > "/dev/stderr"
        }
    }
}
$ awk -F= -f ./missing.awk *.env
KEY_3 appears in 1 of 3 files

Sure, count the files for each key, and show discrepancies.

#! /usr/bin/awk -f

{
    keys[$1]++
}

END {
    for( key in keys ) {
        if( n == 0 ) {
            n = keys[key]
        }

        if( keys[key] != n ) {
            printf "%s appears in %d of %d files\n", \
            key, keys[key], n > "/dev/stderr"
        }
    }
}
$ awk -F= -f ./missing.awk *.env
KEY_3 appears in 1 of 3 files
ぃ双果 2025-01-27 04:13:57

任何人都可以建议awk命令或类似的命令
由于prod.env包含额外密钥的非零退出代码吗?

我会以按照方式

awk 'BEGIN{FS="=";PROCINFO["sorted_in"]="@ind_str_asc"}FNR==1{delete arr1}{arr1[$1]}ENDFILE{keys="";for(i in arr1){keys = keys FS i};arr2[keys];if(length(arr2)>1){exit(1)}}' *.env

说明这项任务的GNU AWK:我将gnu awk告知字段分隔符是=,并且该数组应以排序的索引进行遍历。在每行的起始行上,我要清除数组arr1,然后,对于每个行,请提及键是数组的第1列arr1,在处理文件后,我确实构建字符串<代码>密钥通过使用字段隔板的arr1连接键。这里的遍历顺序很重要,因为它规定了2个带有不同顺序的键的文件将提供相同的字符串。然后,我将此字符串用作arr2的钥匙,并且如果arr2中有超过1个不同的键,我确实以非零代码退出,如要求所规定。

(在GNU AWK 5.0.1中测试)

Can anyone suggest an awk command or similar that would return a
non-zero exit code due to prod.env containing an extra key?

I would harness GNU AWK for this task following way

awk 'BEGIN{FS="=";PROCINFO["sorted_in"]="@ind_str_asc"}FNR==1{delete arr1}{arr1[$1]}ENDFILE{keys="";for(i in arr1){keys = keys FS i};arr2[keys];if(length(arr2)>1){exit(1)}}' *.env

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 array arr1, then for each line mention key being value of 1st column of array arr1, after processing file, I do build string keys by concatenating keys of arr1 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 of arr2 and if there more than 1 different keys in arr2 I do exit with non-zero code as stipulated by requirements.

(tested in GNU Awk 5.0.1)

心如荒岛 2025-01-27 04:13:57

这是一个版本,不仅会通知您未对齐的键,还会向您显示它们的值是什么以及来自哪个 .env 文件:

 mawk 'BEGIN { FS=OFS="="
             __++ 
     } __~FNR {++____; _=FILENAME 
     }        {
             ___[$__]=___[$__]""_""FS""($NF)" | "
     } END { 
            _____=" %s only shows up at ::: %s\n"
        for(__ in ___) {

             _=___[__] 
             sub("[ ]*[|][ ]*$","",_)
             
             if(____!=gsub(FS,"&",_)) {
                   printf(_____,__,_) } } }' test*.env | lgp3

    KEY_3 only shows up at ::: testprod.env=PROD_VALUE_3

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 :

 mawk 'BEGIN { FS=OFS="="
             __++ 
     } __~FNR {++____; _=FILENAME 
     }        {
             ___[$__]=___[$__]""_""FS""($NF)" | "
     } END { 
            _____=" %s only shows up at ::: %s\n"
        for(__ in ___) {

             _=___[__] 
             sub("[ ]*[|][ ]*
quot;,"",_)
             
             if(____!=gsub(FS,"&",_)) {
                   printf(_____,__,_) } } }' test*.env | lgp3

    KEY_3 only shows up at ::: testprod.env=PROD_VALUE_3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文