diff options
author | Cyborus <cyborus@cyborus.xyz> | 2023-12-13 01:49:13 +0100 |
---|---|---|
committer | Cyborus <cyborus@cyborus.xyz> | 2023-12-13 02:06:20 +0100 |
commit | 68d255db7a04c4b62e1c638e625a985432f2a7f9 (patch) | |
tree | e72810c41b7ca3b359f654692e7216a9a3524477 | |
parent | Merge pull request 'add ci' (#8) from ci into main (diff) | |
download | forgejo-cli-68d255db7a04c4b62e1c638e625a985432f2a7f9.tar.xz forgejo-cli-68d255db7a04c4b62e1c638e625a985432f2a7f9.zip |
add git remote selection flag
-rw-r--r-- | src/issues.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 8 | ||||
-rw-r--r-- | src/repo.rs | 30 |
3 files changed, 25 insertions, 17 deletions
diff --git a/src/issues.rs b/src/issues.rs index 56ee300..18f6bab 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -57,9 +57,9 @@ pub enum ViewCommand { } impl IssueCommand { - pub async fn run(self, keys: &crate::KeyInfo) -> eyre::Result<()> { + pub async fn run(self, keys: &crate::KeyInfo, remote_name: Option<&str>) -> eyre::Result<()> { use IssueCommand::*; - let repo = RepoInfo::get_current()?; + let repo = RepoInfo::get_current(remote_name)?; let api = keys.get_api(&repo.host_url())?; match self { Create { title, body } => create_issue(&repo, &api, title, body).await?, diff --git a/src/main.rs b/src/main.rs index c7674d0..1445706 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ mod repo; #[derive(Parser, Debug)] pub struct App { + #[clap(long, short='R')] + remote: Option<String>, #[clap(subcommand)] command: Command, } @@ -36,13 +38,13 @@ async fn main() -> eyre::Result<()> { let mut keys = KeyInfo::load().await?; match args.command { - Command::Repo(subcommand) => subcommand.run(&keys).await?, - Command::Issue(subcommand) => subcommand.run(&keys).await?, + Command::Repo(subcommand) => subcommand.run(&keys, args.remote.as_deref()).await?, + Command::Issue(subcommand) => subcommand.run(&keys, args.remote.as_deref()).await?, Command::User { host } => { let host = host.map(|host| Url::parse(&host)).transpose()?; let url = match host { Some(url) => url, - None => repo::RepoInfo::get_current()?.url().clone(), + None => repo::RepoInfo::get_current(args.remote.as_deref())?.url().clone(), }; let name = keys.get_login(&url)?.username(); eprintln!("currently signed in to {name}@{url}"); diff --git a/src/repo.rs b/src/repo.rs index 7fdecd7..80249b5 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -10,9 +10,9 @@ pub struct RepoInfo { } impl RepoInfo { - pub fn get_current() -> eyre::Result<Self> { + pub fn get_current(name: Option<&str>) -> eyre::Result<Self> { let repo = git2::Repository::open(".")?; - let url = get_remote(&repo)?; + let url = get_remote(&repo, name)?; let mut path = url.path_segments().ok_or_else(|| eyre!("bad path"))?; let owner = path @@ -49,13 +49,19 @@ impl RepoInfo { } } -fn get_remote(repo: &git2::Repository) -> eyre::Result<Url> { - let head = repo.head()?; - let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?; - let remote_name = repo.branch_upstream_remote(branch_name)?; - let remote_name = remote_name - .as_str() - .ok_or_else(|| eyre!("remote name not UTF-8"))?; +fn get_remote(repo: &git2::Repository, name: Option<&str>) -> eyre::Result<Url> { + let remote_name; + let remote_name = match name { + Some(name) => name, + None => { + let head = repo.head()?; + let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?; + remote_name = repo.branch_upstream_remote(branch_name)?; + remote_name + .as_str() + .ok_or_else(|| eyre!("remote name not UTF-8"))? + } + }; let remote = repo.find_remote(remote_name)?; let url = Url::parse(std::str::from_utf8(remote.url_bytes())?)?; Ok(url) @@ -85,7 +91,7 @@ pub enum RepoCommand { } impl RepoCommand { - pub async fn run(self, keys: &crate::KeyInfo) -> eyre::Result<()> { + pub async fn run(self, keys: &crate::KeyInfo, remote_name: Option<&str>) -> eyre::Result<()> { match self { RepoCommand::Create { host, @@ -140,7 +146,7 @@ impl RepoCommand { } } RepoCommand::Info => { - let repo = RepoInfo::get_current()?; + let repo = RepoInfo::get_current(remote_name)?; let api = keys.get_api(&repo.host_url())?; let repo = api.get_repo(repo.owner(), repo.name()).await?; match repo { @@ -151,7 +157,7 @@ impl RepoCommand { } } RepoCommand::Browse => { - let repo = RepoInfo::get_current()?; + let repo = RepoInfo::get_current(remote_name)?; let mut url = repo.host_url().clone(); let new_path = format!( "{}/{}/{}", |