From e022abbdcb21f3b093727b68bd832807d7bfc720 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 7 Aug 2025 16:30:46 +0100 Subject: =?UTF-8?q?=E2=9C=A8feat:=20add=20the=20github=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/error.rs | 10 +++++++++ src/github.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/manager.rs | 11 ++++++++-- 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/github.rs (limited to 'src') diff --git a/src/error.rs b/src/error.rs index f946f58..85f85f6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,6 +11,7 @@ pub enum Error { source: reqwest::Error, }, InvalidAppImage, + InvalidSlug(String), #[from] Io(std::io::Error), @@ -26,6 +27,12 @@ pub enum Error { #[from] IndicatifTemplate(indicatif::style::TemplateError), + + #[from] + Octocrab(octocrab::Error), + + #[from] + Dialoguer(dialoguer::Error), } impl core::fmt::Display for Error { @@ -57,6 +64,9 @@ impl core::fmt::Display for Error { Error::InvalidAppImage => { write!(fmt, "Invalid AppImage") } + Error::InvalidSlug(slug) => write!(fmt, "Invalid repository slug {slug}"), + Error::Octocrab(e) => write!(fmt, "Octocrab error: {e}"), + Error::Dialoguer(e) => write!(fmt, "Dialoguer error: {e}"), } } } diff --git a/src/github.rs b/src/github.rs new file mode 100644 index 0000000..50812d1 --- /dev/null +++ b/src/github.rs @@ -0,0 +1,69 @@ +use dialoguer::FuzzySelect; +use octocrab::models::repos::Asset; + +use crate::{AppImage, Error, Result}; + +pub async fn get_github_release_url(appimage: &AppImage) -> Result { + let octocrab = octocrab::instance(); + + let (owner, repo) = appimage + .source + .meta + .url + .split_once('/') + .ok_or_else(|| Error::InvalidSlug(appimage.source.meta.url.to_string()))?; + + let page = octocrab + .repos(owner, repo) + .releases() + .list() + .per_page(100) + .send() + .await?; + + let mut tags: Vec = vec![]; + + for releases in &page { + for asset in &releases.assets { + if asset.name.to_lowercase().ends_with(".appimage") { + tags.push(releases.tag_name.to_string()); + break; + } + } + } + + let tag_selection = FuzzySelect::new() + .with_prompt("Choose a release") + .items(&tags) + .interact()?; + + let mut assets: Vec = vec![]; + + for releases in page { + if releases.tag_name == tags[tag_selection] { + for asset in releases.assets { + if asset.name.to_lowercase().ends_with(".appimage") { + assets.push(asset); + } + } + } + } + + let mut asset_selection: usize = 0; + + if assets.len() > 1 { + asset_selection = FuzzySelect::new() + .with_prompt("Choose an asset") + .items( + &assets + .iter() + .map(|x| x.name.to_string()) + .collect::>(), + ) + .interact()?; + } + + let url = assets[asset_selection].browser_download_url.to_string(); + + Ok(url) +} diff --git a/src/lib.rs b/src/lib.rs index 3e20bde..987f485 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ mod paths; mod symlink; mod tui; mod error; +mod github; pub use crate::appimage::*; pub use crate::args::*; @@ -17,3 +18,4 @@ pub use crate::paths::*; pub use crate::symlink::*; pub use crate::tui::*; pub use crate::error::*; +pub use crate::github::*; diff --git a/src/manager.rs b/src/manager.rs index e0fb7ac..15f28d9 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -1,6 +1,8 @@ use tokio::fs; -use crate::{AppImage, Downloader, Index, Result, SymlinkManager, index_dir}; +use crate::{ + AppImage, Downloader, Index, Result, SymlinkManager, get_github_release_url, index_dir, +}; #[derive(Debug, Default)] pub struct PackageManager { @@ -32,7 +34,12 @@ impl PackageManager { .download_with_progress(&appimage.source.meta.url, &appimage.file_path) .await?; } else { - todo!() + self.downloader + .download_with_progress( + &get_github_release_url(appimage).await?, + &appimage.file_path, + ) + .await?; } self.index.add(appimage, appname).await?; -- cgit v1.2.3