Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Created June 13, 2016 10:12
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 sideshowcoder/2a30b1ef02dfd2813014f42269165bf9 to your computer and use it in GitHub Desktop.
Save sideshowcoder/2a30b1ef02dfd2813014f42269165bf9 to your computer and use it in GitHub Desktop.
Setup docker-machine env for Emacs to use
(defun setup-docker-machine-env ()
" Parse the output of docker-machine env and setup the needed env vars"
(interactive)
(mapcar
(lambda (e)
(if e
(let ((var-value (split-string-and-unquote (car e) "=")))
(setenv (s-chop-suffix "=" (car var-value)) (car (cdr var-value))))))
(mapcar
(lambda (e)
(cdr (s-split " " e 't)))
(remove-if
(lambda (e)
(s-starts-with? "#"e))
(s-lines (shell-command-to-string "docker-machine env"))))))
@Qquanwei
Copy link

Qquanwei commented Oct 17, 2017

thanks !
but in windows split-string-and-unquote not work very well.

(shell-command-to-string "docker-machine env")
"export DOCKER_TLS_VERIFY=\"1\"
export DOCKER_HOST=\"tcp://192.168.99.101:2376\"
export DOCKER_CERT_PATH=\"C:\\Users\\liquanwei\\.docker\\machine\\machines\\default\"
export DOCKER_MACHINE_NAME=\"default\"
export COMPOSE_CONVERT_WINDOWS_PATHS=\"true\"
# Run this command to configure your shell: 
# eval $(\"C:\\ProgramData\\chocoportable\\lib\\docker-machine\\bin\\docker-machine.exe\" env)
"

eval setup-docker-machine-env throw a error

split-string-and-unquote: Non-hex digit used for Unicode escape

so i decide to using split-string replace split-string-and-unquote and import s.el help me replace quote

(require 's)

(defun setup-docker-machine-env ()
  " Parse the output of docker-machine env and setup the needed env vars"
  (interactive)
  (mapcar
    (lambda (e)
     (if e
       (let ((var-value (split-string (car e) "=")))
           (setenv (s-chop-suffix "=" (car var-value)) (s-replace "\"" "" (car (cdr var-value)))))))
   (mapcar
    (lambda (e)
      (cdr (s-split " " e 't)))
    (remove-if
      (lambda (e)
       (s-starts-with? "#"e))
      (s-lines (shell-command-to-string "docker-machine env"))))))

(provide 'docker-machine-setup)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment