Implement wasmer run {url}

This commit is contained in:
Felix Schütt
2022-11-10 14:23:52 +01:00
parent b7c185b1a6
commit e8c422d520
4 changed files with 227 additions and 3 deletions

View File

@@ -11,6 +11,8 @@ use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use url::Url;
use wasmer::FunctionEnv;
use wasmer::*;
#[cfg(feature = "cache")]
@@ -827,6 +829,10 @@ pub(crate) fn try_run_package_or_file(
) -> Result<(), anyhow::Error> {
let debug_msgs_allowed = isatty::stdout_isatty();
if let Ok(url) = url::Url::parse(&format!("{}", r.path.display())) {
return try_run_url(&url, args, r, debug);
}
// Check "r.path" is a file or a package / command name
if r.path.exists() {
if r.path.is_dir() && r.path.join("wapm.toml").exists() {
@@ -908,3 +914,30 @@ pub(crate) fn try_run_package_or_file(
// else: local package not found - try to download and install package
try_autoinstall_package(args, &sv, package_download_info, r.force_install)
}
fn try_run_url(url: &Url, _args: &[String], r: &Run, _debug: bool) -> Result<(), anyhow::Error> {
let checksum = wasmer_registry::get_remote_webc_checksum(&url)
.map_err(|e| anyhow::anyhow!("error fetching {url}: {e}"))?;
if !wasmer_registry::get_all_installed_webc_packages()
.iter()
.any(|p| p.checksum == checksum)
{
let sp = start_spinner(format!("Installing {}", url));
wasmer_registry::install_webc_package(&url, &checksum)
.map_err(|e| anyhow::anyhow!("error fetching {url}: {e}"))?;
if let Some(sp) = sp {
sp.close();
}
}
let webc_install_path = wasmer_registry::get_webc_dir()
.ok_or_else(|| anyhow::anyhow!("Error installing package: webc download failed"))?
.join(checksum);
let mut r = r.clone();
r.path = webc_install_path;
r.execute()
}