Skip to content

Commit

Permalink
feat(yes): skip prompts and select first value
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 30, 2024
1 parent 2c8d894 commit 286743e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 33 deletions.
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>>,
Expand Down Expand Up @@ -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<String>,
Expand Down
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 => {
Expand Down Expand Up @@ -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?;
Expand Down
10 changes: 7 additions & 3 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl PackageRegistry {
portable: Option<String>,
portable_home: Option<String>,
portable_config: Option<String>,
yes: bool,
) -> Result<()> {
self.storage
.install_packages(
Expand All @@ -136,6 +137,7 @@ impl PackageRegistry {
portable,
portable_home,
portable_config,
yes,
)
.await
}
Expand Down Expand Up @@ -218,6 +220,7 @@ impl PackageRegistry {
"Build Script",
package.build_script.clone().color(Color::BrightBlue),
),
("Note", package.note.clone().color(Color::BrightCyan)),
(
"Category",
package.category.clone().color(Color::BrightCyan),
Expand Down Expand Up @@ -323,13 +326,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 {
Expand All @@ -352,6 +355,7 @@ impl PackageRegistry {
None,
None,
None,
false,
)
.await?;

Expand Down
32 changes: 18 additions & 14 deletions src/registry/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
constant::{BIN_PATH, PACKAGES_PATH},
util::{calculate_checksum, format_bytes, validate_checksum},
},
error,
registry::{
installed::InstalledPackages,
package::appimage::{integrate_appimage, setup_portable_dir},
Expand Down Expand Up @@ -50,6 +49,7 @@ impl Installer {
portable_home: Option<String>,
portable_config: Option<String>,
multi_progress: Arc<MultiProgress>,
yes: bool,
) -> Result<()> {
let package = &self.resolved_package.package;
let is_installed = installed_packages
Expand Down Expand Up @@ -162,25 +162,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."));
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/registry/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl ResolvedPackage {
portable_home: Option<String>,
portable_config: Option<String>,
multi_progress: Arc<MultiProgress>,
yes: bool,
) -> Result<()> {
let install_path = self.package.get_install_path(&self.package.bsum);
let mut installer = Installer::new(self, install_path);
Expand All @@ -71,6 +72,7 @@ impl ResolvedPackage {
portable_home,
portable_config,
multi_progress,
yes,
)
.await?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/registry/package/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Updater {
Some(r) => {
let resolved_packages: Result<Vec<ResolvedPackage>> = r
.iter()
.map(|package_name| registry.storage.resolve_package(package_name))
.map(|package_name| registry.storage.resolve_package(package_name, true))
.collect();
resolved_packages?
}
Expand Down Expand Up @@ -89,6 +89,7 @@ impl Updater {
None,
None,
multi_progress.clone(),
true,
)
.await?;
update_count += 1;
Expand Down
29 changes: 17 additions & 12 deletions src/registry/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl PackageStorage {
self.repository.insert(repo_name.to_owned(), packages);
}

pub fn resolve_package(&self, package_name: &str) -> Result<ResolvedPackage> {
pub fn resolve_package(&self, package_name: &str, yes: bool) -> Result<ResolvedPackage> {
let pkg_query = parse_package_query(package_name);
let packages = self
.get_packages(&pkg_query)
Expand All @@ -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())
Expand All @@ -88,10 +90,11 @@ impl PackageStorage {
portable: Option<String>,
portable_home: Option<String>,
portable_config: Option<String>,
yes: bool,
) -> Result<()> {
let resolved_packages: Result<Vec<ResolvedPackage>> = 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?;

Expand Down Expand Up @@ -162,6 +165,7 @@ impl PackageStorage {
portable_home,
portable_config,
multi_progress,
yes,
)
.await
{
Expand Down Expand Up @@ -191,6 +195,7 @@ impl PackageStorage {
portable_home.clone(),
portable_config.clone(),
multi_progress.clone(),
yes,
)
.await
{
Expand All @@ -214,7 +219,7 @@ impl PackageStorage {
pub async fn remove_packages(&self, package_names: &[String]) -> Result<()> {
let resolved_packages: Vec<ResolvedPackage> = 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?;
Expand Down Expand Up @@ -357,7 +362,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;
Expand Down Expand Up @@ -408,7 +413,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];
Expand All @@ -417,7 +422,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 {
Expand Down

0 comments on commit 286743e

Please sign in to comment.