Skip to content

Commit 9ccc3cc

Browse files
committed
Add support for reading API tokens from ~/.arcrc
If we're not able to read an API token for the current Phabricator host from this configuration file, we fall back on using the value stored in g:phabricator_api_token. Closes #2
1 parent 7296b9c commit 9ccc3cc

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ generated in Phabricator's per-user Settings interface. For example:
4949

5050
https://phabricator.example.com/settings/user/USERNAME/page/apitokens/
5151

52+
The plugin will first attempt to read the per-host API token from the user's
53+
`~/.arcrc` configuration file. For example:
54+
55+
```json
56+
{
57+
"hosts": {
58+
"https://secure.phabricator.com/api/": {
59+
"token": "api-secrettokencharacters"
60+
}
61+
}
62+
}
63+
```
64+
65+
If an API token can not be read from the `~/.arcrc` file for the current
66+
Phabricator host, the value stored in `g:phabricator_api_token` will be used.
67+
5268
[conduit]: https://secure.phabricator.com/book/phabricator/article/conduit/
5369

5470
## License

autoload/phabricator.vim

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ function! s:api_root() abort
6161
return api_root
6262
endfunction
6363

64+
function! s:api_token(api_root) abort
65+
if exists('b:phabricator_api_token')
66+
return b:phabricator_api_token
67+
endif
68+
try
69+
let json = json_decode(join(readfile(expand('~/.arcrc')), ' '))
70+
let host = get(get(json, 'hosts', {}), a:api_root, {})
71+
let token = get(host, 'token', get(g:, 'phabricator_api_token'))
72+
catch
73+
let token = get(g:, 'phabricator_api_token')
74+
endtry
75+
let b:phabricator_api_token = token
76+
return token
77+
endfunction
78+
6479
function! s:request(method, order, query) abort
6580
if !executable('curl')
6681
call s:throw('cURL is required')
@@ -69,7 +84,11 @@ function! s:request(method, order, query) abort
6984
call s:throw('json_decode() is required')
7085
endif
7186

72-
let token = get(g:, 'phabricator_api_token', '')
87+
let api_root = s:api_root()
88+
if empty(api_root)
89+
call s:throw('could not determine Conduit (API) URL')
90+
endif
91+
let token = s:api_token(api_root)
7392
if empty(token)
7493
call s:throw('missing API token')
7594
endif
@@ -84,7 +103,7 @@ function! s:request(method, order, query) abort
84103
if !empty(a:query)
85104
call extend(args, ['-d', 'constraints[query]=core%3A~"' . a:query . '"'])
86105
endif
87-
call add(args, s:api_root() . a:method)
106+
call add(args, api_root . a:method)
88107

89108
let data = system('curl ' . join(args))
90109

doc/phabricator.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,20 @@ CONFIGURATION *phabricator-config*
3333

3434
https://phabricator.example.com/settings/user/USERNAME/page/apitokens/
3535

36+
The plugin will first attempt to read the per-host API token from the user's
37+
`~/.arcrc` configuration file. For example:
38+
39+
{
40+
"hosts": {
41+
"https://secure.phabricator.com/api/": {
42+
"token": "api-secrettokencharacters"
43+
}
44+
}
45+
}
46+
47+
If an API token can not be read from the `~/.arcrc` file for the current
48+
Phabricator host, the value stored in |g:phabricator_api_token| will be
49+
used.
50+
3651
------------------------------------------------------------------------------
3752
vim:tw=78:ft=help:norl:

0 commit comments

Comments
 (0)