Last active
September 26, 2023 17:12
-
-
Save mikeharty/93121990c7f1cd1b4506b00b17aedbe6 to your computer and use it in GitHub Desktop.
ENVY - A poor man's dotenv
This file contains 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
#!/bin/sh | |
usage="\e[44;1m ENVY \e[0m A poor man's dotenv for POSIX shells\n\n" | |
usage+="This script will \"source\" the provided .env file within a subshell, and then\n" | |
usage+="run the commands you pass within that subshell. This is a simple but effective way\n" | |
usage+="to get the basic advantages of dotenv in a shell. It uses a naive regexp to parse\n" | |
usage+="the provided .env, so it is not a full dotenv implementation.\n\n" | |
usage+="Usage: ./envy <dotenv> <commands>\n\n" | |
usage+="Options: \n" | |
usage+=" -d Look for a .env in the current working directory\n" | |
usage+="\n" | |
usage+="Arguments: \n" | |
usage+=" <dotenv> The path to your .env file\n" | |
usage+=" <commands> The commands you want to run with the .env sourced\n\n" | |
_env=$1 | |
_cmds=${@:2} | |
if [ -z "$_env" ] ||\ | |
[ "-h" = "$_env" ] ||\ | |
[ "--help" = "$_env" ]; then | |
printf "$usage" | |
exit 1 | |
fi | |
# flag that allows accepting the default ./.env file | |
if [ "-e" = $_env ]; then | |
_env="./.env" | |
elif [ ! -f $_env ]; then | |
printf "The environment file %s does not exist.\n\n" "$1" | |
exit 1 | |
fi | |
if ! command -v "$2" &> /dev/null | |
then | |
printf "The command file %s does not exist.\n\n" "$2" | |
exit 1 | |
fi | |
( | |
set -a | |
echo "Sourcing: $_env" | |
# shellcheck source=./.env | |
. $_env; | |
echo "Running: $_cmds" | |
# shellcheck disable=SC2039 | |
eval "${@:2}" | |
set +a | |
) 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment