mod cfg; mod net; mod pkgtoolkit; use crate::cfg::config::Config; use crate::net::{http_package::HTTPPackage, i2p_package::I2PPackage}; #[allow(unused_imports)] use crate::pkgtoolkit::pkgtools::Package; use clap::{Args, Parser, Subcommand}; use std::fs::File; use std::fs::create_dir_all; use std::io::Write; use std::path::Path; #[derive(Parser)] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { #[command(about = "Validate .mesk package archive")] Validate { path: String }, #[command(about = "Update all repositories index")] Update, #[command(about = "Upgrade all packages or a specific package")] Upgrade { pkgname: Option }, #[command(about = "Build package from .mesk ")] Build { pkgname: String }, #[command(about = "Install package from remote or local sources")] Install { pkgname: String, source: Option, #[command(flatten)] args: RemoteInstallArgs, }, #[command(about = "Uninstall package")] Uninstall { pkgname: String }, #[command(about = "Get package source")] GetSource { pkgname: String }, #[command(about = "Generator of config file")] DefaultConfig { repo: Option, cachedir: Option, buildir: Option, installed_db: Option, }, #[command(about = "Maintaners, links, developers and more info")] Credits, } #[derive(Args, Clone)] #[command(about = "Remote install arguments")] struct RemoteInstallArgs { #[arg(short = 'b', long = "bin")] #[arg(help = "Install binary package")] bin: bool, #[arg(short = 't', long = "http")] #[arg(help = "Use non-i2p mirror")] http: bool, #[arg(short = 'c', long = "clean")] #[arg(help = "Clean cache before install")] clean: bool, } #[tokio::main] async fn main() -> Result<(), Box> { // Changed return type to be more general let cli: Cli = Cli::parse(); match &cli.command { Commands::Validate { path } => { println!("Validating {}", path); return Ok(()); } Commands::Build { pkgname } => { println!("Building {}", pkgname); return Ok(()); } Commands::Install { pkgname, source: _, args, } => { let config = Config::parse().unwrap(); if args.http { println!("Installing {} via HTTP", pkgname); let mut http_client = HTTPPackage::new(config); match http_client.fetch_index_http().await { Ok(_) => { log::info!("Index fetched successfully."); } Err(e) => { log::error!("Failed to fetch index: {}", e); return Err(e); } } match http_client.fetch_package_http(pkgname).await { Ok(_) => { log::info!("Package '{}' installed successfully.", pkgname); } Err(e) => { log::error!("Failed to install package '{}': {}", pkgname, e); return Err(e); } } } else { println!("Installing {} via I2P", pkgname); let mut i2p_client = I2PPackage::new(config); match i2p_client.fetch_index().await { Ok(_) => { log::info!("Index fetched successfully."); } Err(e) => { log::error!("Failed to fetch index: {}", e); return Err(e); } } match i2p_client.fetch_package(pkgname).await { Ok(_) => { log::info!("Package '{}' installed successfully.", pkgname); } Err(e) => { log::error!("Failed to install package '{}': {}", pkgname, e); return Err(e); } } } return Ok(()); } Commands::Uninstall { pkgname } => { println!("Uninstalling {}", pkgname); return Ok(()); } Commands::GetSource { pkgname } => { println!("Getting source of {}", pkgname); return Ok(()); } Commands::DefaultConfig { repo, cachedir, buildir, installed_db, } => { println!("Generating config file"); if cachedir.is_none() && repo.is_none() && buildir.is_none() { let config = Config::default().unwrap(); println!("---- Start of generated config ----"); println!("{}", config); println!("---- End of generated config ----"); log::warn!("Writing the default config to /etc/mesk/mesk.toml"); let path = Path::new("/etc/mesk/mesk.toml"); create_dir_all(path.parent().unwrap())?; let mut file = File::create(path)?; file.write_all(config.as_bytes())?; println!("Config tool ending work."); } else { let config = Config::generate(repo, cachedir, buildir, installed_db).unwrap(); println!("---- Start of generated config ----"); println!("{:?}", config); println!("---- End of generated config ----"); log::warn!("Writing the default config to /etc/mesk/mesk.toml"); let path = Path::new("/etc/mesk/mesk.toml"); create_dir_all(path.parent().unwrap())?; let mut file = File::create(path)?; file.write_all(config.as_bytes())?; println!("Config tool ending work."); } return Ok(()); } Commands::Update => { let config = Config::parse().unwrap(); println!("Updating index from {}", config.repo.repo_url); let mut i2p_client = I2PPackage::new(config); match i2p_client.fetch_index().await { Ok(_) => { println!("Index updated successfully."); } Err(e) => { log::error!("Failed to update index: {}", e); return Err(e); } } return Ok(()); } Commands::Upgrade { pkgname } => { println!("Upgrading {}", pkgname.as_deref().unwrap_or("all packages")); return Ok(()); } Commands::Credits => { println!( "CREATED BY: Asya and Namilsk as part of the Anthrill independent Global network distribution project" ); println!(" "); println!("The Anthrill project repos: https://codeberg.org/NamelessTeam "); } } Ok(()) }