From 9a48e7b6c35e3f8eabfdd21d9b302de7e0771668 Mon Sep 17 00:00:00 2001 From: Sha Liu Date: Wed, 11 Oct 2017 22:28:13 +0800 Subject: [PATCH 1/3] add functions to select different profiles --- autoload/gist.vim | 74 +++++++++++++++++++++++++++++++++++------------ plugin/gist.vim | 1 + 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/autoload/gist.vim b/autoload/gist.vim index 6727bc0..1177174 100644 --- a/autoload/gist.vim +++ b/autoload/gist.vim @@ -32,17 +32,22 @@ else call webapi#json#true() endif -let s:gist_token_file = expand(get(g:, 'gist_token_file', '~/.gist-vim')) +" a profile defines: gist_api_url, github_user and the gist_token_file +let s:gist_profiles = get(g:, 'gist_profiles', {}) let s:system = function(get(g:, 'webapi#system_function', 'system')) -if !exists('g:github_user') - let g:github_user = substitute(s:system('git config --get github.user'), "\n", '', '') - if strlen(g:github_user) == 0 - let g:github_user = $GITHUB_USER - end -endif +function! s:set_default_github_user() + let g:github_user = substitute(s:system('git config --get github.user'), "\n", '', '') + if strlen(g:github_user) == 0 + let g:github_user = $GITHUB_USER + end +endfunction + +function! gist#list_profiles(arg_lead,cmdline,cursor_pos) + return keys(s:gist_profiles) +endfunction -if !exists('g:gist_api_url') +function! s:set_default_api_url() let g:gist_api_url = substitute(s:system('git config --get github.apiurl'), "\n", '', '') if strlen(g:gist_api_url) == 0 let g:gist_api_url = 'https://api.github.com/' @@ -61,10 +66,38 @@ if !exists('g:gist_api_url') redraw! endif endif -endif -if g:gist_api_url !~# '/$' - let g:gist_api_url .= '/' -endif +endfunction + +function! gist#select_profile(...) + let profile_name = a:0 ? a:1 : '' + let profile = get(s:gist_profiles, profile_name, []) + if len(profile) == 2 + let [g:gist_api_url, g:github_user] = profile + let g:gist_token_dir = expand(get(g:, 'gist_token_dir', '~/.config/gist-vim')) + if !isdirectory(g:gist_token_dir) + call mkdir(g:gist_token_dir) + endif + let g:gist_token_file = expand(simplify(g:gist_token_dir . '/' . profile_name)) + else + echomsg 'gist: profile ' . profile_name . ' does not exist, fallback to default' + let g:gist_api_url = '' + let g:github_user = '' + let g:gist_token_file = '~/.gist-vim' + endif + if g:gist_api_url is '' + call s:set_default_api_url() + endif + if g:github_user is '' + call s:set_default_github_user() + endif + if g:gist_api_url !~# '/$' + let g:gist_api_url .= '/' + endif + echom 'g:gist_api_url:' . g:gist_api_url + echom 'g:github_user:' . g:github_user + echom 'g:gist_token_file:' . g:gist_token_file +endfunction +call gist#select_profile() if !exists('g:gist_update_on_write') let g:gist_update_on_write = 1 @@ -228,7 +261,7 @@ function! s:GistList(gistls, page) abort redraw echohl ErrorMsg | echomsg content.message | echohl None if content.message ==# 'Bad credentials' - call delete(s:gist_token_file) + call delete(g:gist_token_file) endif return endif @@ -783,6 +816,11 @@ function! gist#Gist(count, bang, line1, line2, ...) abort if arg =~# '^\(-h\|--help\)$\C' help :Gist return + elseif arg =~# '^\(--profile\|-f\)' + if a:0 >= 2 + call s:select_profile(a:2) + endif + return elseif arg =~# '^\(-g\|--git\)$\C' && gistidbuf !=# '' && g:gist_api_url ==# 'https://api.github.com/' && has_key(b:, 'gist') && has_key(b:gist, 'id') echo printf('git clone git@github.com:%s', b:gist['id']) return @@ -962,8 +1000,8 @@ function! s:GistGetAuthHeader() abort return printf('basic %s', webapi#base64#b64encode(g:github_user.':'.password)) endif let auth = '' - if filereadable(s:gist_token_file) - let str = join(readfile(s:gist_token_file), '') + if filereadable(g:gist_token_file) + let str = join(readfile(g:gist_token_file), '') if type(str) == 1 let auth = str endif @@ -974,7 +1012,7 @@ function! s:GistGetAuthHeader() abort redraw echohl WarningMsg - echo 'Gist.vim requires authorization to use the GitHub API. These settings are stored in "~/.gist-vim". If you want to revoke, do "rm ~/.gist-vim".' + echo 'Gist.vim requires authorization to use the GitHub API. These settings are stored in "' . g:gist_token_file . '". If you want to revoke, do "rm ' . g:gist_token_file . '".' echohl None let password = inputsecret('GitHub Password for '.g:github_user.':') if len(password) == 0 @@ -1012,9 +1050,9 @@ function! s:GistGetAuthHeader() abort let authorization = webapi#json#decode(res.content) if has_key(authorization, 'token') let secret = printf('token %s', authorization.token) - call writefile([secret], s:gist_token_file) + call writefile([secret], g:gist_token_file) if !(has('win32') || has('win64')) - call system('chmod go= '.s:gist_token_file) + call system('chmod go= '.g:gist_token_file) endif elseif has_key(authorization, 'message') let secret = '' diff --git a/plugin/gist.vim b/plugin/gist.vim index 4aa674d..9a0ada1 100644 --- a/plugin/gist.vim +++ b/plugin/gist.vim @@ -19,5 +19,6 @@ function! s:CompleteArgs(arg_lead,cmdline,cursor_pos) endfunction command! -nargs=? -range=% -bang -complete=customlist,s:CompleteArgs Gist :call gist#Gist(, "", , , ) +command! -nargs=? -complete=customlist,gist#list_profiles GistProfile call gist#select_profile() " vim:set et: From f6bea1d1c03df9ea8941324ec9231b6d789d19d3 Mon Sep 17 00:00:00 2001 From: Sha Liu Date: Thu, 12 Oct 2017 21:53:20 +0800 Subject: [PATCH 2/3] introduce g:gist_default_profile --- autoload/gist.vim | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/autoload/gist.vim b/autoload/gist.vim index 1177174..9f19598 100644 --- a/autoload/gist.vim +++ b/autoload/gist.vim @@ -68,8 +68,8 @@ function! s:set_default_api_url() endif endfunction -function! gist#select_profile(...) - let profile_name = a:0 ? a:1 : '' +function! gist#select_profile(...) abort + let profile_name = a:0 ? a:1 : get(g:, 'gist_default_profile', '') let profile = get(s:gist_profiles, profile_name, []) if len(profile) == 2 let [g:gist_api_url, g:github_user] = profile @@ -817,10 +817,7 @@ function! gist#Gist(count, bang, line1, line2, ...) abort help :Gist return elseif arg =~# '^\(--profile\|-f\)' - if a:0 >= 2 - call s:select_profile(a:2) - endif - return + call call('gist#select_profile', a:000[1:]) elseif arg =~# '^\(-g\|--git\)$\C' && gistidbuf !=# '' && g:gist_api_url ==# 'https://api.github.com/' && has_key(b:, 'gist') && has_key(b:gist, 'id') echo printf('git clone git@github.com:%s', b:gist['id']) return From a5808bca1386411b7413c2a36ede6e2279b23e85 Mon Sep 17 00:00:00 2001 From: Sha Liu Date: Thu, 12 Oct 2017 21:51:40 +0800 Subject: [PATCH 3/3] add doc for gist-vim-setup-profile --- doc/gist-vim.txt | 59 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/doc/gist-vim.txt b/doc/gist-vim.txt index c7005a7..4a045c4 100644 --- a/doc/gist-vim.txt +++ b/doc/gist-vim.txt @@ -15,6 +15,10 @@ For the latest version please see https://github.com/mattn/gist-vim. ============================================================================== USAGE *:Gist* *gist-vim-usage* +- Select profile (see also |gist-vim-setup|) > + + :Gist --profile [profilename] +> - Post current buffer to gist, using default privacy option. > :Gist @@ -293,10 +297,55 @@ REQUIREMENTS *gist-vim-requirements* - and, if you want to use your git profile, the git command-line client. ============================================================================== -SETUP *gist-vim-setup* +SETUP *gist-vim-setup* + +This plugin uses GitHub API v3. gist-vim provides two ways to authenticate +against the GitHub APIs. + + *gist-vim-setup-profile* +Method 1~ + +You define the following variables +> + let g:gist_profiles = { + \ 'github' : ['', 'username'], + \ 'enterprise' : ['https://mycompany.com/api/v3', 'employeeid'] + } -This plugin uses GitHub API v3. The authentication value is stored in `~/.gist-vim`. -gist-vim provides two ways to authenticate against the GitHub APIs. + let g:gist_token_dir = '~/.config/gist-vim/' (default value) + + let g:gist_default_profile = 'enterprise' (no default value) +< +and use +> + :Gist --profile [profilename] +< +to select one profile. If `profilename` is not given, it fallbacks to +`g:gist_default_profile` if it is defined. Otherwise, |gist-vim-setup-old| will +be used. `:Gist --profile` is implicitly called when the script is autoloaded. + +Each profile defines the github api url and the login +username. If the api url is `''`, it defaults to ` 'https://api.github.com/'` + +Then, gist.vim will ask for your password to create an authorization when you +first use it. The password is not stored and only the OAuth access token will +be kept for later use. The token will be stored at +> + g:gist_token_dir/profilename +< +You can revoke the token at any time from the list of +"Authorized applications" on GitHub's "Account Settings" page. +(https://github.com/settings/applications) + +If the profile is not correctly defined, for example, key does not exists or +the list does not contain two elements, `:Gist --profile [profilename]` will +fallback to the |gist-vim-setup-old| method. + + + *gist-vim-setup-old* +Method 2~ + +This method does not allow you to have more than one profile. First, you need to set your GitHub username in global git config: > @@ -304,8 +353,8 @@ First, you need to set your GitHub username in global git config: < Then, gist.vim will ask for your password to create an authorization when you first use it. The password is not stored and only the OAuth access token will -be kept for later use. You can revoke the token at any time from the list of -"Authorized applications" on GitHub's "Account Settings" page. +be kept at `~/.gist-vim` for later use. You can revoke the token at any time +from the list of "Authorized applications" on GitHub's "Account Settings" page. (https://github.com/settings/applications) If you have two-factor authentication enabled on GitHub, you'll see the message