Add process handling utils

This commit is contained in:
Jeremy Dormitzer 2019-04-23 09:02:42 -04:00
parent 4bc386bef0
commit 0dc7323f17

View File

@ -252,6 +252,26 @@ And this function persists a variable:
(prin1-to-string vars-plist) nil file)))))
#+END_SRC
** Process handling
Some utilities for calling out to other processes.
#+BEGIN_SRC emacs-lisp
(defun make-process-sentinel (success err)
"Makes a process sentinel that calls `success` on success and `err` on error"
(lambda (proc event)
(cond ((string-match-p "finished" event) (funcall success))
(t (funcall err)))))
(defun make-success-err-msg-sentinel (buf success-msg err-msg &optional kill-on-err)
(make-process-sentinel
(lambda ()
(message success-msg)
(kill-buffer buf))
(lambda ()
(message err-msg)
(when kill-on-err
(kill-buffer buf)))))
#+END_SRC
* Customization File
I don't want anything to write to my init.el, so save customizations in a separate file:
#+BEGIN_SRC emacs-lisp
@ -2604,13 +2624,6 @@ Syntax highlighting for Dockerfiles:
* AWS
** S3
#+BEGIN_SRC emacs-lisp
(defun save-to-s3-sentinel (proc event)
(cond ((string-match-p "finished" event)
(progn
(message (concat "Uploaded to " last-s3-url))
(kill-buffer "*aws-s3*")))
(t (message "Failed to upload to s3, check *aws-s3* buffer for details"))))
(defun save-to-s3 (bucket public)
(interactive "MS3 bucket URL (e.g. s3://bucket/filename): \nP")
(let* ((tmp-file (make-temp-file "s3-upload"))
@ -2618,7 +2631,11 @@ Syntax highlighting for Dockerfiles:
(setq last-s3-url bucket)
(write-region (point-min) (point-max) tmp-file)
(let ((proc (apply #'start-process "aws-s3" "*aws-s3*" cmd)))
(set-process-sentinel proc #'save-to-s3-sentinel))))
(set-process-sentinel
proc
(make-success-err-msg-sentinel "*aws-s3*"
(format "Uploaded to %s" last-s3-url)
"Failed to upload to s3, check *aws-s3* buffer for details")))))
#+END_SRC
* Lola
Some functions to make my day job easier.
@ -2722,19 +2739,17 @@ Some functions to make my day job easier.
** AWS-MFA
The aws-mfa command:
#+BEGIN_SRC emacs-lisp
(defun aws-mfa-sentinel (proc event)
(cond ((string-match-p "finished" event)
(progn
(message "AWS MFA succeeded")
(kill-buffer "*aws-mfa*")))
(t
(progn
(message "AWS MFA failed, check *aws-mfa* buffer for details")))))
(defun aws-mfa (mfa-token)
(interactive "MMFA code: ")
(let ((proc (start-process "aws-mfa" "*aws-mfa*" "/Users/jdormit/.virtualenvs/k8s-deploy/bin/aws-mfa" "--force")))
(set-process-sentinel proc #'aws-mfa-sentinel)
(let ((proc (start-process "aws-mfa"
"*aws-mfa*"
"/Users/jdormit/.virtualenvs/k8s-deploy/bin/aws-mfa"
"--force")))
(set-process-sentinel
proc
(make-success-err-msg-sentinel "*aws-mfa*"
"AWS MFA succeeded"
"AWS MFA failed, check *aws-mfa* buffer for details"))
(process-send-string proc (concat mfa-token "\n"))))
(with-eval-after-load 'kubernetes