From de144f29e663d5d1aaebbf608cf794dd2c3323c9 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Wed, 7 Aug 2024 18:19:57 -0400 Subject: [PATCH] feat: `wiki browse` --- src/wiki.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/wiki.rs b/src/wiki.rs index f5724df..4aff76c 100644 --- a/src/wiki.rs +++ b/src/wiki.rs @@ -27,6 +27,11 @@ pub enum WikiSubcommand { repo: Option, page: String, }, + Browse { + #[clap(long, short)] + repo: Option, + page: String, + }, } impl WikiCommand { @@ -40,6 +45,7 @@ impl WikiCommand { match self.command { Contents { repo: _ } => wiki_contents(&repo, &api).await?, View { repo: _, page } => view_wiki_page(&repo, &api, &*page).await?, + Browse { repo: _, page } => browse_wiki_page(&repo, &api, &*page).await?, } Ok(()) } @@ -47,14 +53,16 @@ impl WikiCommand { fn repo(&self) -> Option<&RepoArg> { use WikiSubcommand::*; match &self.command { - Contents { repo } | View { repo, .. } => repo.as_ref(), + Contents { repo } | View { repo, .. } | Browse { repo, .. } => repo.as_ref(), } } fn no_repo_error(&self) -> eyre::Error { use WikiSubcommand::*; match &self.command { - Contents { repo: _ } | View { .. } => eyre::eyre!("couldn't guess repo"), + Contents { repo: _ } | View { .. } | Browse { .. } => { + eyre::eyre!("couldn't guess repo") + } } } } @@ -104,3 +112,15 @@ async fn view_wiki_page(repo: &RepoName, api: &Forgejo, page: &str) -> eyre::Res println!("{}", crate::markdown(&contents)); Ok(()) } + +async fn browse_wiki_page(repo: &RepoName, api: &Forgejo, page: &str) -> eyre::Result<()> { + let page = api + .repo_get_wiki_page(repo.owner(), repo.name(), page) + .await?; + let html_url = page + .html_url + .as_ref() + .ok_or_eyre("page does not have html url")?; + open::that(html_url.as_str())?; + Ok(()) +}