-
-
Save mike623/d9a4b7cb71dbabb53b2414e9a2058e65 to your computer and use it in GitHub Desktop.
wrap-env
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/bash | |
# | |
# Place this script into your script folder and use like this: | |
# | |
# ./script/wrap-env.sh .env $(npm bin grunt)/grunt serve | |
# | |
# Your .env file must have exports in it, not just a list of variable like Docker env files work | |
# | |
usage="$(basename "$0") [-h] .env_file command [arg ...] | |
where: | |
first argument must be env variable file | |
second argument must be a command you want to run, for example \`grunt\` | |
optionally followed by the arguments to the command" | |
while getopts ':h' option; do | |
case "$option" in | |
h) echo "$usage" | |
exit | |
;; | |
\?) printf "illegal option: -%s\n" "$OPTARG" >&2 | |
echo "$usage" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [ "$#" -le 1 ]; then | |
printf "Must provide at least 2 arguments.\n" >&2 | |
printf "\n$usage"; | |
exit 1 | |
fi | |
if ! [ -f "$1" ]; then | |
printf "Environment file $1 does not exist.\n" >&2 | |
printf "\n$usage"; | |
exit 1 | |
fi | |
path=$1; | |
shift; | |
env $(grep -v '^#' $path | sed 's/export/''/g' | xargs) $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment