From 8d35fa0e2f32dab6894ca06bfc333af94be97ec7 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 14 Dec 2016 22:49:08 +0600 Subject: add dotenv plugin (#4373) --- plugins/dotenv/dotenv.plugin.zsh | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 plugins/dotenv/dotenv.plugin.zsh (limited to 'plugins/dotenv/dotenv.plugin.zsh') diff --git a/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh new file mode 100644 index 000000000..9dd784229 --- /dev/null +++ b/plugins/dotenv/dotenv.plugin.zsh @@ -0,0 +1,10 @@ +#!/bin/zsh + +source_env() { + if [[ -f .env ]]; then + source .env + fi +} + +autoload -U add-zsh-hook +add-zsh-hook chpwd source_env -- cgit v1.2.3-70-g09d2 From 7cb5fa8aea3d325fee08e3c1708abd12cdea1c1c Mon Sep 17 00:00:00 2001 From: Arthur <192247+arteezy@users.noreply.github.com> Date: Wed, 11 Jul 2018 03:31:47 +0600 Subject: Fix dotenv plugin accepted file format and clarify README (#6093) * Fix dotenv plugin accepted file format * clarify README and add disclaimer section --- plugins/dotenv/README.md | 19 +++++++++++++++---- plugins/dotenv/dotenv.plugin.zsh | 8 +++++++- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'plugins/dotenv/dotenv.plugin.zsh') diff --git a/plugins/dotenv/README.md b/plugins/dotenv/README.md index ade09fbb2..e0e75571f 100644 --- a/plugins/dotenv/README.md +++ b/plugins/dotenv/README.md @@ -2,19 +2,19 @@ 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](http://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. +Storing configuration in the environment is one of the tenets of a [twelve-factor app](http://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=(git man dotenv) +plugins=(... dotenv) ``` ## Usage -Create `.env` file inside your project directory and put your local ENV variables there. +Create `.env` file inside your project root directory and put your ENV variables there. For example: ```sh @@ -30,5 +30,16 @@ 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. -**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 supposed to be local only. +## 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 index 9dd784229..fa47c4c68 100644 --- a/plugins/dotenv/dotenv.plugin.zsh +++ b/plugins/dotenv/dotenv.plugin.zsh @@ -2,7 +2,13 @@ source_env() { if [[ -f .env ]]; then - source .env + if [[ -o a ]]; then + source .env + else + set -a + source .env + set +a + fi fi } -- cgit v1.2.3-70-g09d2 From 9ecde7f73211607353954b6fd76fef56d7e663b3 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 7 Aug 2018 23:54:07 +0200 Subject: dotenv: call function on startup Fixes #7017 --- plugins/dotenv/dotenv.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/dotenv/dotenv.plugin.zsh') diff --git a/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh index fa47c4c68..a0c2d0051 100644 --- a/plugins/dotenv/dotenv.plugin.zsh +++ b/plugins/dotenv/dotenv.plugin.zsh @@ -1,5 +1,3 @@ -#!/bin/zsh - source_env() { if [[ -f .env ]]; then if [[ -o a ]]; then @@ -14,3 +12,5 @@ source_env() { autoload -U add-zsh-hook add-zsh-hook chpwd source_env + +source_env -- cgit v1.2.3-70-g09d2 From c781d708da064b42af19e20113d042b65e886d94 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Wed, 8 Aug 2018 00:05:34 +0200 Subject: dotenv: test and warn of incorrect.env syntax Fixes #6337 --- plugins/dotenv/dotenv.plugin.zsh | 3 +++ 1 file changed, 3 insertions(+) (limited to 'plugins/dotenv/dotenv.plugin.zsh') diff --git a/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh index a0c2d0051..b701b5596 100644 --- a/plugins/dotenv/dotenv.plugin.zsh +++ b/plugins/dotenv/dotenv.plugin.zsh @@ -1,5 +1,8 @@ source_env() { if [[ -f .env ]]; then + # test .env syntax + zsh -fn .env || echo 'dotenv: error when sourcing `.env` file' >&2 + if [[ -o a ]]; then source .env else -- cgit v1.2.3-70-g09d2 From f960e2be6f01abe5f185d668be661b57051322ac Mon Sep 17 00:00:00 2001 From: Arshad Kazmi Date: Mon, 20 May 2019 02:15:27 +0530 Subject: dotenv: add support for custom env file names (#7861) --- plugins/dotenv/README.md | 11 +++++++++++ plugins/dotenv/dotenv.plugin.zsh | 12 ++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) (limited to 'plugins/dotenv/dotenv.plugin.zsh') diff --git a/plugins/dotenv/README.md b/plugins/dotenv/README.md index e880e9d69..cac552485 100644 --- a/plugins/dotenv/README.md +++ b/plugins/dotenv/README.md @@ -32,6 +32,17 @@ PORT=3001 ``` You can even mix both formats, although it's probably a bad idea. +### 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 +``` + ## 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. diff --git a/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh index b701b5596..89763d0ee 100644 --- a/plugins/dotenv/dotenv.plugin.zsh +++ b/plugins/dotenv/dotenv.plugin.zsh @@ -1,13 +1,13 @@ source_env() { - if [[ -f .env ]]; then + if [[ -f $ZSH_DOTENV_FILE ]]; then # test .env syntax - zsh -fn .env || echo 'dotenv: error when sourcing `.env` file' >&2 + zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2 if [[ -o a ]]; then - source .env + source $ZSH_DOTENV_FILE else set -a - source .env + source $ZSH_DOTENV_FILE set +a fi fi @@ -16,4 +16,8 @@ source_env() { autoload -U add-zsh-hook add-zsh-hook chpwd source_env +if [[ -z $ZSH_DOTENV_FILE ]]; then + ZSH_DOTENV_FILE=.env +fi + source_env -- cgit v1.2.3-70-g09d2