Skip to content

Support a leave-template #261

@RobertKosten

Description

@RobertKosten

Is your feature request related to a problem? Please describe.

My .env files are simple cases of exports, like so

export FOO="bar"
export BAZ="$(password-manager get key)"

Since these set secrets and I sometimes forget to open a fresh terminal when I'm done with them, these linger in the open session. To combat this I'm using a generic .env.leave

if [[ -f "$(dirname "${BASH_SOURCE[0]}")/.env" ]]; then
    while read -r LINE; do
        AUTOENV_LEAVE_VARIABLE="$(echo "$LINE" | sed -e 's/^export *//' | cut -d '=' -f 1 | xargs )"
        unset $AUTOENV_LEAVE_VARIABLE
    done < "$(dirname "${BASH_SOURCE[0]}")/.env"
    unset AUTOENV_LEAVE_VARIABLE
fi

This unsets all variables set in the corresponding .env. However I still have to put copies of this file everywhere, which is still error-prone and untidy.

Describe the solution you'd like

It would be great if there were a way to configure a generic leave-template that could be called automagically whenever there isn't a dedicated .env.leave file in the directory, but still an .env. Of course it needs to know the path it is running for (hence I call it a "template"), the equivalent of ${BASH_SOURCE[0]} above. Multiple solutions are possible here, from actually templating the file into place temporarily, to setting dedicated variables the file can read, etc.

Describe alternatives you've considered

I currently run a find job to place a copy of my .env.leave wherever it finds an .env before sourcing the activate.sh, but that's slow and decidedly not pretty, IMHO.

export AUTOENV_ENABLE_AUTOLEAVE="Set this to a non-empty string in order to enable automagic .env.leave creation"
if [[ ! -z "$AUTOENV_ENABLE_AUTOLEAVE" ]]; then
    AUTOENV_AUTOLEAVE_TEMPLATE_FILE="${AUTOENV_AUTOLEAVE_TEMPLATE:-${HOME}/.config/autoenv/auto.leave}"
    if [[ -f "$AUTOENV_AUTOLEAVE_TEMPLATE_FILE" ]]; then
	for AUTOENV_AUTOLEAVE_FILE in $(find "$HOME" -type f -name '.env'); do
	    if [[ ! -f "$AUTOENV_AUTOLEAVE_FILE.leave" ]]; then
		cp "$AUTOENV_AUTOLEAVE_TEMPLATE_FILE" "$AUTOENV_AUTOLEAVE_FILE.leave"
	    fi
	done
	unset AUTOENV_AUTOLEAVE_FILE
    fi
    unset AUTOENV_AUTOLEAVE_TEMPLATE_FILE
fi

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions