Skip to content

fabianopinto/e

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 

Repository files navigation

e

Shell helper function to handle multiple environment setups.

Setup

Add this function to your profile, e.g., .bashrc or .zshrc.

Environment files

Organize your project folder structure as you see fit. Create any number of folders named .local anywhere you like, and within it, create environment files with the .env extension.

Here are some examples of these environment files:

~/.local/cf.env

export CF_USERNAME='johndoe'
echo -n 'CF password: '
read -rs CF_PASSWORD
export CF_PASSWORD
echo

~/projects/someproject/.local/cicd.env

export NODE='cicd'
export AWS_REGION='eu-west-1'
export AWS_PROFILE='test'
export CF_URL='https://api.cloud.example.com'

Environment files need to have user permissions to read and execute. Read the Security section below for more information.

Usage

With the function loaded and the environment files in the right place, you can use the function to load any number of environment files. Any environment file found traversing the folder structure up to the root folder will be applied.

Following the example above, you can use:

cd ~/projects/someproject
e cf cicd

The function will load all environment variables from ~/.local/cf.env and ~/projects/someproject/.local/cicd.env. You can use any sequence of environment keys, each processed in sequence.

It is worth noting that the ~/.local/cf.env example will prompt the user for some credentials, which will be stored in the environment.

Options

Option Description
-h or --help Show help message
-l or --list List all environment files found
-d or --dump Dump the current environment

Environment search

The function can be used from any folder. It will traverse the folders up to the root folder, searching for environment files with the pattern .local/*.env.

If an environment file cannot be found, this message will be shown:

Environment not found: .local/unknown.env

Use the option -l or --list to show all environment files found and available.

Clean-up

Previously set environment variables are erased before the environment files are loaded. The environment files load always keeps track of what is set, which can be undone later.

This way, you can easily clean any loaded environment using:

e

Use the option -d or --dump to list all environment variables and their values, if any.

Security

As a safeguard, If an environment file has group or others write-permission, it will be rejected, showing this message:

Security risk (-rwx----w-): .local/unsafe.env

To set the correct permissions for an environment file, use the following command:

chmod u+rx,go-w .local/myfile.env

This script uses source to load environment files, mainly setting environment variables. However, this can be risky if the environment files are not trusted. Always review the content of environment files before using them.

Never store sensible information in your environment files.

Troubleshooting

Required utilities

To traverse the folder structure and process environment files, the utilities cut, sed, and sort are utilized. These require the GNU versions, which may necessitate installing the GNU Core Utils.

On macOS, you can use Homebrew to install the GNU utilities, and the GNU implementation of sed:

brew install coreutils gnu-sed

Wrong permissions

If you encounter a "Security risk" message while loading an environment file, please refer to the Security section above.

Final notes

Feel free to use, adapt, enhance, and share this code.

About

Shell helper function to handle multiple environment setups.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages