Skip to content

Commit

Permalink
Add env-clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Nov 6, 2023
1 parent ae7ae38 commit 19afc89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Breaking changes:
* None.

Other notable changes:
* `bashy-basics`: New utility function `env-names`.
* `bashy-basics`: New utility functions `env-clean` and `env-names`.

### v2.7 -- 2023-10-30

Expand Down
18 changes: 18 additions & 0 deletions scripts/lib/bashy-basics/_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ function check-json-output-args {
fi
}

# Removes from the environment all the variables except for the ones listed.
# Note that it is probably best to only run this in a subshell, e.g. just before
# running code that can't be fully trusted with a fuller set of environment
# variables.
function env-clean {
local allowList=" $* " # Minor cleverness for easier regex matching below.

local allNames
allNames=($(env-names)) || return "$?"

local n
for n in "${allNames[@]}"; do
if [[ ! (${allowList} =~ " ${n} ") ]]; then
export -n "${n}"
fi
done
}

# Prints a list of the names of all defined environment variables.
function env-names {
# It turns out that the most straightforward way to get a list of
Expand Down

0 comments on commit 19afc89

Please sign in to comment.