From f3d98b21d0b66952501aed5403df6773716f9e0b Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Sat, 29 Nov 2025 16:08:17 +0300 Subject: Connected some functions to main.rs. --- src/cfg/config.rs | 30 +++++++++++++++-- src/i2impl/mi2p.rs | 3 +- src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++++++---- src/pkgtoolkit/pkgtools.rs | 10 ------ 4 files changed, 102 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/cfg/config.rs b/src/cfg/config.rs index 7c81e21..ffccbad 100644 --- a/src/cfg/config.rs +++ b/src/cfg/config.rs @@ -1,5 +1,4 @@ use std::fs; -use std::env; use serde::{Deserialize, Serialize}; use toml; @@ -57,7 +56,7 @@ impl Config { /// /// This function reads the /etc/mesk.toml file, parses it and returns the Config object. pub fn parse() -> Result { - let contents = fs::read_to_string("/etc/mesk.toml").unwrap(); + let contents = fs::read_to_string("/etc/mesk/mesk.toml").unwrap(); let result: Config = toml::from_str(&contents)?; Ok(result) } @@ -74,6 +73,7 @@ impl Config { auto_update: true, destination: (String::from("mesk"), String::from("mesk")), // Its a hurt place, you need to generate destinations by i2pd and paste here (to mesk.toml) + // Better to leave it empty or set it to (mesk, mesk), now destination conn not implemented }, log: Log { log_file: String::from("/var/log/mesk.log"), @@ -88,4 +88,28 @@ impl Config { let toml_str = toml::to_string(&default)?; Ok(toml_str) } -} \ No newline at end of file + + pub fn generate(repo: &Option, cachedir: &Option, buildir: &Option) -> Result { + let generator: Config = Config { + repo: Repo { + repo_url: if repo.is_none() { format!("https://mesk.anthrill.i2p/repo/{}/", std::env::consts::ARCH ) } else { repo.clone().unwrap() }, + auto_update: true, + destination: (String::from("mesk"), String::from("mesk")), + }, + log: Log { + log_file: String::from("/var/log/mesk.log"), + log_level: Loglevel::Info, + }, + paths: Paths { + cache_dir: if cachedir.is_none() { String::from("/var/cache/mesk") } else { cachedir.clone().unwrap() }, + build_dir: if buildir.is_none() { String::from("/var/cache/mesk/build") } else { buildir.clone().unwrap() }, + + /* + FIXME: I can leave this parameter, but I think it would be better to make the build + path in the /var/cache/mesk/$pkgname-$pkgver/BUILD/ + */ + }, + }; + Ok(toml::to_string(&generator)?) + } +} diff --git a/src/i2impl/mi2p.rs b/src/i2impl/mi2p.rs index 9faef34..cf2b8e5 100644 --- a/src/i2impl/mi2p.rs +++ b/src/i2impl/mi2p.rs @@ -1,7 +1,6 @@ use crate::cfg::config::Config; -use serde::de::value::StrDeserializer; use tokio; use emissary_core::runtime::{ AsyncRead, @@ -45,7 +44,7 @@ impl I2PStatus { } */ -struct I2P { +pub struct I2P { session: Option>, connected: bool, config: Config, diff --git a/src/main.rs b/src/main.rs index 1630c66..a3f69dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,19 @@ mod cfg; mod i2impl; mod pkgtoolkit; -#[allow(unused_imports)] use crate::cfg::config::Config; #[allow(unused_imports)] use crate::pkgtoolkit::pkgtools::Package; #[allow(unused_imports)] -use crate::i2impl::mi2p; +use crate::i2impl::mi2p::I2P; -use clap::{Parser, Subcommand, Args}; +use clap::{Args, Command, Parser, Subcommand}; +use yosemite::Stream; +use std::io::Write; +use std::path::Path; +use std::fs::create_dir_all; +use std::fs::File; +use tokio; #[derive(Parser)] struct Cli { @@ -23,6 +28,12 @@ enum Commands { Validate { pkgname: 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, @@ -30,6 +41,7 @@ enum Commands { #[command(about = "Install package from remote or local sources")] Install{ pkgname: String, + source: Option, }, #[command(about = "Uninstall package")] Uninstall{ @@ -38,6 +50,12 @@ enum Commands { #[command(about = "Get package source")] GetSource{ pkgname: String + }, + #[command(about = "Generator of config file")] + DefaultConfig { + repo: Option, + cachedir: Option, + buildir: Option, } } @@ -52,7 +70,8 @@ struct RemoteInstallArgs { i2p: bool, } -fn main() -> Result<(), std::io::Error> { +#[tokio::main] +async fn main() -> Result<(), std::io::Error> { let cli: Cli = Cli::parse(); // Plug in these functions only until the backend is ready for testing (Aplha versions) @@ -66,7 +85,7 @@ fn main() -> Result<(), std::io::Error> { println!("Building {}", pkgname); return Ok(()) }, - Commands::Install { pkgname } => { + Commands::Install { pkgname, source} => { println!("Installing {}", pkgname); return Ok(()) }, @@ -77,8 +96,56 @@ fn main() -> Result<(), std::io::Error> { Commands::GetSource { pkgname } => { println!("Getting source of {}", pkgname); return Ok(()) - - } + }, + Commands::DefaultConfig { repo, cachedir, buildir } => { + 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()).unwrap(); + let mut file = File::create(path).unwrap(); + file.write(config.as_bytes()).unwrap(); + println!("Config tool ending work."); + + } else { + let config = Config::generate(repo, cachedir, buildir).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()).unwrap(); + let mut file = File::create(path).unwrap(); + file.write_all(config.as_bytes()).unwrap(); + println!("Config tool ending work."); + + } + return Ok(()) + }, + Commands::Update => { + let config = Config::parse().unwrap(); + println!("Updating index from {}", config.repo.repo_url); + let mut i2pd = I2P::new(config); + let _index= I2P::fetch_index(&mut i2pd).await?; + println!("Index updated"); + return Ok(()) + }, + Commands::Upgrade { pkgname } => { + println!("Upgrading all packages"); + return Ok(()) + }, + + } Ok(()) } \ No newline at end of file diff --git a/src/pkgtoolkit/pkgtools.rs b/src/pkgtoolkit/pkgtools.rs index f44a283..5acbd11 100644 --- a/src/pkgtoolkit/pkgtools.rs +++ b/src/pkgtoolkit/pkgtools.rs @@ -250,16 +250,6 @@ impl Package { /// * Returns an error if INSTALL file is empty. /// // TODO: Add meta-files validation here. - /// Checks if the archive contains INSTALL, SETTS and BUILD files. - /// - /// Checks if INSTALL file exists and is not empty. If it does not exist or is empty, returns an error. - /// - /// Checks if SETTS and BUILD files exist and are not empty. If they do not exist or are empty, logs a warning. - /// # Errors - /// * Returns an error if INSTALL file does not exist or is empty. - /// * Returns an error if INSTALL file is empty. - /// - // TODO: Add meta-files validation here. pub fn check(path_to_archive: String) -> Result { // Call the new extraction function Self::extract_archive(&path_to_archive)?; -- cgit v1.2.3