From 53a9ede65c210bf6cb985cb99ea3757243e3247a Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sun, 16 Jul 2023 15:12:30 +0200 Subject: [PATCH] Marginally refactor function to add SSH keys to current agent Does this improve readability? --- site-lisp/db-utils.el | 44 ++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index fdf31dd..ece0ccc 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -851,31 +851,37 @@ running ssh-agent and waits for the process to finish." (user-error "SSH key %s does not exist, aborting" key-file)) (with-environment-variables (("SSH_ASKPASS_REQUIRE" "never")) - (let ((proc (make-process :name "ssh-add" - :buffer nil - :command (list "ssh-add" key-file) - :filter #'(lambda (process output) - (cond - ((string= (format "Enter passphrase for %s: " - key-file) - output) - (process-send-string process (funcall password-fn)) - (process-send-string process "\n")) - ((or (save-match-data - (string-match (format "^Identity added: %s" key-file) - output)) - (string= output "\n")) - ;; Ignore harmless output - t) - (t (message "Unknown output received from ssh-agent: %s" output)))) - :sentinel #'(lambda (_ event) + + (let* ((ssh-add-handle-output #'(lambda (process output) + (cond + ((string= (format "Enter passphrase for %s: " + key-file) + output) + (process-send-string process (funcall password-fn)) + (process-send-string process "\n")) + ((or (save-match-data + (string-match (format "^Identity added: %s" key-file) + output)) + (string= output "\n")) + ;; Ignore harmless output + t) + (t (message "Unknown output received from ssh-agent: %s" output))))) + + (ssh-add-handle-event-change #'(lambda (_ event) (cond ((string= event "finished\n") (message "Successfully added %s to local SSH agent" key-file)) (t (message "Adding SSH key %s failed, ssh-add process reached state %s" key-file - event))))))) + event))))) + + (proc (make-process :name "ssh-add" + :buffer nil + :command (list "ssh-add" key-file) + :filter ssh-add-handle-output + :sentinel ssh-add-handle-event-change))) + ;; We are waiting for the process to finish, to not let its output ;; intermingle with others. XXX: is there a more standard way to wait for ;; a process to finish?