Skip to content

Dotenv - Emacs plugin for loading project's .env file.

License

Notifications You must be signed in to change notification settings

pkulev/dotenv.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotenv.el — Plugin for loading .env project files.

Dotenv parser by following rules:

  • skip empty lines
  • trim every string (all spaces before first non-whitespace char and after last non-whitespace char will be removed)
  • skip commentary lines (# at the line start)
  • skip lines which doesn’t look like an proper assignment
  • variable name and its value will be trimmed

Installation

use-package

(use-package dotenv
  :ensure nil
  :quelpa
  (dotenv :repo "pkulev/dotenv.el"
          :fetcher github :upgrade t))

Examples

Integration with projectile

Hook for loading project’s .env (if exists) after switching project via projectile.

(use-package dotenv
  :ensure nil
  :after projectile
  :quelpa
  (dotenv :repo "pkulev/dotenv.el"
          :fetcher github :upgrade t)
  :config
  (defun dotenv-projectile-hook ()
   "Projectile hook."
   (dotenv-update-project-env (projectile-project-root)))

  (add-to-list 'projectile-after-switch-project-hook #'dotenv-projectile-hook))

Transform variable name or contents using dotenv-transform-alist

In this example .env file contains PYTHONPATH among other vaiables. Assuming that PYTHONPATH contains list of pathes (delimeted by ’:’) relative to the project’s root we can transform it into absolute. To do that add to dotenv-transform-alist a pair of a predicate function and a transform function. Predicate function takes k and v and returns t or nil, transform function takes k and v and returns two-element list with new values for k and v.

(use-package dotenv
  :ensure nil
  :after projectile
  :quelpa
  (dotenv :repo "pkulev/dotenv.el"
          :fetcher github :upgrade t)
  :config
  (defun dotenv-absolutify-pythonpath (k v)
    (list k (dotenv-absolutify-path-var-in-project v)))

  (add-to-list
   'dotenv-transform-alist
   '((lambda (k v) (string= k "PYTHONPATH")) . dotenv-absolutify-pythonpath))

  (defun dotenv-projectile-hook ()
   "Projectile hook."
   (dotenv-update-project-env (projectile-project-root)))

  (add-to-list 'projectile-after-switch-project-hook #'dotenv-projectile-hook))

API reference

Customizable variables

nametypedescription
dotenv-file-namestringDotenv file name (default: .env).
dotenv-transform-alistalistList of predicate-transform pairs for
custom key or/and value processing.

Public functions

nameargumentsreturnsdescription
dotenv-loadabs-path (str)list of (k . v) pairsLoad .env from file.
dotenv-getkey (str), path (str)stringReturns string value
by provided key.
dotenv-setTODOTODOTODO
dotenv-update-project-envproject-root (str)nilUpdates project environment
with .env.
dotenv-update-envenv-pairs, ?overrideTODOTODO
dotenv-dump-env

About

Dotenv - Emacs plugin for loading project's .env file.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published