Created
June 19, 2015 17:15
-
-
Save offby1/db24ef4a448c16d5bb1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require 'cl) | |
| (defun set-env-in-subshells (varname value) | |
| (let ((shell-command (concat "export " | |
| (shell-quote-argument varname) | |
| "=" | |
| (let ((v (shell-quote-argument value))) | |
| (if (and (equal varname "PATH") running-on-windows) | |
| (let ((cygpath-command (format "cygpath --path %s" v))) | |
| (shell-quote-argument (car (split-string (shell-command-to-string cygpath-command))))) | |
| v)) | |
| "\n"))) | |
| (mapcar (lambda (b) | |
| (with-current-buffer b | |
| (case major-mode | |
| ((eshell-mode) | |
| (setenv varname value)) | |
| ((shell-mode) | |
| ;; of course we're assuming it's Bash here. | |
| (let ((proc (get-buffer-process b))) | |
| (when proc | |
| (comint-send-string proc shell-command))))))) | |
| (buffer-list)))) | |
| (defun happy () | |
| (= 0 | |
| (call-process | |
| "ssh-add" | |
| nil ;;input | |
| nil ;; output | |
| nil ;;do redisplay | |
| "-l"))) | |
| (defun snarf-ssh-auth-stuff () | |
| "Snarf agent data from keychain or some file, and set | |
| environment variables appropriately" | |
| (interactive) | |
| (when (executable-find "ssh-add") | |
| (when (not (happy)) | |
| (some (lambda (fn) | |
| (with-temp-buffer | |
| (and (file-readable-p fn) | |
| (equal (user-uid) (elt (file-attributes fn) 2)) | |
| (progn | |
| (insert-file-contents fn) | |
| (goto-char (point-min)) | |
| (save-excursion | |
| (while (search-forward ";" (point-max) t) | |
| (replace-match "\n") | |
| )) | |
| (keep-lines "=") | |
| (while (not (eobp)) | |
| (when (looking-at (rx (and | |
| (submatch (+? (not (any "=")))) | |
| "=" | |
| (submatch (+ (not (any ";\n")))) | |
| ))) | |
| (let ((varname (match-string 1)) | |
| (value (match-string 2))) | |
| (setenv varname value) | |
| (set-env-in-subshells varname value) | |
| )) | |
| (forward-line 1)) | |
| (or (happy) | |
| (progn | |
| (if (fboundp 'lwarn) | |
| (lwarn | |
| 'stale-ssh-auth-data | |
| :warning | |
| "The data in %s don't appear to point to a working ssh-agent" | |
| fn) | |
| (error fn | |
| "The data don't appear to point to a working ssh-agent")) | |
| nil)))))) | |
| (cons | |
| (expand-file-name "~/ssh-auth-stuff") | |
| (condition-case error-descr | |
| (directory-files | |
| "~/.keychain/" | |
| t | |
| (rx (and "-sh" (zero-or-one (and "-" (any "A-Za-z0-9")))))) | |
| (file-error (progn (message (error-message-string error-descr)) | |
| nil)))))) | |
| (if (happy) | |
| (message "Hooray: %s" | |
| (replace-regexp-in-string | |
| "\n" "" | |
| (shell-command-to-string "ssh-add -l"))) | |
| (message "Oh darn, couldn't load any SSH auth data.")))) | |
| (provide 'ssh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment