有没有办法验证.env文件?

时间:2017-05-25 22:29:09

标签: ini

我在尝试解析我拥有的.env文件时遇到错误,但我无法弄清楚它在哪里犯了错误。是否有一种简单的方法来在线或以其他方式对文件进行lint /验证?

非常感谢!

2 个答案:

答案 0 :(得分:0)

It depends on the syntax you are using. Looking at the Docker and NPM documentation, different tools seem to have a different scope on what they are able to parse.

I use a simple grep to validate if I have a <key>=<value> pattern, where key and value are non-empty. You can adapt the patterns to match your context, ensuring upper case keys for example.

#!/bin/bash

for envfile in $(find . -maxdepth 1 -type f -name '.env.*'); do 
    for line in $(cat ${envfile}); do
        # exclude comments
        if [[ "${line:0:1}" == "#" ]]; then
            continue
        fi

        match_line=$(echo ${line} | grep -E "^[A-Za-z0-9_].+=.+$")

        if [[ ${match_line} == "" ]]; then
            echo "Error in file: ${envfile}: line: ${line}"
        fi
    done
done

Alternatively, look at your language loadenv library to see if you can catch specific parsing exceptions, if available, to narrow down the specific line that causes the error.

答案 1 :(得分:0)

您可以尝试https://github.com/dotenv-linter/dotenv-linter。 这是用于.env文件的快速备份。用Rust写。