From a56c0b54faa1e7d5311c44e472a20f1bd41a164b Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 15 Sep 2021 19:11:09 +0100 Subject: [PATCH] feat(bins): add envy command Quick and hacky bash script which supports loading .envrc files suitable for direnv, but with support for local override files, and named environment files, and supports looking for the .envrc* files upwards in parent folders. For example, when you run: envy echo hi It will look in the current folder, and all parent folders for any files named: - .envrc - .envrc.local - .envrc.echo - .envrc.echo.local The first such file that is found is considered to be in the project root, and all further files checked for will only be checked for in said project root. The first argument to envy is considered a environment name if there is a matching .envrc. file. If not such machine file is found, the first argument is treated as part of the command you want to run. Meaning with the above example, if ".envrc.echo" or ".envrc.echo.local" exists, the command that will be executed is "hi", but if neither of them exist, it will execute "echo hi". To define environment variables in a .envrc* file, you use use bash's export statement. --- bin/envy | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 bin/envy diff --git a/bin/envy b/bin/envy new file mode 100755 index 0000000..e1e25ff --- /dev/null +++ b/bin/envy @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +[ -n "$ENVY_DEBUG" ] && set -x + +export ENVY_ROOT="" + +envy__locate_up() { + local name="$1" + local cwd="$(pwd)" + local wd="$cwd" + local found="" + + while [ -z "$found" ] && [ "$wd" != "/" ]; do + if [ -f "${wd}/${name}" ]; then + found="${wd}/${name}" + else + cd .. + wd="$(pwd)" + fi + done + + echo "$found" + cd "$cwd" +} + +envy__locate_in_root() { + local name="$1" + local root="$2" + local cwd="$(pwd)" + + if [ -z "$root" ]; then + envy__locate_up "$name" + return 0 + fi + + if [ -f "${root}/${name}" ]; then + echo "${root}/${name}" + fi +} + +envy__main() { + local baserc + local localrc + local namedrc + local namedlocalrc + local envname="$1" + local shifted + + baserc="$(envy__locate_in_root ".envrc")" + if [ -n "$baserc" ]; then + if [ -z "$ENVY_ROOT" ]; then + export ENVY_ROOT="$(dirname "$baserc")" + fi + source "$baserc" + fi + + localrc="$(envy__locate_in_root ".envrc.local" "$ENVY_ROOT")" + if [ -n "$localrc" ]; then + if [ -z "$ENVY_ROOT" ]; then + export ENVY_ROOT="$(dirname "$localrc")" + fi + source "$localrc" + fi + + if [ "${#envname}" -lt 64 ]; then + namedrc="$(envy__locate_in_root ".envrc.${envname}" "$ENVY_ROOT")" + if [ -n "$namedrc" ]; then + shift 1 + shifted="1" + if [ -z "$ENVY_ROOT" ]; then + export ENVY_ROOT="$(dirname "$namedrc")" + fi + source "$namedrc" + fi + + namedlocalrc="$(envy__locate_in_root ".envrc.${envname}.local" "$ENVY_ROOT")" + if [ -n "$namedlocalrc" ]; then + if [ -z "$shifted" ]; then + shift 1 + fi + if [ -z "$ENVY_ROOT" ]; then + export ENVY_ROOT="$(dirname "$namedlocalrc")" + fi + source "$namedlocalrc" + fi + fi + + exec "$@" +} + +envy__main "$@"