# This file provides the following direnv extensions: # * ensure_all_documented # * ensure_all_set # It assumes .envrc lists all environment variables *required* by a project, # by exporting them set equal to an empty string: # export MYVAR= # or, to the default value: # export MYVAR=some-default # .envrc.private (which should be excluded from source control) supplies the values # for required variables. # Usage: # 1. In `.envrc`, document all exports: # # # MYVAR: a value I want to set # export MYVAR= # # OTHERVAR: (optional) a value I might want to set # export OTHERVAR= # # source_env .envrc.private # # 2. In `.envrc.private`, set all required values: # # export MYVAR=foo # # 3. At the end of `.envrc`, invoke the extensions (after checing they are available): # # if has ensure_all_documented; then # ensure_all_documented # else # echo "No ensure_all_documented function found, skipping documentation check" # fi # # if has ensure_all_set; then # ensure_all_set # else # echo "No ensure_all_set function found, skipping environment variable check" # fi __direnv_print_error() ( local RED="\e[1;31m" local DEFAULT="\e[0m" echo -e "${RED}$1${DEFAULT}" ) all_exports() ( # Find lines with exports of environment variables, # collect the names of the environment variables. # To get exported environment variables as an array # do this: # local exports=($(all_exports)) grep -Po '(?<=^export )[A-Z0-9_]+(?==)' .envrc ) # Ensure that all exports from .envrc are documented, # unless they are marked as (optional). # A comment should be as follows: # '# VARIABLE: documentation' ensure_all_documented() ( local exports=($(all_exports)) for (( i=0; i<${#exports[@]}; i++ )) do local export=${exports[$i]} grep -P "^# $export:" .envrc >/dev/null || __direnv_print_error "direnv: export $export does not have a comment" done ) ensure_all_set() ( local exports=($(all_exports)) for (( i=0; i<${#exports[@]}; i++ )) do local export=${exports[$i]} # Skip optional exports if grep -P "^# $export: \(optional\)" .envrc >/dev/null; then continue fi if [ -z ${!export} ]; then __direnv_print_error "direnv: export $export is unset. Either set it in .envrc.private or mark it as optional by adding a comment '# $export: (optional) ...'" fi done )