Merge pull request #337 from samdmarshall/github-api-token-env-var
Access GitHub API token through environment variable
This commit is contained in:
commit
c3c845f632
1 changed files with 36 additions and 15 deletions
|
|
@ -16,6 +16,7 @@ type
|
|||
|
||||
const
|
||||
ApiKeyFile = "github_api_token"
|
||||
ApiTokenEnvironmentVariable = "NIMBLE_GITHUB_API_TOKEN"
|
||||
|
||||
proc userAborted() =
|
||||
raise newException(NimbleError, "User aborted the process.")
|
||||
|
|
@ -25,23 +26,43 @@ proc createHeaders(a: Auth): string =
|
|||
"Content-Type: application/x-www-form-urlencoded\c\L" &
|
||||
"Accept: */*\c\L")
|
||||
|
||||
proc requestNewToken(cfg: Config): string =
|
||||
display("Info:", "Please create a new personal access token on Github in" &
|
||||
" order to allow Nimble to fork the packages repository.",
|
||||
priority = HighPriority)
|
||||
display("Hint:", "Make sure to give the access token access to public repos" &
|
||||
" (public_repo scope)!", Warning, HighPriority)
|
||||
sleep(5000)
|
||||
display("Info:", "Your default browser should open with the following URL: " &
|
||||
"https://github.com/settings/tokens/new", priority = HighPriority)
|
||||
sleep(3000)
|
||||
openDefaultBrowser("https://github.com/settings/tokens/new")
|
||||
let token = promptCustom("Personal access token?", "").strip()
|
||||
# inform the user that their token will be written to disk
|
||||
let tokenWritePath = cfg.nimbleDir / ApiKeyFile
|
||||
display("Info:", "Writing access token to file:" & tokenWritePath,
|
||||
priority = HighPriority)
|
||||
writeFile(tokenWritePath, token)
|
||||
sleep(3000)
|
||||
return token
|
||||
|
||||
proc getGithubAuth(cfg: Config): Auth =
|
||||
|
||||
try:
|
||||
result.token = readFile(cfg.nimbleDir / ApiKeyFile)
|
||||
except IOError:
|
||||
display("Info:", "Please create a new personal access token on Github in" &
|
||||
" order to allow Nimble to fork the packages repository.",
|
||||
priority = HighPriority)
|
||||
display("Hint:", "Make sure to give the access token access to public repos" &
|
||||
" (public_repo scope)!", Warning, HighPriority)
|
||||
sleep(5000)
|
||||
display("Info:", "Your default browser should open with the following URL: " &
|
||||
"https://github.com/settings/tokens/new", priority = HighPriority)
|
||||
sleep(3000)
|
||||
openDefaultBrowser("https://github.com/settings/tokens/new")
|
||||
result.token = promptCustom("Personal access token?", "").strip()
|
||||
writeFile(cfg.nimbleDir / ApiKeyFile, result.token)
|
||||
# always prefer the environment variable to asking for a new one
|
||||
if existsEnv(ApiTokenEnvironmentVariable):
|
||||
result.token = getEnv(ApiTokenEnvironmentVariable)
|
||||
display("Info:", "Using the '" & ApiTokenEnvironmentVariable &
|
||||
"' environment variable for the GitHub API Token.",
|
||||
priority = HighPriority)
|
||||
else:
|
||||
# try to read from disk, if it cannot be found write a new one
|
||||
try:
|
||||
let apiTokenFilePath = cfg.nimbleDir / ApiKeyFile
|
||||
result.token = readFile(apiTokenFilePath)
|
||||
display("Info:", "Using GitHub API Token in file: " & apiTokenFilePath,
|
||||
priority = HighPriority)
|
||||
except IOError:
|
||||
result.token = requestNewToken(cfg)
|
||||
|
||||
let resp = getContent("https://api.github.com/user",
|
||||
extraHeaders=createHeaders(result)).parseJson()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue