Skip to content

Instantly share code, notes, and snippets.

@minitauros
Last active April 19, 2023 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minitauros/2c93c2825f316c0bf98f851798765f98 to your computer and use it in GitHub Desktop.
Save minitauros/2c93c2825f316c0bf98f851798765f98 to your computer and use it in GitHub Desktop.
Quickly switch direnv env vars
#!/bin/bash
#
# For easily switching between .envrc files.
#
# This allows you to have and use multiple .envrc files in the same
# directory, like .envrc.test, .envrc.local and .envrc.prod.
#
# Usage:
#
# denv [extension]
# Replaces the current .envrc file by .envrc.[extension]
#
# Examples:
#
# denv test
# Replaces the contents of .envrc by those of .envrc.test, then allows it.
#
# denv +
# Runs direnv allow
#
# denv .
# Runs direnv allow
#
# denv -
# Runs direnv deny
#
# denv
# Runs direnv deny
suffix=$1
if [[ -z "${suffix}" ]]; then
echo "suffix not given, running deny"
direnv deny
exit 0
elif [[ "${suffix}" == '-' ]]; then
echo "suffix is '-', running deny"
direnv deny
exit 0
elif [[ "${suffix}" == '+' ]]; then
echo "suffix is '+', running allow"
direnv allow
exit 0
elif [[ "${suffix}" == '.' ]]; then
echo "suffix is '.', running allow"
direnv allow
exit 0
fi
envrc_filename=".envrc.$suffix"
if [[ -f "${envrc_filename}" ]]; then
cp "${envrc_filename}" .envrc
echo "running direnv allow with file ${envrc_filename}"
direnv allow
else
echo ".envrc file with suffix $suffix does not exist"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment