diff options
author | 2022-10-03 21:42:20 +0530 | |
---|---|---|
committer | 2022-10-03 21:42:20 +0530 | |
commit | af120ab348f2e1a5a39dec035ed9dcf84189a64e (patch) | |
tree | 2a3aadd7ce1b7b771dfe3fe7c983569726c8d7ed /zsh/oh-my-zsh/plugins/dotenv | |
download | dotfiles-af120ab348f2e1a5a39dec035ed9dcf84189a64e.tar.gz dotfiles-af120ab348f2e1a5a39dec035ed9dcf84189a64e.tar.bz2 dotfiles-af120ab348f2e1a5a39dec035ed9dcf84189a64e.zip |
dotfiles
Diffstat (limited to 'zsh/oh-my-zsh/plugins/dotenv')
-rw-r--r-- | zsh/oh-my-zsh/plugins/dotenv/README.md | 92 | ||||
-rw-r--r-- | zsh/oh-my-zsh/plugins/dotenv/dotenv.plugin.zsh | 67 |
2 files changed, 159 insertions, 0 deletions
diff --git a/zsh/oh-my-zsh/plugins/dotenv/README.md b/zsh/oh-my-zsh/plugins/dotenv/README.md new file mode 100644 index 0000000..ab9d329 --- /dev/null +++ b/zsh/oh-my-zsh/plugins/dotenv/README.md @@ -0,0 +1,92 @@ +# dotenv + +Automatically load your project ENV variables from `.env` file when you `cd` into project root directory. + +Storing configuration in the environment is one of the tenets of a [twelve-factor app](https://www.12factor.net). Anything that is likely to change between deployment environments, such as resource handles for databases or credentials for external services, should be extracted from the code into environment variables. + +To use it, add `dotenv` to the plugins array in your zshrc file: + +```sh +plugins=(... dotenv) +``` + +## Usage + +Create `.env` file inside your project root directory and put your ENV variables there. + +For example: + +```sh +export AWS_S3_TOKEN=d84a83539134f28f412c652b09f9f98eff96c9a +export SECRET_KEY=7c6c72d959416d5aa368a409362ec6e2ac90d7f +export MONGO_URI=mongodb://127.0.0.1:27017 +export PORT=3001 +``` + +`export` is optional. This format works as well: + +```sh +AWS_S3_TOKEN=d84a83539134f28f412c652b09f9f98eff96c9a +SECRET_KEY=7c6c72d959416d5aa368a409362ec6e2ac90d7f +MONGO_URI=mongodb://127.0.0.1:27017 +PORT=3001 +``` + +You can even mix both formats, although it's probably a bad idea. + +## Settings + +### ZSH_DOTENV_FILE + +You can also modify the name of the file to be loaded with the variable `ZSH_DOTENV_FILE`. +If the variable isn't set, the plugin will default to use `.env`. +For example, this will make the plugin look for files named `.dotenv` and load them: + +```zsh +# in ~/.zshrc, before Oh My Zsh is sourced: +ZSH_DOTENV_FILE=.dotenv +``` + +### ZSH_DOTENV_PROMPT + +Set `ZSH_DOTENV_PROMPT=false` in your zshrc file if you don't want the confirmation message. +You can also choose the `Always` option when prompted to always allow sourcing the .env file +in that directory. See the next section for more details. + +### ZSH_DOTENV_ALLOWED_LIST, ZSH_DOTENV_DISALLOWED_LIST + +The default behavior of the plugin is to always ask whether to source a dotenv file. There's +a **Y**es, **N**o, **A**lways and N**e**ver option. If you choose Always, the directory of the .env file +will be added to an allowed list; if you choose Never, it will be added to a disallowed list. +If a directory is found in either of those lists, the plugin won't ask for confirmation and will +instead either source the .env file or proceed without action respectively. + +The allowed and disallowed lists are saved by default in `$ZSH_CACHE_DIR/dotenv-allowed.list` and +`$ZSH_CACHE_DIR/dotenv-disallowed.list` respectively. If you want to change that location, +change the `$ZSH_DOTENV_ALLOWED_LIST` and `$ZSH_DOTENV_DISALLOWED_LIST` variables, like so: + +```zsh +# in ~/.zshrc, before Oh My Zsh is sourced: +ZSH_DOTENV_ALLOWED_LIST=/path/to/dotenv/allowed/list +ZSH_DOTENV_DISALLOWED_LIST=/path/to/dotenv/disallowed/list +``` + +The file is just a list of directories, separated by a newline character. If you want +to change your decision, just edit the file and remove the line for the directory you want to +change. + +NOTE: if a directory is found in both the allowed and disallowed lists, the disallowed list +takes preference, _i.e._ the .env file will never be sourced. + +## Version Control + +**It's strongly recommended to add `.env` file to `.gitignore`**, because usually it contains sensitive information such as your credentials, secret keys, passwords etc. You don't want to commit this file, it's supposed to be local only. + +## Disclaimer + +This plugin only sources the `.env` file. Nothing less, nothing more. It doesn't do any checks. It's designed to be the fastest and simplest option. You're responsible for the `.env` file content. You can put some code (or weird symbols) there, but do it on your own risk. `dotenv` is the basic tool, yet it does the job. + +If you need more advanced and feature-rich ENV management, check out these awesome projects: + +* [direnv](https://github.com/direnv/direnv) +* [zsh-autoenv](https://github.com/Tarrasch/zsh-autoenv) diff --git a/zsh/oh-my-zsh/plugins/dotenv/dotenv.plugin.zsh b/zsh/oh-my-zsh/plugins/dotenv/dotenv.plugin.zsh new file mode 100644 index 0000000..46cd4b1 --- /dev/null +++ b/zsh/oh-my-zsh/plugins/dotenv/dotenv.plugin.zsh @@ -0,0 +1,67 @@ +## Settings + +# Filename of the dotenv file to look for +: ${ZSH_DOTENV_FILE:=.env} + +# Path to the file containing allowed paths +: ${ZSH_DOTENV_ALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-allowed.list"} +: ${ZSH_DOTENV_DISALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-disallowed.list"} + + +## Functions + +source_env() { + if [[ ! -f "$ZSH_DOTENV_FILE" ]]; then + return + fi + + if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then + local confirmation dirpath="${PWD:A}" + + # make sure there is an (dis-)allowed file + touch "$ZSH_DOTENV_ALLOWED_LIST" + touch "$ZSH_DOTENV_DISALLOWED_LIST" + + # early return if disallowed + if command grep -Fx -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then + return + fi + + # check if current directory's .env file is allowed or ask for confirmation + if ! command grep -Fx -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then + # get cursor column and print new line before prompt if not at line beginning + local column + echo -ne "\e[6n" > /dev/tty + read -t 1 -s -d R column < /dev/tty + column="${column##*\[*;}" + [[ $column -eq 1 ]] || echo + + # print same-line prompt and output newline character if necessary + echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) " + read -k 1 confirmation + [[ "$confirmation" = $'\n' ]] || echo + + # check input + case "$confirmation" in + [nN]) return ;; + [aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;; + [eE]) echo "$dirpath" >> "$ZSH_DOTENV_DISALLOWED_LIST"; return ;; + *) ;; # interpret anything else as a yes + esac + fi + fi + + # test .env syntax + zsh -fn $ZSH_DOTENV_FILE || { + echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2 + return 1 + } + + setopt localoptions allexport + source $ZSH_DOTENV_FILE +} + +autoload -U add-zsh-hook +add-zsh-hook chpwd source_env + +source_env |