feat: wiki view

This commit is contained in:
Cyborus 2024-08-05 15:16:37 -04:00
parent 296ac0c853
commit a756d1da57
No known key found for this signature in database

View file

@ -1,5 +1,6 @@
use base64ct::Encoding;
use clap::{Args, Subcommand}; use clap::{Args, Subcommand};
use eyre::OptionExt; use eyre::{Context, OptionExt};
use forgejo_api::Forgejo; use forgejo_api::Forgejo;
use crate::{ use crate::{
@ -18,7 +19,14 @@ pub struct WikiCommand {
#[derive(Subcommand, Clone, Debug)] #[derive(Subcommand, Clone, Debug)]
pub enum WikiSubcommand { pub enum WikiSubcommand {
Contents { repo: Option<RepoArg> }, Contents {
repo: Option<RepoArg>,
},
View {
#[clap(long, short)]
repo: Option<RepoArg>,
page: String,
},
} }
impl WikiCommand { impl WikiCommand {
@ -31,6 +39,7 @@ impl WikiCommand {
match self.command { match self.command {
Contents { repo: _ } => wiki_contents(&repo, &api).await?, Contents { repo: _ } => wiki_contents(&repo, &api).await?,
View { repo: _, page } => view_wiki_page(&repo, &api, &*page).await?,
} }
Ok(()) Ok(())
} }
@ -38,14 +47,14 @@ impl WikiCommand {
fn repo(&self) -> Option<&RepoArg> { fn repo(&self) -> Option<&RepoArg> {
use WikiSubcommand::*; use WikiSubcommand::*;
match &self.command { match &self.command {
Contents { repo } => repo.as_ref(), Contents { repo } | View { repo, .. } => repo.as_ref(),
} }
} }
fn no_repo_error(&self) -> eyre::Error { fn no_repo_error(&self) -> eyre::Error {
use WikiSubcommand::*; use WikiSubcommand::*;
match &self.command { match &self.command {
Contents { repo: _ } => eyre::eyre!("couldn't guess repo"), Contents { repo: _ } | View { .. } => eyre::eyre!("couldn't guess repo"),
} }
} }
} }
@ -70,3 +79,28 @@ async fn wiki_contents(repo: &RepoName, api: &Forgejo) -> eyre::Result<()> {
Ok(()) Ok(())
} }
async fn view_wiki_page(repo: &RepoName, api: &Forgejo, page: &str) -> eyre::Result<()> {
let SpecialRender { bold, reset, .. } = *crate::special_render();
let page = api
.repo_get_wiki_page(repo.owner(), repo.name(), page)
.await?;
let title = page
.title
.as_deref()
.ok_or_eyre("page does not have title")?;
println!("{bold}{title}{reset}");
println!();
let contents_b64 = page
.content_base64
.as_deref()
.ok_or_eyre("page does not have content")?;
let contents = String::from_utf8(base64ct::Base64::decode_vec(contents_b64)?)
.wrap_err("page content is not utf-8")?;
println!("{}", crate::markdown(&contents));
Ok(())
}