From ee6995fea8d86218aea85fba5fa35ebcb7ac64bd Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Wed, 11 Sep 2024 14:53:00 -0700 Subject: [PATCH] Improve cross-platform handling of github token --- js/packages/repo-quest/src/index.tsx | 2 +- rs/Cargo.lock | 19 +++++++++++++++++++ rs/crates/repo-quest/Cargo.toml | 1 + rs/crates/repo-quest/src/github.rs | 26 +++++++++++--------------- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/js/packages/repo-quest/src/index.tsx b/js/packages/repo-quest/src/index.tsx index ec46f3d..cf1194b 100644 --- a/js/packages/repo-quest/src/index.tsx +++ b/js/packages/repo-quest/src/index.tsx @@ -114,7 +114,7 @@ let GithubLoader = () => ( ) : ( -
ERROR: {token.value}
+ ) } diff --git a/rs/Cargo.lock b/rs/Cargo.lock index fbe6265..1409fc3 100644 --- a/rs/Cargo.lock +++ b/rs/Cargo.lock @@ -3071,6 +3071,7 @@ dependencies = [ "tokio-retry", "toml 0.8.19", "tracing", + "which", ] [[package]] @@ -4972,6 +4973,18 @@ dependencies = [ "windows-core 0.58.0", ] +[[package]] +name = "which" +version = "6.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f" +dependencies = [ + "either", + "home", + "rustix", + "winsafe", +] + [[package]] name = "winapi" version = "0.3.9" @@ -5360,6 +5373,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" + [[package]] name = "wry" version = "0.43.1" diff --git a/rs/crates/repo-quest/Cargo.toml b/rs/crates/repo-quest/Cargo.toml index da2c110..d809601 100644 --- a/rs/crates/repo-quest/Cargo.toml +++ b/rs/crates/repo-quest/Cargo.toml @@ -27,3 +27,4 @@ tauri-plugin-shell = "2.0.0-rc" specta = "=2.0.0-rc.20" specta-typescript = "0.0.7" tauri-specta = { version = "=2.0.0-rc.19", features = ["derive", "typescript"] } +which = "6.0.3" diff --git a/rs/crates/repo-quest/src/github.rs b/rs/crates/repo-quest/src/github.rs index fc1d855..188ef66 100644 --- a/rs/crates/repo-quest/src/github.rs +++ b/rs/crates/repo-quest/src/github.rs @@ -411,22 +411,18 @@ fn read_github_token_from_fs() -> GithubToken { } fn generate_github_token_from_cli() -> GithubToken { - let shell = env::var("SHELL").unwrap_or_else(|_| "sh".into()); - let which_status = Command::new(&shell).args(["-c", "which gh"]).status(); - match which_status { - Ok(status) => { - if status.success() { - let token_output = token_try!(Command::new(shell) - .args(["-c", "gh auth token"]) - .output() - .context("Failed to run `gh auth token`")); - let token = token_try!(String::from_utf8(token_output.stdout)); - let token_clean = token.trim_end().to_string(); - GithubToken::Found(token_clean) - } else { - GithubToken::NotFound - } + let gh_path_res = which::which("gh"); + match gh_path_res { + Ok(gh_path) => { + let token_output = token_try!(Command::new(gh_path) + .args(["auth", "token"]) + .output() + .context("Failed to run `gh auth token`")); + let token = token_try!(String::from_utf8(token_output.stdout)); + let token_clean = token.trim_end().to_string(); + GithubToken::Found(token_clean) } + Err(which::Error::CannotFindBinaryPath) => GithubToken::NotFound, Err(err) => GithubToken::Error(format!("{err:?}")), } }