diff --git a/README.md b/README.md index 8305ed3..bd3042b 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,7 @@ For more help and document, see our documentation: - [Downloads Block](https://scibun.github.io/ScimonDocs/download-block.html) - [Readme Block](https://scibun.github.io/ScimonDocs/readme-block.html) - [Commands Block](https://scibun.github.io/ScimonDocs/commands-block.html) (Experimental) -- [Compress files](https://scibun.github.io/ScimonDocs/compress.html) - [Open links](https://scibun.github.io/ScimonDocs/open-links.html) -- [Checksum and Checksum Validate](https://scibun.github.io/ScimonDocs/checsum.html) - [Directives and Comments](https://scibun.github.io/ScimonDocs/directives.html) - [Markdown render](https://scibun.github.io/ScimonDocs/markdown-render.html) - [Scrape](https://scibun.github.io/ScimonDocs/scrape.html) @@ -40,12 +38,6 @@ For more help and document, see our documentation: path = "downloads/" open = "https://github.com/kremilly" -readme = "https://gist.githubusercontent.com/Kremilly/5fd360d994bb0fe108b648d0e4c9e92f/raw/ac524eba2112bf0bdbac1ad27e24f78f678589ec/readme-example.md" -checksum = "https://gist.githubusercontent.com/kremilly/499d6d51d096c1813cea0eade8eb0bc4/raw/d7c5965aeaf005cf0b612e3468ab47c30480083b/scibun.sha256" -checksum.unmatch = "keep" - -compress = "downloads.zip" - downloads { https://arxiv.org/pdf/2405.01513 https://olacesar.com/e-books/protegido.pdf diff --git a/src/cmd/checksum.rs b/src/cmd/checksum.rs deleted file mode 100644 index 0ef9164..0000000 --- a/src/cmd/checksum.rs +++ /dev/null @@ -1,92 +0,0 @@ -use std::{ - io::Write, - error::Error, - - fs::{ - self, - File, - }, -}; - -use crate::{ - args_cli::Flags, - monset::vars::Vars, - utils::file::FileUtils, - system::hashes::Hashes, - - ui::{ - ui_base::UI, - checksum_alerts::ChecksumAlerts, - }, -}; - -pub struct Checksum; - -impl Checksum { - - fn checksum_unmatch_delete_file(contents: &str, path: &str, line: &str) { - if let Some(value) = Vars::get_checksum_unmatch(contents) { - if value == "delete" { - let file = Hashes::extract_filename(line).unwrap(); - fs::remove_file(format!("{}{}", path, file)).unwrap(); - ChecksumAlerts::lines_unmatch_file_deleted(&file); - } - } - } - - pub async fn generate_hashes(path: &str, file: &str, refs: &Vec, flags: &Flags) -> Result<(), Box> { - if !flags.no_checksum { - let path_file = format!( - "{}{}", path, FileUtils::replace_extension( - file, "sha256" - ) - ); - - let mut output_file = File::create(&path_file)?; - - for file_path in refs { - let hash = Hashes::calculate_local_sha256(&file_path)?; - - writeln!( - output_file, "{} {}", hash, file_path.replace( - path, "" - ) - )?; - } - - ChecksumAlerts::created(&path_file); - } - - Ok(()) - } - - pub async fn compare_lines(contents: &str, checksum_file: &str, flags: &Flags) -> Result<(), Box> { - if !flags.no_checksum && !flags.no_checksum_validate { - let path = Vars::get_path(contents); - - if let Some(url) = Vars::get_checksum(contents).await { - let local_hash_file = format!( - "{}{}", path, FileUtils::replace_extension(checksum_file, "sha256") - ); - - let (local_lines, local_total_lines) = Hashes::read_local_file(&local_hash_file)?; - let (remote_lines, remote_total_lines) = Hashes::read_remote_file(&url).await?; - - UI::section_header("checksum validate"); - ChecksumAlerts::lines_total_is_different(local_total_lines, remote_total_lines); - - for (_, (local, remote)) in local_lines.iter().zip(remote_lines.iter()).enumerate() { - if !local.contains(remote) { - ChecksumAlerts::is_different(local); - Self::checksum_unmatch_delete_file(contents, &path, local); - } else { - ChecksumAlerts::is_equal(local); - } - } - } - } - - Ok(()) - } - -} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 415e45e..b4a5c5f 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,3 +1,2 @@ pub mod tasks; -pub mod monset; -pub mod checksum; \ No newline at end of file +pub mod monset; \ No newline at end of file diff --git a/src/cmd/monset.rs b/src/cmd/monset.rs index 6c4d1f4..e1e0973 100644 --- a/src/cmd/monset.rs +++ b/src/cmd/monset.rs @@ -10,7 +10,6 @@ use std::{ use crate::{ args_cli::Flags, - cmd::tasks::Tasks, utils::validation::Validate, ui::errors_alerts::ErrorsAlerts, @@ -37,13 +36,11 @@ impl Monset { Ok(Cursor::new(buffer.clone())) } - pub async fn downloads(run: &str, flags: &Flags) -> Result, Box> { + pub async fn downloads(run: &str, flags: &Flags) -> Result<(), Box> { let mut reader = Self::read_file(run).await?; - let refs = DownloadsBlock::read_lines(&mut reader, flags, run).await?; + let _ = DownloadsBlock::read_lines(&mut reader, flags).await?; - Tasks::compress(run, &refs)?; - - Ok(refs) + Ok(()) } pub async fn run_code(run: &str) -> Result<(), Box> { diff --git a/src/cmd/tasks.rs b/src/cmd/tasks.rs index 52a82d5..f5b845b 100644 --- a/src/cmd/tasks.rs +++ b/src/cmd/tasks.rs @@ -1,40 +1,17 @@ use is_url::is_url; -use zip::{ - CompressionMethod, - - write::{ - FileOptions, - ExtendedFileOptions - }, -}; - use std::{ - fs::File, - path::Path, borrow::Cow, error::Error, - - io::{ - Read, - Write, - Result as IoResult - }, }; use crate::{ args_cli::Flags, - - monset::{ - vars::Vars, - macros::Macros, - }, + monset::macros::Macros, ui::{ - ui_base::UI, errors_alerts::ErrorsAlerts, success_alerts::SuccessAlerts, - compress_alerts::CompressAlerts, }, system::{ @@ -50,44 +27,6 @@ pub struct Tasks; impl Tasks { - pub fn compress(contents: &str, files: &Vec) -> IoResult<()> { - if let Some(zip_file) = Vars::get_compress(contents) { - UI::section_header("Compressing files"); - let folder_path = Vars::get_path(contents); - - let output_path = Path::new(&zip_file); - let output_file = File::create(output_path)?; - - let mut zip = zip::ZipWriter::new(output_file); - let options: FileOptions = FileOptions::default() - .compression_method(CompressionMethod::Deflated) - .unix_permissions(0o755); - - for file in files { - let path = Path::new(file); - let name = path.strip_prefix(Path::new(&folder_path)).unwrap(); - zip.start_file(name.to_str().unwrap(), options.clone())?; - - let mut f = File::open(path)?; - let mut buffer = Vec::new(); - - f.read_to_end(&mut buffer)?; - zip.write_all(&buffer)?; - - CompressAlerts::added( - &file.replace( - &format!("{}/", &folder_path), "" - ), &zip_file - ); - } - - zip.finish()?; - CompressAlerts::completed(&zip_file); - } - - Ok(()) - } - pub async fn download(url: &str, path: &str, flags: &Flags) -> Result> { let mut line_url = Cow::Borrowed( url.trim() diff --git a/src/monset/blocks/downloads_block.rs b/src/monset/blocks/downloads_block.rs index 9718622..2ba85ab 100644 --- a/src/monset/blocks/downloads_block.rs +++ b/src/monset/blocks/downloads_block.rs @@ -7,6 +7,7 @@ use std::{ use crate::{ args_cli::Flags, + cmd::tasks::Tasks, utils::file::FileUtils, system::providers::Providers, @@ -15,11 +16,6 @@ use crate::{ macros_alerts::MacrosAlerts, }, - cmd::{ - tasks::Tasks, - checksum::Checksum, - }, - monset::{ vars::Vars, macros::Macros, @@ -31,9 +27,7 @@ pub struct DownloadsBlock; impl DownloadsBlock { - pub async fn read_lines(reader: R, flags: &Flags, checksum_file: &str) -> Result, Box> where R: BufRead { - let mut links = Vec::new(); - + pub async fn read_lines(reader: R, flags: &Flags) -> Result<(), Box> where R: BufRead { let contents = reader.lines().collect::, _>>()?.join("\n"); let path = Vars::get_path(&contents); @@ -46,13 +40,11 @@ impl DownloadsBlock { let end_index = contents.rfind("}"); if let (Some(start_index), Some(end_index)) = (start_index, end_index) { - let mut refs = Vec::new(); - FileUtils::create_path(&path); let downloads_content = &contents[start_index + "downloads ".len()..end_index]; if downloads_content.trim().starts_with("commands {") { - return Ok(links); + return Ok(()); } UI::section_header("downloads"); @@ -73,15 +65,11 @@ impl DownloadsBlock { if !Macros::handle_check_macro_line(&line, "ignore") { if !final_url.is_empty() && is_url(&final_url) && final_url.starts_with("http") { - let success = Tasks::download( + Tasks::download( &url, &path, flags, ).await?; - - refs.push(success); - let url_no_macros = Macros::remove_macros(&final_url); - links.push(url_no_macros.to_string()); } } else { MacrosAlerts::ignore(&final_url); @@ -90,16 +78,11 @@ impl DownloadsBlock { Vars::get_open(&contents, flags.no_open_link).await; ReadMeBlock::render_var_and_save_file(&contents, flags).await?; - - Checksum::generate_hashes(&path, checksum_file, &refs, flags).await?; - Checksum::compare_lines(&contents, checksum_file, flags).await?; - - Tasks::compress(&contents, &refs)?; } else { eprintln!("'downloads' block not found in file."); } - Ok(links) + Ok(()) } } diff --git a/src/monset/vars.rs b/src/monset/vars.rs index 74bf454..2f1e668 100644 --- a/src/monset/vars.rs +++ b/src/monset/vars.rs @@ -45,35 +45,5 @@ impl Vars { None } } - - pub fn get_compress(contents: &str) -> Option { - let readme_pattern = Regex::new(BlocksRegExp::GET_COMPRESS_VAR).unwrap(); - - if let Some(caps) = readme_pattern.captures(&contents) { - caps.get(1).map(|m| m.as_str().to_string()) - } else { - None - } - } - - pub async fn get_checksum(contents: &str) -> Option { - let readme_pattern = Regex::new(BlocksRegExp::GET_CHECKSUM_VAR).unwrap(); - - if let Some(caps) = readme_pattern.captures(&contents) { - caps.get(1).map(|m| m.as_str().to_string()) - } else { - None - } - } - - pub fn get_checksum_unmatch(contents: &str) -> Option { - let readme_pattern = Regex::new(BlocksRegExp::GET_CHECKSUM_UNMATCH_ACTION).unwrap(); - if let Some(caps) = readme_pattern.captures(&contents) { - caps.get(1).map(|m| m.as_str().to_string()) - } else { - None - } - } - } diff --git a/src/regexp/regex_blocks.rs b/src/regexp/regex_blocks.rs index 17e1650..454ba6c 100644 --- a/src/regexp/regex_blocks.rs +++ b/src/regexp/regex_blocks.rs @@ -13,10 +13,4 @@ impl BlocksRegExp { pub const GET_README_VAR: &'static str = r#"(?i)readme\s*=\s*"([^"]+)""#; - pub const GET_COMPRESS_VAR: &'static str = r#"(?i)compress\s*=\s*"([^"]+)""#; - - pub const GET_CHECKSUM_VAR: &'static str = r#"(?i)checksum\s*=\s*"([^"]+)""#; - - pub const GET_CHECKSUM_UNMATCH_ACTION: &'static str = r#"(?i)checksum\.unmatch\s*=\s*"([^"]+)""#; - } diff --git a/src/regexp/regex_core.rs b/src/regexp/regex_core.rs index b5fdcca..5078f34 100644 --- a/src/regexp/regex_core.rs +++ b/src/regexp/regex_core.rs @@ -8,8 +8,6 @@ impl CoreRegExp { pub const RENDER_EXTRA_GIST: &'static str = r#"\[!extra gist data=['"](.*?)['"]\]"#; pub const RENDER_EXTRA_QRCODE: &'static str = r#"\[!extra qrcode data=['"](.*?)['"], size=(\d+)\]"#; - pub const GET_CHECKSUM: &'static str = r"(?i)\b([a-f0-9]{64})\b\s+(.+)"; - pub const CLEAN_LINE: &'static str = r"\s*\{\s*\}\s*"; } diff --git a/src/system/hashes.rs b/src/system/hashes.rs index 47ffd76..fd1f761 100644 --- a/src/system/hashes.rs +++ b/src/system/hashes.rs @@ -1,14 +1,7 @@ -use regex::Regex; - use std::{ fs::File, + io::Read, error::Error, - - io::{ - Read, - BufRead, - BufReader, - }, }; use sha2::{ @@ -16,64 +9,10 @@ use sha2::{ Sha256, }; -use crate::{ - utils::remote::Remote, - regexp::regex_core::CoreRegExp, -}; - pub struct Hashes; impl Hashes { - pub fn extract_filename(line: &str) -> Result> { - let re = Regex::new(CoreRegExp::GET_CHECKSUM).unwrap(); - let captures = re.captures(line).ok_or("No match found")?; - let filename = captures.get(2).unwrap().as_str().to_string(); - - Ok(filename) - } - - pub fn extract_hash_and_filename(line: &str) -> Result<(String, String), Box> { - let re = Regex::new(CoreRegExp::GET_CHECKSUM).unwrap(); - let captures = re.captures(line).ok_or("No match found")?; - - let hash = captures.get(1).unwrap().as_str().to_string(); - let filename = captures.get(2).unwrap().as_str().to_string(); - - Ok((hash, filename)) - } - - pub fn read_local_file(file: &str) -> Result<(Vec, usize), Box> { - let mut lines = Vec::new(); - - let file = File::open(file)?; - let reader = BufReader::new(file); - - for line in reader.lines() { - let line = line?; - - if !line.trim().is_empty() { - lines.push(line); - } - } - - let total_lines = &lines.len(); - Ok((lines, *total_lines)) - } - - pub async fn read_remote_file(url: &str) -> Result<(Vec, usize), Box> { - let body = Remote::content(url).await?; - - let lines: Vec = body - .lines() - .map(|s| s.to_string()) - .filter(|line| !line.trim().is_empty()) - .collect(); - - let total_lines = &lines.len(); - Ok((lines, *total_lines)) - } - pub fn calculate_local_sha256(file_path: &str) -> Result> { let mut file = File::open(file_path)?; let mut hasher = Sha256::new(); diff --git a/src/ui/checksum_alerts.rs b/src/ui/checksum_alerts.rs deleted file mode 100644 index 70f42b7..0000000 --- a/src/ui/checksum_alerts.rs +++ /dev/null @@ -1,88 +0,0 @@ -extern crate colored; - -use colored::*; - -use crate::{ - system::{ - hashes::Hashes, - general::General, - }, - - ui::{ - ui_base::UI, - emojis::Emojis, - }, -}; - -pub struct ChecksumAlerts; - -impl ChecksumAlerts { - - pub fn created(file: &str) { - let current_datetime = General::date_time(); - - UI::section_header("checksum"); - - println!( - "{} Hashes file created with successfully ({})", - current_datetime.blue().bold(), - file.cyan() - ); - } - - pub fn is_equal(line: &str) -> bool { - if let Ok((hash, filename)) = Hashes::extract_hash_and_filename(line) { - let current_datetime = General::date_time(); - - println!( - "{} The hash {} of {} is match. {}", - current_datetime.green().bold(), - hash.yellow(), - filename.blue(), - Emojis::CHECKED - ); - } - - false - } - - pub fn is_different(line: &str) -> bool { - if let Ok((hash, filename)) = Hashes::extract_hash_and_filename(line) { - let current_datetime = General::date_time(); - - println!( - "{} The hash {} of {} is not match. {}", - current_datetime.red().bold(), - hash.yellow(), - filename.blue(), - Emojis::ERROR - ); - } - - true - } - - pub fn lines_total_is_different(local_total_lines: usize, remote_total_lines: usize) { - if local_total_lines != remote_total_lines { - let current_datetime = General::date_time(); - - println!("{} The number of lines in the files is different (Lines: {} (local) of {} (referencies)). {}", - current_datetime.red().bold(), - local_total_lines, - remote_total_lines, - Emojis::ERROR - ); - } - } - - pub fn lines_unmatch_file_deleted(filename: &str) { - let current_datetime = General::date_time(); - - println!("{} The file {} was deleted, because is hashes unmatched. {}", - current_datetime.red().bold(), - filename.blue(), - Emojis::FORBIDDEN - ); - } - -} diff --git a/src/ui/compress_alerts.rs b/src/ui/compress_alerts.rs deleted file mode 100644 index b4c406a..0000000 --- a/src/ui/compress_alerts.rs +++ /dev/null @@ -1,37 +0,0 @@ -extern crate colored; - -use colored::*; - -use crate::{ - ui::emojis::Emojis, - system::general::General, -}; - -pub struct CompressAlerts; - -impl CompressAlerts { - - pub fn added(file: &str, zip_file: &str) { - let current_datetime = General::date_time(); - - println!( - "{} -> Added {} in: {}. {}", - current_datetime.green().bold(), - file.blue(), - zip_file.cyan(), - Emojis::ADD - ); - } - - pub fn completed(zip_file: &str) { - let current_datetime = General::date_time(); - - println!( - "{} -> All files in the folder have been compressed into {}. {}", - current_datetime.green().bold(), - zip_file.blue(), - Emojis::COMPRESS - ); - } - -} diff --git a/src/ui/emojis.rs b/src/ui/emojis.rs deleted file mode 100644 index a3d34d2..0000000 --- a/src/ui/emojis.rs +++ /dev/null @@ -1,16 +0,0 @@ -pub struct Emojis; - -impl Emojis { - - pub const HOME: &'static str = "🏠"; - pub const CLOCK: &'static str = "⏰"; - pub const VERSION: &'static str = "📜"; - - pub const LOCKED: &'static str = "🔒"; - pub const CHECKED: &'static str = "✅"; - pub const ERROR: &'static str = "❌"; - pub const FORBIDDEN: &'static str = "⛔"; - pub const ADD: &'static str = "➕"; - pub const COMPRESS: &'static str = "📦"; - -} \ No newline at end of file diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 06c715c..274b6bc 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,8 +1,5 @@ -pub mod emojis; pub mod ui_base; pub mod macros_alerts; pub mod errors_alerts; pub mod success_alerts; -pub mod checksum_alerts; -pub mod compress_alerts; pub mod errors_commands_alerts; \ No newline at end of file diff --git a/src/ui/success_alerts.rs b/src/ui/success_alerts.rs index 03dbe69..465f30f 100644 --- a/src/ui/success_alerts.rs +++ b/src/ui/success_alerts.rs @@ -3,7 +3,6 @@ extern crate colored; use colored::*; use crate::{ - ui::emojis::Emojis, utils::domains::Domains, system::general::General, }; @@ -24,7 +23,7 @@ impl SuccessAlerts { let current_datetime = General::date_time(); if password { - encrypted_emoji = Emojis::LOCKED; + encrypted_emoji = "🔒"; } println!( diff --git a/src/ui/ui_base.rs b/src/ui/ui_base.rs index d5ff84a..3f41d42 100644 --- a/src/ui/ui_base.rs +++ b/src/ui/ui_base.rs @@ -6,7 +6,6 @@ use figlet_rs::FIGfont; use indicatif::ProgressStyle; use crate::{ - ui::emojis::Emojis, utils::str::StrUtils, consts::global::Global, system::general::General, @@ -25,9 +24,9 @@ impl UI { if let Some(title) = standard_font.convert(&name) { println!("{}", &title.to_string().bold().cyan()); println!(""); - println!("{} Version: {}", Emojis::VERSION, Global::APP_VERSION.yellow()); - println!("{} Homepage: {} • {}", Emojis::HOME, Global::APP_HOMEPAGE.blue(), Global::APP_AUTHOR.green()); - println!("{} Started in: {}", Emojis::CLOCK, General::date_time().magenta()); + println!("Version: {}", Global::APP_VERSION.yellow()); + println!("Homepage: {} • {}", Global::APP_HOMEPAGE.blue(), Global::APP_AUTHOR.green()); + println!("Started in: {}", General::date_time().magenta()); } } }