summaryrefslogtreecommitdiff
path: root/plugins/dotenv
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/dotenv')
-rw-r--r--plugins/dotenv/README.md62
-rw-r--r--plugins/dotenv/dotenv.plugin.zsh35
2 files changed, 97 insertions, 0 deletions
diff --git a/plugins/dotenv/README.md b/plugins/dotenv/README.md
new file mode 100644
index 000000000..dbc02bf61
--- /dev/null
+++ b/plugins/dotenv/README.md
@@ -0,0 +1,62 @@
+# 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.
+
+## Installation
+
+Just add the plugin to your `.zshrc`:
+
+```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.
+
+## Plugin options
+
+### 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:
+
+```
+# 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.
+
+## 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/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh
new file mode 100644
index 000000000..54036bee3
--- /dev/null
+++ b/plugins/dotenv/dotenv.plugin.zsh
@@ -0,0 +1,35 @@
+source_env() {
+ if [[ -f $ZSH_DOTENV_FILE ]]; then
+ if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
+ # confirm before sourcing file
+ local confirmation
+ # print same-line prompt and output newline character if necessary
+ echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? (Y/n) "
+ read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
+ # only bail out if confirmation character is n
+ if [[ "$confirmation" = [nN] ]]; then
+ return
+ fi
+ fi
+
+ # test .env syntax
+ zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
+
+ if [[ -o a ]]; then
+ source $ZSH_DOTENV_FILE
+ else
+ set -a
+ source $ZSH_DOTENV_FILE
+ set +a
+ fi
+ fi
+}
+
+autoload -U add-zsh-hook
+add-zsh-hook chpwd source_env
+
+if [[ -z $ZSH_DOTENV_FILE ]]; then
+ ZSH_DOTENV_FILE=.env
+fi
+
+source_env