Skip to content
Browse files

use global git config for username + token

  • Loading branch information...
1 parent 3705426 commit 5d117aa7292a2bac52c155a3195f789f6ab99052 @defunkt defunkt committed Oct 10, 2008
Showing with 46 additions and 26 deletions.
  1. +46 −26 gist.el
View
72 gist.el
@@ -56,7 +56,7 @@ Copies the URL into the kill ring."
(file-name-extension file)
"txt"))
(output (generate-new-buffer " *gist*"))
- (login (get-github-user-info))
+ (login (github-auth-string))
(do-private (if private "-F private=1" "")))
(shell-command-on-region
begin end
@@ -81,27 +81,47 @@ Copies the URL into the kill ring."
(interactive "r")
(gist-region begin end t))
-(defun get-github-user-info (&optional github-user github-key)
- "Asks the user for their github username and api key. If they
-don't have them, or wish to paste anonymously, they can do so.
-Totally not required."
+(defun github-raw-auth-string (user token)
+ "Given a username and API token, returns a curl-friendly string."
+ (if (and (> (length user) 0) (> (length token) 0))
+ (format "-F 'login=%s' -F 'token=%s'" user token)))
+
+(defun github-config (key)
+ "Returns a GitHub specific value from the global Git config."
+ (let ((strip (lambda (string)
+ (if (> (length string) 0)
+ (substring string 0 (- (length string) 1))))))
+ (funcall strip (shell-command-to-string
+ (concat "git config --global github." key)))))
+
+(defun github-set-config (key value)
+ "Sets a GitHub specific value to the global Git config."
+ (shell-command-to-string (format "git config --global github.%s %s" key value)))
+
+(defun github-auth-string ()
+ "Returns a curl-friendly GitHub auth string.
+Searches for a GitHub username and token in the global git config.
+If nothing is found, prompts for the info then sets it to the git config."
(interactive)
- (cond ((and (not (string= github-username ""))
- (not (string= github-api-key "")))
- (format "-F 'login=%s' -F 'token=%s'" github-username github-api-key))
- ((and (> (length github-user) 1)
- (> (length github-key) 1))
- (format "-F 'login=%s' -F 'token=%s'" github-user github-key))
- (t
- (if (or (not github-user)
- (not github-key))
- (let ((github-user (read-string "GitHub username?: "))
- (github-key (read-string "GitHub API key?: ")))
- (if (or (string= github-user "")
- (string= github-key ""))
- ""
- (format "-F 'login=%s' -F 'token=%s'" github-user github-key))))
- "")))
+
+ (let* ((user (github-config "user"))
+ (token (github-config "token"))
+ (auth-string (github-raw-auth-string user token)))
+
+ (if (> (length auth-string) 0)
+ auth-string
+
+ (cond
+ ((not user)
+ (setq user (read-string "GitHub username: "))
+ (github-set-config "user" user)))
+
+ (cond
+ ((not token)
+ (setq token (read-string "GitHub API token: "))
+ (github-set-config "token" token)))
+
+ (github-raw-auth-string user token))))
;;;###autoload
(defun gist-buffer ()
@@ -123,7 +143,7 @@ Copies the URL into the kill ring."
Copies the URL into the kill ring."
(interactive)
(condition-case nil
- (gist-region (point) (mark))
+ (gist-region (point) (mark))
(mark-inactive (gist-buffer))))
;;;###autoload
@@ -132,7 +152,7 @@ Copies the URL into the kill ring."
Copies the URL into the kill ring."
(interactive)
(condition-case nil
- (gist-region-private (point) (mark))
+ (gist-region-private (point) (mark))
(mark-inactive (gist-buffer-private))))
(defvar gist-fetch-url "http://gist.github.com/%d.txt"
@@ -144,15 +164,15 @@ Copies the URL into the kill ring."
If the Gist already exists in a buffer, switches to it"
(interactive "nGist ID: ")
- (let* ((gist-buffer-name (format "*gist %d*" id))
+ (let* ((gist-buffer-name (format "*gist %d*" id))
(gist-buffer (get-buffer gist-buffer-name)))
(if (bufferp gist-buffer)
(switch-to-buffer-other-window gist-buffer)
(progn
(message "Fetching Gist %d..." id)
- (setq gist-buffer
+ (setq gist-buffer
(url-retrieve-synchronously (format gist-fetch-url id)))
- (with-current-buffer gist-buffer
+ (with-current-buffer gist-buffer
(rename-buffer gist-buffer-name t)
(beginning-of-buffer)
(search-forward-regexp "\n\n")

0 comments on commit 5d117aa

Please sign in to comment.
Something went wrong with that request. Please try again.