Skip to content

Commit

Permalink
Improve cross-platform handling of github token
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Sep 11, 2024
1 parent 500a974 commit ee6995f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion js/packages/repo-quest/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ let GithubLoader = () => (
</div>
</>
) : (
<pre>ERROR: {token.value}</pre>
<ErrorView action="Loading Github token" message={token.value} />
)
}
</Await>
Expand Down
19 changes: 19 additions & 0 deletions rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rs/crates/repo-quest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
26 changes: 11 additions & 15 deletions rs/crates/repo-quest/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}")),
}
}
Expand Down

0 comments on commit ee6995f

Please sign in to comment.