Skip to content

Instantly share code, notes, and snippets.

@thapakazi
Forked from ffevotte/gist:9345586
Created August 8, 2018 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thapakazi/6556642b392448d9d01268ffd0f6c0b3 to your computer and use it in GitHub Desktop.
Save thapakazi/6556642b392448d9d01268ffd0f6c0b3 to your computer and use it in GitHub Desktop.
(defun source (filename)
"Update environment variables from a shell source file."
(interactive "fSource file: ")
(message "Sourcing environment from `%s'..." filename)
(with-temp-buffer
(shell-command (format "diff -u <(true; export) <(source %s; export)" filename) '(4))
(let ((envvar-re "declare -x \\([^=]+\\)=\\(.*\\)$"))
;; Remove environment variables
(while (search-forward-regexp (concat "^-" envvar-re) nil t)
(let ((var (match-string 1)))
(message "%s" (prin1-to-string `(setenv ,var nil)))
(setenv var nil)))
;; Update environment variables
(goto-char (point-min))
(while (search-forward-regexp (concat "^+" envvar-re) nil t)
(let ((var (match-string 1))
(value (read (match-string 2))))
(message "%s" (prin1-to-string `(setenv ,var ,value)))
(setenv var value)))))
(message "Sourcing environment from `%s'... done." filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment