add git remote selection flag

This commit is contained in:
Cyborus 2023-12-12 19:49:13 -05:00
parent 98e3cceff8
commit 68d255db7a
No known key found for this signature in database
3 changed files with 25 additions and 17 deletions

View file

@ -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?,

View file

@ -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}");

View file

@ -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!(
"{}/{}/{}",