Redo Lola service commands to use comint

This commit is contained in:
Jeremy Dormitzer 2019-11-11 17:01:43 -05:00
parent a41ea85b11
commit cdbf0e6c2f

View File

@ -297,15 +297,61 @@ Pretty-print JSON:
Load environment variables into Emacs from a shell script:
#+BEGIN_SRC emacs-lisp
(defun source-env-file (file)
(interactive "fFile: \n")
(let ((var-re "\\(.+?\\)=\\(.+\\)$"))
(defun extract-vars-from-env-file (file)
"Extracts an alist of variable name to value from
a bash script that exports environment variables."
(let ((var-re "\\(.+?\\)=\\(.+\\)$")
(env '()))
(with-temp-buffer
(shell-command (concat "source " file " > /dev/null && env")
(current-buffer))
(goto-char (point-min))
(save-match-data
(while (re-search-forward var-re nil t)
(setenv (match-string 1) (match-string 2)))))))
(push (cons (match-string 1) (match-string 2)) env))))
env))
(defun source-env-file (file)
(interactive "fFile: \n")
(let (env (extract-vars-from-env-file file))
(dolist (binding env)
(setenv (car binding) (cdr binding)))))
(defmacro with-env-from-file (file &rest body)
(declare (indent 1))
(let ((env-var (make-symbol "the-env"))
(path-var (make-symbol "the-path")))
`(let* ((,env-var (extract-vars-from-env-file ,file))
(,path-var (assoc "PATH" ,env-var))
(exec-path
(if ,path-var
(append (split-string (cdr ,path-var) ":") exec-path)
exec-path))
(process-environment
(append
(mapcar
(lambda (elt) (format "%s=%s" (car elt) (cdr elt)))
,env-var)
process-environment)))
,@body)))
(defmacro with-env (env &rest body)
(declare (indent 1))
`(let* ((process-environment
(append
(mapcar
(lambda (elt) (format "%s=%s" (car elt) (cdr elt)))
,env)
process-environment)))
,@body))
#+END_SRC
Convenience macro to run some code in a particular default-directory:
#+BEGIN_SRC emacs-lisp
(defmacro with-default-directory (dir &rest body)
(declare (indent 1))
`(let ((default-directory ,dir))
,@body))
#+END_SRC
** Persisting variables between session
@ -629,6 +675,15 @@ Emacs has a shell for every mood!
(leader-def-key "ca" #'xref-find-apropos)
(general-def 'normal "M-." #'xref-find-definitions)
#+END_SRC
* xref
After I select an xref reference, I want the xref buffer closed:
#+BEGIN_SRC emacs-lisp
(defun xref-goto-xref-and-quit ()
(interactive)
(xref-goto-xref t))
(general-def xref--xref-buffer-mode-map "RET" #'xref-goto-xref-and-quit)
#+END_SRC
* Speedbar
Speedbar is cool but having it open in a separate frame is annoying. This makes it open in a side window in the same frame:
#+BEGIN_SRC emacs-lisp
@ -2524,7 +2579,9 @@ First, install a dependency:
A nice Emacs UI over [[https://github.com/BurntSushi/ripgrep#installation][ripgrep]].
#+BEGIN_SRC emacs-lisp
(use-package deadgrep
:commands deadgrep)
:commands deadgrep
:general
(deadgrep-mode-map "SPC" leader-map))
(leader-def-key "fg" 'deadgrep)
@ -3031,6 +3088,18 @@ Some functions to make my day job easier.
(insert cmd)
(eshell-send-input)))
;; TODO add some better process handling, e.g. gracefully kill services on restart
(defun run-service (name program &rest args)
(let ((bufname (format "*%s*" name)))
(apply #'make-comint-in-buffer name bufname program nil args)
(with-current-buffer bufname (toggle-truncate-lines 1))))
(defmacro with-venv (venv &rest body)
(declare (indent 1))
`(with-env-from-file (substitute-in-file-name (format "$WORKON_HOME/%s/bin/activate" ,venv))
,@body))
(defun get-python-setup (venv)
(lambda ()
(eshell/deactivate)
@ -3039,72 +3108,71 @@ Some functions to make my day job easier.
(defun lola-server ()
(interactive)
(run-service-in-eshell
"*lola-server*"
"~/lola/lola-server"
"python bin/start_web.py"
(get-python-setup "lola-server")))
(with-venv "lola-server"
(with-default-directory "~/lola/lola-server"
(with-env-from-file "~/lola/lola-server/.env"
(run-service "lola-server" "python" "bin/start_web.py")))))
(defun lola-celery-worker ()
(defun lola-server-celery-worker ()
(interactive)
(run-service-in-eshell
"*lola-celery-worker*"
"~/lola/lola-server"
"python bin/start_celery_worker.py -P gevent"
(get-python-setup "lola-server")))
(with-venv "lola-server"
(with-default-directory "~/lola/lola-server"
(with-env-from-file "~/lola/lola-server/.env"
(run-service "lola-server-celery-worker"
"python"
"bin/start_celery_worker.py"
"-P"
"gevent")))))
(defun lola-travel-service ()
(interactive)
(run-service-in-eshell
"*lola-travel-service*"
"~/lola/lola-travel-service"
"python bin/start_web.py"
(get-python-setup "travel-service")))
(with-venv "travel-service"
(with-default-directory "~/lola/lola-travel-service"
(with-env-from-file "~/lola/lola-travel-service/.env"
(run-service "lola-travel-service" "python" "bin/start_web.py")))))
(defun lola-travel-service-celery ()
(interactive)
(run-service-in-eshell
"*lola-travel-service-celery*"
"~/lola/lola-travel-service"
"python bin/start_celery_workers.py"
(get-python-setup "travel-service")))
(with-venv "travel-service"
(with-default-directory "~/lola/lola-travel-service"
(with-env-from-file "~/lola/lola-travel-service/.env"
(run-service "lola-travel-service-celery"
"python"
"bin/start_celery_workers.py"
"-Q"
"default,io_pool,cpu_pool,priority_io_pool,priority_cpu_pool")))))
(defun lola-secrets ()
(interactive)
(run-service-in-eshell
"*lola-secrets*"
"~/lola/secrets"
"python bin/cmdline.py www"
(get-python-setup "secrets")))
(with-venv "secrets"
(with-default-directory "~/lola/secrets"
(with-env-from-file "~/lola/secrets/.env"
(run-service "lola-secrets" "python" "bin/cmdline.py" "www")))))
(defun lola-desktop (env)
(interactive
(list (ido-completing-read+
"Environment: "
'("local" "development" "staging"))))
(run-service-in-eshell
"*lola-desktop*"
"~/lola/lola-desktop"
"npm start"
(lambda () (setenv "LOLA_ENV" env))))
(with-env `(("LOLA_ENV" . ,env))
(with-default-directory "~/lola/lola-desktop"
(run-service "lola-desktop" "npm" "start"))))
(defun lola-wallet (env)
(interactive
(list (ido-completing-read+
"Environment: "
'("local" "development" "staging"))))
(run-service-in-eshell
"*lola-wallet*"
"~/lola/wallet"
"npm start"
(lambda () (setenv "LOLA_ENV" env))))
(with-env `(("LOLA_ENV" . ,env))
(with-default-directory "~/lola/wallet"
(run-service "lola-wallet" "npm" "start"))))
(defun lola-run-all ()
(interactive)
(let ((fns (list #'lola-travel-service
#'lola-travel-service-celery
#'lola-server
#'lola-celery-worker
#'lola-server-celery-worker
#'lola-desktop
#'lola-wallet
#'lola-secrets)))
@ -3112,16 +3180,21 @@ Some functions to make my day job easier.
(defun run-luigid ()
(interactive)
(run-service-in-eshell "*luigid*"
"~/lola/data-pipeline"
"luigid"
(get-python-setup "data-pipeline")))
(run-python-service
"luigid"
"~/lola/data-pipeline"
"data-pipeline"
"luigid"
'()
"~/lola/data-pipeline/.env"))
(defun run-prometheus ()
(interactive)
(run-service-in-eshell "*prometheus*"
"~/prometheus"
"prometheus --config.file=prometheus.yml"))
(run-service
"prometheus"
"~/prometheus"
"prometheus"
'("--config.file=prometheus.yml")))
(defun release-manager ()
(interactive)
@ -3132,7 +3205,7 @@ Some functions to make my day job easier.
(jdormit/define-prefix "L" "lola")
(leader-def-key "Ls" #'lola-server)
(leader-def-key "Lc" #'lola-celery-worker)
(leader-def-key "Lc" #'lola-server-celery-worker)
(leader-def-key "Lt" #'lola-travel-service)
(leader-def-key "LT" #'lola-travel-service-celery)
(leader-def-key "Ld" #'lola-desktop)