Skip to content

Commit

Permalink
feat(run): run remote binary without metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 10, 2024
1 parent 0967773 commit 695e0da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
23 changes: 6 additions & 17 deletions src/registry/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
mod install;
mod remove;
mod run;
pub mod run;
pub mod update;

use std::{
fmt::Display,
path::{Path, PathBuf},
sync::Arc,
};
use std::{fmt::Display, path::PathBuf, sync::Arc};

use anyhow::Result;
use install::Installer;
use remove::Remover;
use run::Runner;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;

use crate::core::constant::PACKAGES_PATH;

use super::installed::InstalledPackages;

#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct Package {
pub name: String,
pub bin_name: String,
Expand All @@ -39,7 +34,7 @@ pub struct Package {
pub variant: Option<String>,
}

#[derive(Debug, Clone)]
#[derive(Default, Debug, Clone)]
pub struct ResolvedPackage {
pub repo_name: String,
pub root_path: RootPath,
Expand Down Expand Up @@ -69,13 +64,6 @@ impl ResolvedPackage {
remover.execute(&mut installed_packages).await?;
Ok(())
}

pub async fn run(&self, args: &[String], cache_dir: &Path) -> Result<()> {
let package_path = cache_dir.join(&self.package.bin_name);
let runner = Runner::new(self, package_path, args);
runner.execute().await?;
Ok(())
}
}

impl Package {
Expand Down Expand Up @@ -106,8 +94,9 @@ pub struct PackageQuery {
pub root_path: Option<RootPath>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum RootPath {
#[default]
Bin,
Base,
Pkg,
Expand Down
27 changes: 3 additions & 24 deletions src/registry/package/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{
fs::Permissions, io::Write, os::unix::fs::PermissionsExt, path::PathBuf, process::Command,
};
use std::{fs::Permissions, os::unix::fs::PermissionsExt, path::PathBuf, process::Command};

use anyhow::{Context, Result};
use futures::StreamExt;
Expand Down Expand Up @@ -40,14 +38,7 @@ impl Runner {
));
} else {
println!("Found existing cache for {}", package_name);
let result = validate_checksum(&package.bsum, &self.install_path).await;
if result.is_err() {
eprintln!("Checksum validation failed for {}", package_name);
eprintln!("The package will be re-downloaded.");
} else {
self.run().await?;
return Ok(());
}
return self.run().await;
}
}

Expand Down Expand Up @@ -112,19 +103,7 @@ impl Runner {
} 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): ",
package_name
);
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(&self.temp_path).await?;
return Err(anyhow::anyhow!(""));
}
eprint!("\n{}: Checksum verification failed.", package_name);
}
}

Expand Down
39 changes: 34 additions & 5 deletions src/registry/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tokio::{
use crate::{
core::{
config::CONFIG,
util::{build_path, format_bytes},
util::{build_path, format_bytes, get_platform},
},
registry::{
installed::InstalledPackages,
Expand All @@ -28,7 +28,7 @@ use crate::{
};

use super::{
package::{Package, PackageQuery, RootPath},
package::{run::Runner, Package, PackageQuery, RootPath},
select_package_variant,
};

Expand Down Expand Up @@ -379,14 +379,43 @@ impl PackageStorage {
fs::create_dir_all(&cache_dir).await?;

let package_name = &command[0];
let resolved_pkg = self.resolve_package(package_name)?;

let args = if command.len() > 1 {
&command[1..]
} else {
&[]
};
resolved_pkg.run(args, &cache_dir).await?;
let runner = if let Ok(resolved_pkg) = self.resolve_package(package_name) {
let package_path = cache_dir.join(&resolved_pkg.package.bin_name);
Runner::new(&resolved_pkg, package_path, args)
} else {
let query = parse_package_query(package_name);
let package_path = cache_dir.join(&query.name);
let mut resolved_pkg = ResolvedPackage::default();
resolved_pkg.package.name = query.name;
resolved_pkg.package.variant = query.variant;
resolved_pkg.root_path = query.root_path.unwrap_or_default();

// TODO: don't use just first repo
let platform = get_platform();
let repo = &CONFIG.repositories[0];
let base_url = format!("{}/{}", repo.url, platform);

let root_path = if resolved_pkg.root_path == RootPath::Base {
"/Baseutils"
} else {
""
};
let download_url = format!(
"{}{}/{}",
base_url,
root_path,
resolved_pkg.package.full_name('/')
);
resolved_pkg.package.download_url = download_url;
Runner::new(&resolved_pkg, package_path, args)
};

runner.execute().await?;

Ok(())
}
Expand Down

0 comments on commit 695e0da

Please sign in to comment.