onepub-dev / dcli

An extensive library and tooling for building console/cli applications and scripts using the Dart programming language.
242 stars 28 forks source link

Cleaning environment #133

Closed ram535 closed 3 years ago

ram535 commented 3 years ago

I was reading a bash script for installing gcc. The script has this section.

#======================================================================
# Clean environment
#======================================================================

# Before beginning the configuration and build, clean the current shell of all
# environment variables, and set only the minimum that should be required. This
# prevents all sorts of unintended interactions between environment variables
# and the build process.

# store USER, HOME and then completely clear environment
U=$USER
H=$HOME

for i in $(env | awk -F"=" '{print $1}') ;
do
    unset $i || true   # ignore unset fails
done

# restore
export USER=$U
export HOME=$H
export PATH=/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin

echo shell environment follows:
env
.
.
.
# setting some environment variables

Do you think this could be a good feature?

bsutton commented 3 years ago

You can probably do this now.

The env global variable is a map.

Just iterator over the map and set the value of each key to null.

We could possibly add a method Env().clearall()

On Thu, 22 Apr 2021, 1:26 pm Ramses, @.***> wrote:

I was reading a bash script for installing gcc. The script has this section.

======================================================================

Clean environment

======================================================================

Before beginning the configuration and build, clean the current shell of all

environment variables, and set only the minimum that should be required. This

prevents all sorts of unintended interactions between environment variables

and the build process.

store USER, HOME and then completely clear environment

U=$USER H=$HOME

for i in $(env | awk -F"=" '{print $1}') ; do unset $i || true # ignore unset fails done

restore

export USER=$U export HOME=$H export PATH=/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin

echo shell environment follows: env .. . ..

setting some environment variables

Do you think this could be a good feature?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bsutton/dcli/issues/133, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG32OC2HNNONPOMO5D2XHDTJ6JN5ANCNFSM43LSHTGA .

bsutton commented 3 years ago

As you can already do this via interaction with the envs variable in dcli I don't the method adds much value.