Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

profile management #209

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions autoload/gist.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand All @@ -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(...) 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
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -783,6 +816,8 @@ function! gist#Gist(count, bang, line1, line2, ...) abort
if arg =~# '^\(-h\|--help\)$\C'
help :Gist
return
elseif arg =~# '^\(--profile\|-f\)'
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 [email protected]:%s', b:gist['id'])
return
Expand Down Expand Up @@ -962,8 +997,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
Expand All @@ -974,7 +1009,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
Expand Down Expand Up @@ -1012,9 +1047,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 = ''
Expand Down
59 changes: 54 additions & 5 deletions doc/gist-vim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -293,19 +297,64 @@ 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:
>
$ git config --global github.user Username
<
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
Expand Down
1 change: 1 addition & 0 deletions plugin/gist.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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(<count>, "<bang>", <line1>, <line2>, <f-args>)
command! -nargs=? -complete=customlist,gist#list_profiles GistProfile call gist#select_profile(<q-args>)

" vim:set et: