From bfe004fdf7e8d6fc3fc1be27818ad9cc4a892978 Mon Sep 17 00:00:00 2001 From: Rabindra Dhakal Date: Wed, 6 Nov 2024 08:06:58 +0545 Subject: [PATCH] fix(install): add constraints to local installs binary name --- src/core/health.rs | 10 +++++----- src/core/log.rs | 36 ++++++++++++++++++++++++++++++++---- src/core/util.rs | 2 +- src/lib.rs | 2 +- src/misc/download.rs | 4 ++-- src/package/appimage.rs | 9 ++++++--- src/package/mod.rs | 11 ++++++++++- src/package/remove.rs | 4 ++-- src/package/run.rs | 6 +++--- src/package/update.rs | 4 ++-- src/registry/loader.rs | 4 ++-- src/registry/mod.rs | 8 ++++---- src/registry/storage.rs | 4 ++-- 13 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/core/health.rs b/src/core/health.rs index 9e6872a..e4ccc20 100644 --- a/src/core/health.rs +++ b/src/core/health.rs @@ -9,7 +9,7 @@ use crate::{ color::{Color, ColorExt}, constant::CAP_MKNOD, }, - success, warn, + successln, warnln, }; use super::constant::CAP_SYS_ADMIN; @@ -64,11 +64,11 @@ pub async fn check_health() { println!("{0} USER NAMESPACE CHECK {0}", "☵".repeat(4)); for error in &errors { - warn!("{}", error); + warnln!("{}", error); } if errors.is_empty() { - success!("User namespace checked successfully.") + successln!("User namespace checked successfully.") } else { println!( "{} {}", @@ -168,13 +168,13 @@ async fn check_fusermount() { } if !error.is_empty() { - warn!( + warnln!( "{}\n{} {}", error, "More info at:".color(Color::Cyan), "https://l.ajam.dev/fuse".color(Color::Blue) ); } else { - success!("Fuse checked successfully."); + successln!("Fuse checked successfully."); } } diff --git a/src/core/log.rs b/src/core/log.rs index 20ca435..2ea6da2 100644 --- a/src/core/log.rs +++ b/src/core/log.rs @@ -1,27 +1,55 @@ #[macro_export] -macro_rules! warn { +macro_rules! warnln { ($($arg:tt)*) => { println!("{} {}", "[WARN]".color(Color::BrightYellow).bold(), format!($($arg)*)) }; } #[macro_export] -macro_rules! info { +macro_rules! infoln { ($($arg:tt)*) => { println!("{} {}", "[INFO]".color(Color::BrightBlue).bold(), format!($($arg)*)) }; } #[macro_export] -macro_rules! error { +macro_rules! errorln { ($($arg:tt)*) => { eprintln!("{} {}", "[ERROR]".color(Color::BrightRed).bold(), format!($($arg)*)) }; } #[macro_export] -macro_rules! success { +macro_rules! successln { ($($arg:tt)*) => { println!("{} {}", "[SUCCESS]".color(Color::BrightGreen).bold(), format!($($arg)*)) }; } + +#[macro_export] +macro_rules! warn { + ($($arg:tt)*) => { + print!("{} {}", "[WARN]".color(Color::BrightYellow).bold(), format!($($arg)*)) + }; +} + +#[macro_export] +macro_rules! info { + ($($arg:tt)*) => { + print!("{} {}", "[INFO]".color(Color::BrightBlue).bold(), format!($($arg)*)) + }; +} + +#[macro_export] +macro_rules! error { + ($($arg:tt)*) => { + eprint!("{} {}", "[ERROR]".color(Color::BrightRed).bold(), format!($($arg)*)) + }; +} + +#[macro_export] +macro_rules! success { + ($($arg:tt)*) => { + print!("{} {}", "[SUCCESS]".color(Color::BrightGreen).bold(), format!($($arg)*)) + }; +} diff --git a/src/core/util.rs b/src/core/util.rs index 4662b95..a4644f2 100644 --- a/src/core/util.rs +++ b/src/core/util.rs @@ -385,7 +385,7 @@ pub fn interactive_ask(ques: &str, ask_type: AskType) -> Result { if ask_type == AskType::Warn { warn!("{ques}"); } else { - println!("{ques}"); + print!("{ques}"); } std::io::stdout().flush()?; diff --git a/src/lib.rs b/src/lib.rs index 8f6937d..b89f714 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub async fn init() -> Result<()> { let path_env = env::var("PATH")?; if !path_env.split(':').any(|p| Path::new(p) == *BIN_PATH) { - warn!( + warnln!( "{} is not in {1}. Please add it to {1} to use installed binaries.", &*BIN_PATH.to_string_lossy().color(Color::Blue), "PATH".color(Color::BrightGreen).bold() diff --git a/src/misc/download.rs b/src/misc/download.rs index d71d7c9..3d467e1 100644 --- a/src/misc/download.rs +++ b/src/misc/download.rs @@ -19,7 +19,7 @@ use crate::{ error, package::parse_package_query, registry::{select_single_package, PackageRegistry}, - success, + successln, }; fn extract_filename(url: &str) -> String { @@ -117,7 +117,7 @@ async fn download(url: &str, output: Option) -> Result<()> { fs::set_permissions(&output_path, Permissions::from_mode(0o755)).await?; } - success!("Downloaded {}", output_path.display().color(Color::Blue)); + successln!("Downloaded {}", output_path.display().color(Color::Blue)); Ok(()) } diff --git a/src/package/appimage.rs b/src/package/appimage.rs index 1a5bc37..62b9255 100644 --- a/src/package/appimage.rs +++ b/src/package/appimage.rs @@ -16,7 +16,7 @@ use crate::{ constant::{BIN_PATH, PACKAGES_PATH}, util::{download, home_data_path}, }, - error, info, + error, infoln, }; use super::Package; @@ -68,9 +68,12 @@ fn normalize_image(image: DynamicImage) -> DynamicImage { let (new_width, new_height) = find_nearest_supported_dimension(width, height); if (width, height) != (new_width, new_height) { - info!( + infoln!( "Resizing image from {}x{} to {}x{}", - width, height, new_width, new_height + width, + height, + new_width, + new_height ); image.resize(new_width, new_height, FilterType::Lanczos3) } else { diff --git a/src/package/mod.rs b/src/package/mod.rs index 44aab25..25d72da 100644 --- a/src/package/mod.rs +++ b/src/package/mod.rs @@ -127,7 +127,16 @@ pub fn parse_package_query(query: &str) -> PackageQuery { #[inline] pub fn ask_package_info(name: &str, path: &Path, size: u64) -> Result { - let bin_name = interactive_ask("Binary Name:", AskType::Normal)?; + let bin_name = loop { + let bin_name = interactive_ask("Binary Name: ", AskType::Normal)?; + if bin_name.is_empty() { + eprintln!("Binary name can't be empty."); + } else if !bin_name.chars().all(|c| c.is_alphanumeric()) { + eprintln!("Binary name must only contain letters and numbers."); + } else { + break bin_name; + } + }; let package = Package { pkg: name.to_owned(), diff --git a/src/package/remove.rs b/src/package/remove.rs index f354047..6360952 100644 --- a/src/package/remove.rs +++ b/src/package/remove.rs @@ -10,7 +10,7 @@ use crate::{ }, package::appimage::remove_applinks, registry::installed::{InstalledPackage, InstalledPackages}, - success, + successln, }; pub struct Remover { @@ -34,7 +34,7 @@ impl Remover { self.remove_package_path(&install_dir).await?; installed_packages.unregister_package(&self.package).await?; - success!( + successln!( "Package {} removed successfully.", package.full_name('/').color(Color::Blue) ); diff --git a/src/package/run.rs b/src/package/run.rs index 4546fb3..23e4077 100644 --- a/src/package/run.rs +++ b/src/package/run.rs @@ -9,7 +9,7 @@ use crate::{ color::{Color, ColorExt}, util::{format_bytes, validate_checksum}, }, - error, info, warn, + error, infoln, warnln, }; use super::ResolvedPackage; @@ -43,7 +43,7 @@ impl Runner { self.install_path.to_string_lossy().color(Color::Blue) )); } else { - info!( + infoln!( "Found existing cache for {}", package_name.color(Color::Blue) ); @@ -108,7 +108,7 @@ impl Runner { } if package.bsum == "null" { - warn!( + warnln!( "Missing checksum for {}. Installing anyway.", package.full_name('/').color(Color::Blue) ); diff --git a/src/package/update.rs b/src/package/update.rs index d0cba21..f0ae4da 100644 --- a/src/package/update.rs +++ b/src/package/update.rs @@ -7,7 +7,7 @@ use crate::{ core::color::{Color, ColorExt}, error, registry::PackageRegistry, - success, + successln, }; use super::{parse_package_query, PackageQuery, ResolvedPackage}; @@ -98,7 +98,7 @@ impl Updater { } total_progress_bar.finish_and_clear(); - success!( + successln!( "{} packages updated.", update_count.color(Color::BrightMagenta) ); diff --git a/src/registry/loader.rs b/src/registry/loader.rs index b61f3c4..7826475 100644 --- a/src/registry/loader.rs +++ b/src/registry/loader.rs @@ -6,7 +6,7 @@ use crate::{ color::{Color, ColorExt}, config::Repository, }, - warn, + warnln, }; use super::fetcher::MetadataFetcher; @@ -27,7 +27,7 @@ impl MetadataLoader { .with_file_name(format!("{}.remote.bsum", repo.name)); let local_checksum = fs::read(&checksum_path).await.unwrap_or_default(); if checksum != local_checksum { - warn!("Local registry is outdated. Refetching..."); + warnln!("Local registry is outdated. Refetching..."); let content = fetcher.execute(repo).await; fs::write(checksum_path, &checksum).await?; return content; diff --git a/src/registry/mod.rs b/src/registry/mod.rs index d703b6a..3b14d0f 100644 --- a/src/registry/mod.rs +++ b/src/registry/mod.rs @@ -16,11 +16,11 @@ use crate::{ config::CONFIG, util::{get_terminal_width, wrap_text}, }, - error, info, + error, infoln, package::{ image::get_package_image_string, parse_package_query, update::Updater, ResolvedPackage, }, - success, + successln, }; mod fetcher; @@ -304,7 +304,7 @@ impl PackageRegistry { drop(installed_guard); match result { Ok(_) => { - success!( + successln!( "{} is linked to binary path", package_name.color(Color::Blue) ); @@ -336,7 +336,7 @@ impl PackageRegistry { } pub fn select_single_package(packages: &[ResolvedPackage]) -> Result<&ResolvedPackage> { - info!( + infoln!( "Multiple packages available for {}", packages[0].package.pkg.clone().color(Color::Blue) ); diff --git a/src/registry/storage.rs b/src/registry/storage.rs index ba1b965..222e0c3 100644 --- a/src/registry/storage.rs +++ b/src/registry/storage.rs @@ -30,7 +30,7 @@ use crate::{ ask_package_info, parse_package_query, run::Runner, Package, PackageQuery, ResolvedPackage, }, registry::installed::InstalledPackages, - warn, + warnln, }; use super::select_single_package; @@ -126,7 +126,7 @@ impl PackageStorage { .into_iter() .filter_map(|(package, is_installed)| { if is_installed { - warn!( + warnln!( "{} is already installed - {}", package.package.full_name('/'), if force { "reinstalling" } else { "skipping" }