summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCyborus <cyborus@noreply.codeberg.org>2023-12-13 02:17:38 +0100
committerCyborus <cyborus@noreply.codeberg.org>2023-12-13 02:17:38 +0100
commitc4810e3e934a54deac93b1c9abb78453aa8ffd91 (patch)
treef04f1da152391704abeb7dafff6ee96936b00690
parentMerge pull request 'add issue search' (#9) from search-issues into main (diff)
parentformat (diff)
downloadforgejo-cli-c4810e3e934a54deac93b1c9abb78453aa8ffd91.tar.xz
forgejo-cli-c4810e3e934a54deac93b1c9abb78453aa8ffd91.zip
Merge pull request 'add git remote selection flag' (#10) from select-remote into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/10
-rw-r--r--src/issues.rs4
-rw-r--r--src/main.rs10
-rw-r--r--src/repo.rs30
3 files changed, 27 insertions, 17 deletions
diff --git a/src/issues.rs b/src/issues.rs
index 13f9590..cb33549 100644
--- a/src/issues.rs
+++ b/src/issues.rs
@@ -83,9 +83,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..ad62b50 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,15 @@ 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!(
"{}/{}/{}",