From 32ed21695b9c01cef92f0f7f6d29f9cca30b30f9 Mon Sep 17 00:00:00 2001 From: Samantha Marshall Date: Sat, 11 Mar 2017 14:51:54 -0500 Subject: [PATCH 1/5] adding support for an environment variable --- src/nimblepkg/publish.nim | 44 ++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/nimblepkg/publish.nim b/src/nimblepkg/publish.nim index 5b123a3..a024490 100644 --- a/src/nimblepkg/publish.nim +++ b/src/nimblepkg/publish.nim @@ -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,36 @@ 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 token_write_path = cfg.nimbleDir / ApiKeyFile + display("Info:", "Writing access token to file:" & token_write_path, + priority = HighPriority) + writeFile(token_write_path, token) + 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) + else: + # try to read from disk, if it cannot be found write a new one + try: + result.token = readFile(cfg.nimbleDir / ApiKeyFile) + except IOError: + result.token = requestNewToken(cfg) let resp = getContent("https://api.github.com/user", extraHeaders=createHeaders(result)).parseJson() From 26344e20830f6d9029844ecae310b9bdecc1f99b Mon Sep 17 00:00:00 2001 From: Samantha Marshall Date: Sat, 11 Mar 2017 14:54:48 -0500 Subject: [PATCH 2/5] adding on-screen delay for new message --- src/nimblepkg/publish.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nimblepkg/publish.nim b/src/nimblepkg/publish.nim index a024490..562a388 100644 --- a/src/nimblepkg/publish.nim +++ b/src/nimblepkg/publish.nim @@ -43,6 +43,7 @@ proc requestNewToken(cfg: Config): string = display("Info:", "Writing access token to file:" & token_write_path, priority = HighPriority) writeFile(token_write_path, token) + sleep(3000) return token proc getGithubAuth(cfg: Config): Auth = From 47f5691c0b15c101ad21aa4a21950d555b50132b Mon Sep 17 00:00:00 2001 From: Samantha Marshall Date: Mon, 13 Mar 2017 13:18:27 -0400 Subject: [PATCH 3/5] adding new display message about where the token is coming from --- src/nimblepkg/publish.nim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/nimblepkg/publish.nim b/src/nimblepkg/publish.nim index 562a388..9b24518 100644 --- a/src/nimblepkg/publish.nim +++ b/src/nimblepkg/publish.nim @@ -51,6 +51,9 @@ proc getGithubAuth(cfg: Config): Auth = # always prefer the environment variable to asking for a new one if existsEnv(ApiTokenEnvironmentVariable): result.token = getEnv(ApiTokenEnvironmentVariable) + display("Info:", "Using the '" & ApiTokenEnvironmentVariable & + "' environment varaible for the GitHub API Token.", + priority = HighPriority) else: # try to read from disk, if it cannot be found write a new one try: From ad869f9df65cb8972a555b954fc9dd4561667994 Mon Sep 17 00:00:00 2001 From: Samantha Marshall Date: Mon, 13 Mar 2017 13:38:32 -0400 Subject: [PATCH 4/5] fixing typo and adding message for retrieving API Token from file --- src/nimblepkg/publish.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/nimblepkg/publish.nim b/src/nimblepkg/publish.nim index 9b24518..6dd2378 100644 --- a/src/nimblepkg/publish.nim +++ b/src/nimblepkg/publish.nim @@ -52,12 +52,15 @@ proc getGithubAuth(cfg: Config): Auth = if existsEnv(ApiTokenEnvironmentVariable): result.token = getEnv(ApiTokenEnvironmentVariable) display("Info:", "Using the '" & ApiTokenEnvironmentVariable & - "' environment varaible for the GitHub API Token.", + "' 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: - result.token = readFile(cfg.nimbleDir / ApiKeyFile) + let api_token_file_path = cfg.nimbleDir / ApiKeyFile + result.token = readFile(api_token_file_path) + display("Info:", "Using GitHub API Token in file: " & api_token_file_path, + priority = HighPriority) except IOError: result.token = requestNewToken(cfg) From 0d516b483ee27497202faca04e1ec4a56d458108 Mon Sep 17 00:00:00 2001 From: Samantha Marshall Date: Mon, 13 Mar 2017 14:47:16 -0400 Subject: [PATCH 5/5] fixing casing --- src/nimblepkg/publish.nim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/nimblepkg/publish.nim b/src/nimblepkg/publish.nim index 6dd2378..2a4ce93 100644 --- a/src/nimblepkg/publish.nim +++ b/src/nimblepkg/publish.nim @@ -39,10 +39,10 @@ proc requestNewToken(cfg: Config): string = 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 token_write_path = cfg.nimbleDir / ApiKeyFile - display("Info:", "Writing access token to file:" & token_write_path, + let tokenWritePath = cfg.nimbleDir / ApiKeyFile + display("Info:", "Writing access token to file:" & tokenWritePath, priority = HighPriority) - writeFile(token_write_path, token) + writeFile(tokenWritePath, token) sleep(3000) return token @@ -57,9 +57,9 @@ proc getGithubAuth(cfg: Config): Auth = else: # try to read from disk, if it cannot be found write a new one try: - let api_token_file_path = cfg.nimbleDir / ApiKeyFile - result.token = readFile(api_token_file_path) - display("Info:", "Using GitHub API Token in file: " & api_token_file_path, + 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)