Created
July 30, 2022 00:07
-
-
Save markeissler/3edd8ac48d9d54fae47663fe45295659 to your computer and use it in GitHub Desktop.
Strip comments and single quotes from dotenv files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# SOME_VALUE='my_value' | |
# SOME_VALUE=my_value | |
# SOME_ARRAY='["one", "two", "three"]' | |
# SOME_ARRAY='["one","two","three"]' | |
# SOME_ARRAY=["one", "two", "three"] | |
input="${1}" | |
# Safeguard from overwriting a source file by making sure filename ends in `.raw`. | |
if [[ ! "${input}" =~ \.raw$ ]]; then | |
>&2 echo "source file does not have a '.raw' extension!" && exit 1 | |
fi | |
if [[ ! -r "${input}" ]]; then | |
>&2 echo "source file does not look to a regular file!" && exit 1 | |
fi | |
key="(\w+)" | |
pattern1="([^']+)" | |
gsed -rn -e "s/^$key=['\"]?$pattern1['\"]?$/\1=\2/p" "${input}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment