From 42a996f49115744eb9a0d7dd44075f89b0b66a85 Mon Sep 17 00:00:00 2001 From: Rabindra Dhakal Date: Wed, 30 Oct 2024 19:15:19 +0545 Subject: [PATCH] feat(yes): skip prompts and select first value --- src/cli.rs | 8 ++++++++ src/lib.rs | 14 +++++++++++--- src/registry/mod.rs | 9 ++++++--- src/registry/package/install.rs | 31 ++++++++++++++++++------------- src/registry/package/mod.rs | 2 ++ src/registry/package/update.rs | 3 ++- src/registry/storage.rs | 28 ++++++++++++++++------------ 7 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 6c91f79..435d414 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -26,6 +26,10 @@ pub enum Commands { #[arg(required = false, short, long)] force: bool, + /// Skip all prompts and use first + #[arg(required = false, short, long)] + yes: bool, + /// Set portable dir for home & config #[arg(required = false, short, long, num_args = 0..=1)] portable: Option>, @@ -111,6 +115,10 @@ pub enum Commands { #[command(arg_required_else_help = true)] #[clap(name = "run", visible_alias = "exec", alias = "execute")] Run { + /// Skip all prompts and use first + #[arg(required = false, short, long)] + yes: bool, + /// Command to execute #[arg(required = true, trailing_var_arg = true)] command: Vec, diff --git a/src/lib.rs b/src/lib.rs index 1aacce6..ce36410 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ pub async fn init() -> Result<()> { portable, portable_home, portable_config, + yes, } => { if portable.is_some() && (portable_home.is_some() || portable_config.is_some()) { error!("--portable cannot be used with --portable-home or --portable-config"); @@ -53,7 +54,14 @@ pub async fn init() -> Result<()> { let portable_config = portable_config.map(|p| p.unwrap_or_default()); registry - .install_packages(&packages, force, portable, portable_home, portable_config) + .install_packages( + &packages, + force, + portable, + portable_home, + portable_config, + yes, + ) .await?; } Commands::Sync => { @@ -84,8 +92,8 @@ pub async fn init() -> Result<()> { Commands::Inspect { package } => { registry.inspect(&package).await?; } - Commands::Run { command } => { - registry.run(command.as_ref()).await?; + Commands::Run { command, yes } => { + registry.run(command.as_ref(), yes).await?; } Commands::Use { package } => { registry.use_package(&package).await?; diff --git a/src/registry/mod.rs b/src/registry/mod.rs index 73d0730..995ecd1 100644 --- a/src/registry/mod.rs +++ b/src/registry/mod.rs @@ -127,6 +127,7 @@ impl PackageRegistry { portable: Option, portable_home: Option, portable_config: Option, + yes: bool, ) -> Result<()> { self.storage .install_packages( @@ -136,6 +137,7 @@ impl PackageRegistry { portable, portable_home, portable_config, + yes, ) .await } @@ -323,13 +325,13 @@ impl PackageRegistry { self.storage.inspect(package_name).await } - pub async fn run(&self, command: &[String]) -> Result<()> { - self.storage.run(command).await + pub async fn run(&self, command: &[String], yes: bool) -> Result<()> { + self.storage.run(command, yes).await } pub async fn use_package(&self, package_name: &str) -> Result<()> { let installed_guard = self.installed_packages.lock().await; - let resolved_package = self.storage.resolve_package(package_name)?; + let resolved_package = self.storage.resolve_package(package_name, false)?; let result = installed_guard.use_package(&resolved_package).await; drop(installed_guard); match result { @@ -352,6 +354,7 @@ impl PackageRegistry { None, None, None, + false, ) .await?; diff --git a/src/registry/package/install.rs b/src/registry/package/install.rs index 03ef7af..ab7df33 100644 --- a/src/registry/package/install.rs +++ b/src/registry/package/install.rs @@ -50,6 +50,7 @@ impl Installer { portable_home: Option, portable_config: Option, multi_progress: Arc, + yes: bool, ) -> Result<()> { let package = &self.resolved_package.package; let is_installed = installed_packages @@ -162,25 +163,29 @@ impl Installer { download_progress.finish(); if package.bsum == "null" { - error!( + warn!( "Missing checksum for {}. Installing anyway.", package.full_name('/').color(Color::BrightBlue) ); } else { let result = validate_checksum(&package.bsum, &self.temp_path).await; if result.is_err() { - eprint!( - "\n{}: Checksum verification failed. Do you want to remove the package? (y/n): ", - prefix - ); - std::io::stdout().flush()?; - - let mut response = String::new(); - std::io::stdin().read_line(&mut response)?; - - if response.trim().eq_ignore_ascii_case("y") { - tokio::fs::remove_file(&temp_path).await?; - return Err(anyhow::anyhow!("Checksum verification failed.")); + if yes { + warn!("Checksum verification failed. Installing anyway."); + } else { + eprint!( + "\n{}: Checksum verification failed. Do you want to remove the package? (y/n): ", + prefix + ); + std::io::stdout().flush()?; + + let mut response = String::new(); + std::io::stdin().read_line(&mut response)?; + + if response.trim().eq_ignore_ascii_case("y") { + tokio::fs::remove_file(&temp_path).await?; + return Err(anyhow::anyhow!("Checksum verification failed.")); + } } } } diff --git a/src/registry/package/mod.rs b/src/registry/package/mod.rs index 7a5f4af..9fa2ae5 100644 --- a/src/registry/package/mod.rs +++ b/src/registry/package/mod.rs @@ -58,6 +58,7 @@ impl ResolvedPackage { portable_home: Option, portable_config: Option, multi_progress: Arc, + yes: bool, ) -> Result<()> { let install_path = self.package.get_install_path(&self.package.bsum); let mut installer = Installer::new(self, install_path); @@ -71,6 +72,7 @@ impl ResolvedPackage { portable_home, portable_config, multi_progress, + yes, ) .await?; Ok(()) diff --git a/src/registry/package/update.rs b/src/registry/package/update.rs index 60a6e2d..c8c788b 100644 --- a/src/registry/package/update.rs +++ b/src/registry/package/update.rs @@ -33,7 +33,7 @@ impl Updater { Some(r) => { let resolved_packages: Result> = r .iter() - .map(|package_name| registry.storage.resolve_package(package_name)) + .map(|package_name| registry.storage.resolve_package(package_name, false)) .collect(); resolved_packages? } @@ -89,6 +89,7 @@ impl Updater { None, None, multi_progress.clone(), + true, ) .await?; update_count += 1; diff --git a/src/registry/storage.rs b/src/registry/storage.rs index ab6a5ca..b3c1354 100644 --- a/src/registry/storage.rs +++ b/src/registry/storage.rs @@ -58,7 +58,7 @@ impl PackageStorage { self.repository.insert(repo_name.to_owned(), packages); } - pub fn resolve_package(&self, package_name: &str) -> Result { + pub fn resolve_package(&self, package_name: &str, yes: bool) -> Result { let pkg_query = parse_package_query(package_name); let packages = self .get_packages(&pkg_query) @@ -69,12 +69,14 @@ impl PackageStorage { "Is it a fish? Is is a frog? On no, it's a fly." )); } - 1 => &ResolvedPackage { - repo_name: packages[0].repo_name.to_owned(), - package: packages[0].package.to_owned(), - collection: packages[0].collection.to_owned(), - }, - _ => select_package_variant(&packages)?, + 1 => &packages[0], + _ => { + if yes { + &packages[0] + } else { + select_package_variant(&packages)? + } + } }; Ok(package.to_owned()) @@ -88,10 +90,11 @@ impl PackageStorage { portable: Option, portable_home: Option, portable_config: Option, + yes: bool, ) -> Result<()> { let resolved_packages: Result> = package_names .iter() - .map(|package_name| self.resolve_package(package_name)) + .map(|package_name| self.resolve_package(package_name, yes)) .collect(); let resolved_packages = resolved_packages?; @@ -162,6 +165,7 @@ impl PackageStorage { portable_home, portable_config, multi_progress, + yes, ) .await { @@ -214,7 +218,7 @@ impl PackageStorage { pub async fn remove_packages(&self, package_names: &[String]) -> Result<()> { let resolved_packages: Vec = package_names .iter() - .filter_map(|package_name| self.resolve_package(package_name).ok()) + .filter_map(|package_name| self.resolve_package(package_name, false).ok()) .collect(); for package in resolved_packages { package.remove().await?; @@ -357,7 +361,7 @@ impl PackageStorage { } pub async fn inspect(&self, package_name: &str) -> Result<()> { - let resolved_pkg = self.resolve_package(package_name)?; + let resolved_pkg = self.resolve_package(package_name, false)?; let client = reqwest::Client::new(); let url = resolved_pkg.package.build_log; @@ -408,7 +412,7 @@ impl PackageStorage { Ok(()) } - pub async fn run(&self, command: &[String]) -> Result<()> { + pub async fn run(&self, command: &[String], yes: bool) -> Result<()> { fs::create_dir_all(&*CACHE_PATH).await?; let package_name = &command[0]; @@ -417,7 +421,7 @@ impl PackageStorage { } else { &[] }; - let runner = if let Ok(resolved_pkg) = self.resolve_package(package_name) { + let runner = if let Ok(resolved_pkg) = self.resolve_package(package_name, yes) { let package_path = CACHE_PATH.join(&resolved_pkg.package.bin_name); Runner::new(&resolved_pkg, package_path, args) } else {