From 068eea55e0612184151461e64633b3dc18e53490 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Mon, 1 Dec 2025 14:06:10 +0300 Subject: Implemented test suite, fixed some issues and added more modular structure into .woodpecker.yaml --- src/cfg/config.rs | 10 +++++----- src/lib.rs | 14 ++++++++++++++ src/main.rs | 2 +- src/net/http_package.rs | 43 ++++++++++++++++++++++++++++++------------- src/net/i2p_package.rs | 4 ++-- src/pkgtoolkit/pkgtools.rs | 2 +- 6 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 src/lib.rs (limited to 'src') diff --git a/src/cfg/config.rs b/src/cfg/config.rs index de9901f..213224f 100644 --- a/src/cfg/config.rs +++ b/src/cfg/config.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use std::fs; use toml; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Deserialize, Debug, Serialize, Clone)] #[serde(rename_all = "lowercase")] pub enum Loglevel { Trace, @@ -13,14 +13,14 @@ pub enum Loglevel { } /// `mesk.toml` configuration fields here -#[derive(Deserialize, Debug, Serialize)] +#[derive(Deserialize, Debug, Serialize, Clone)] pub struct Config { pub repo: Repo, pub log: Log, pub paths: Paths, } -#[derive(Deserialize, Debug, Serialize)] +#[derive(Deserialize, Debug, Serialize, Clone)] pub struct Log { #[serde(rename = "log_file")] pub log_file: String, @@ -29,7 +29,7 @@ pub struct Log { } // Rename needed for editing mesk.toml file fields but dont touch code. -#[derive(Deserialize, Debug, Serialize)] +#[derive(Deserialize, Debug, Serialize, Clone)] pub struct Repo { #[serde(rename = "repo_url")] pub repo_url: String, @@ -43,7 +43,7 @@ pub struct Repo { // pub arch = arch; } -#[derive(Deserialize, Debug, Serialize)] +#[derive(Deserialize, Debug, Serialize, Clone)] pub struct Paths { #[serde(rename = "cache_dir")] pub cache_dir: String, diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ed6b426 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,14 @@ + +pub mod cfg { + pub mod config; +} + +pub mod net { + pub mod http_package; + pub mod i2p_package; +} + +pub mod pkgtoolkit { + pub mod pkgtools; +} + diff --git a/src/main.rs b/src/main.rs index 2d36f20..4403c51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod cfg; -mod net; // This should contain both i2p_package and http_package modules +mod net; mod pkgtoolkit; use crate::cfg::config::Config; diff --git a/src/net/http_package.rs b/src/net/http_package.rs index ec7c318..ade4ee1 100644 --- a/src/net/http_package.rs +++ b/src/net/http_package.rs @@ -4,14 +4,15 @@ use futures_util::stream::TryStreamExt; use indicatif::{ProgressBar, ProgressStyle}; use reqwest; use serde::Deserialize; -use std::collections::HashMap; -use std::path::Path; -use tokio::fs::File; -use tokio::io::AsyncWriteExt; +use std::{ collections::HashMap, path::Path }; +use tokio::{ fs::File, io::AsyncWriteExt}; +use std::fs::File as StdFile; +use flate2::read::GzDecoder; +use tar::Archive; pub struct HTTPPackage { - config: Config, - index_packages: Option>, + pub config: Config, + pub index_packages: Option>, } #[derive(Deserialize, Debug)] @@ -42,6 +43,8 @@ impl HTTPPackage { let repo_url_str = &self.config.repo.repo_url; let cache_dir = &self.config.paths.cache_dir; + log::debug!("Cache directory: {:?}", cache_dir); + let index_url = if repo_url_str.ends_with(".tar.gz") { repo_url_str.clone() } else { @@ -86,6 +89,10 @@ impl HTTPPackage { let mut stream = response.bytes_stream(); let file_path = Path::new(cache_dir).join("INDEX.tar.gz"); + // Ensure cache_dir exists + tokio::fs::create_dir_all(cache_dir).await + .map_err(|e| format!("Failed to create cache dir: {}", e))?; + let mut file = File::create(&file_path).await?; let mut downloaded: u64 = 0; @@ -98,9 +105,16 @@ impl HTTPPackage { pb.finish_with_message("INDEX.tar.gz download finished"); - // --- Извлечение и парсинг INDEX.toml --- log::info!("Extracting INDEX.tar.gz to cache directory..."); - Package::extract_archive(&file_path.to_string_lossy())?; // Используем существующую функцию из pkgtoolkit + // Package::extract_archive(&file_path.to_string_lossy())?; + + + let archive_file = StdFile::open(&file_path) + .map_err(|e| format!("Failed to open archive: {}", e))?; + let gz_decoder = GzDecoder::new(archive_file); + let mut archive = Archive::new(gz_decoder); + archive.unpack(cache_dir) + .map_err(|e| format!("Failed to unpack archive: {}", e))?; let index_toml_path = Path::new(cache_dir).join("INDEX.toml"); if !index_toml_path.exists() { @@ -110,12 +124,13 @@ impl HTTPPackage { } let index_content = tokio::fs::read_to_string(&index_toml_path).await?; - let index_data: IndexData = toml::from_str(&index_content)?; + log::debug!("Content of INDEX.toml:\n{}", index_content); + + let index_data: IndexData = toml::from_str(&index_content) + .map_err(|e| format!("Failed to parse INDEX.toml: {}", e))?; let mut package_map = HashMap::new(); for pkg in index_data.packages { - // PKG_URL = /repo/package.mesk - // FULL URL = "http://mesk.anthrill.i2p/i2p/repo/pkg.mesk" let base_url = url::Url::parse(&self.config.repo.repo_url)?; let full_url = base_url.join(&pkg.url)?; let mut pkg_clone = pkg.clone(); @@ -131,7 +146,7 @@ impl HTTPPackage { ); Ok(true) - } +} /// An internal auxiliary function for downloading data and writing it to a file with a progress display. /// @@ -212,7 +227,7 @@ impl HTTPPackage { ) -> Result> { let package_info = self.fetch_package_info(package_name)?; let url = &package_info.url; - + let client = reqwest::Client::new(); let file_name = Path::new(url) @@ -223,6 +238,8 @@ impl HTTPPackage { let cache_dir = &self.config.paths.cache_dir; let file_path = Path::new(cache_dir).join(file_name); + tokio::fs::create_dir_all(&cache_dir).await?; + Self::download_file_with_progress(&client, url, &file_path, file_name).await?; log::info!( diff --git a/src/net/i2p_package.rs b/src/net/i2p_package.rs index 751d561..2b847d6 100644 --- a/src/net/i2p_package.rs +++ b/src/net/i2p_package.rs @@ -19,8 +19,8 @@ use yosemite::Session; use yosemite::SessionOptions; pub struct I2PPackage { - config: Config, - index_packages: Option>, + pub config: Config, + pub index_packages: Option>, } #[derive(Deserialize, Debug)] diff --git a/src/pkgtoolkit/pkgtools.rs b/src/pkgtoolkit/pkgtools.rs index 9fe01c6..29145d3 100644 --- a/src/pkgtoolkit/pkgtools.rs +++ b/src/pkgtoolkit/pkgtools.rs @@ -77,7 +77,7 @@ struct Build { } impl Archs { - fn as_str(&self) -> &'static str { + pub fn as_str(&self) -> &'static str { match self { Archs::X86_64 => "x86_64", Archs::Aarch64 => "aarch64", -- cgit v1.2.3