From 7f187f14cb45f7858fe087072159ded8f34960b2 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 24 Jan 2024 14:39:07 -0800 Subject: [PATCH] Add features. --- crates/download/Cargo.toml | 4 + crates/download/src/download_ext.rs | 68 ++++++++++++++++ crates/download/src/lib.rs | 71 +--------------- crates/unpack/Cargo.toml | 4 + crates/unpack/src/lib.rs | 122 +--------------------------- crates/unpack/src/unpack_ext.rs | 119 +++++++++++++++++++++++++++ 6 files changed, 203 insertions(+), 185 deletions(-) create mode 100644 crates/download/src/download_ext.rs create mode 100644 crates/unpack/src/unpack_ext.rs diff --git a/crates/download/Cargo.toml b/crates/download/Cargo.toml index ba9116a..5fe7c49 100644 --- a/crates/download/Cargo.toml +++ b/crates/download/Cargo.toml @@ -16,3 +16,7 @@ moon_pdk = { workspace = true } [dev-dependencies] moon_pdk_test_utils = { workspace = true } starbase_sandbox = { workspace = true } + +[features] +default = ["wasm"] +wasm = [] diff --git a/crates/download/src/download_ext.rs b/crates/download/src/download_ext.rs new file mode 100644 index 0000000..a0842dc --- /dev/null +++ b/crates/download/src/download_ext.rs @@ -0,0 +1,68 @@ +use extism_pdk::*; +use moon_extension_common::download::download_from_url; +use moon_pdk::{ + anyhow, args::*, extension::*, host_log, plugin_err, virtual_path, HostLogInput, HostLogTarget, + VirtualPath, +}; + +#[host_fn] +extern "ExtismHost" { + fn host_log(input: Json); + fn to_virtual_path(path: String) -> String; +} + +#[derive(Args)] +pub struct DownloadExtensionArgs { + #[arg(long, short = 'u', required = true)] + pub url: String, + + #[arg(long, short = 'd')] + pub dest: Option, + + #[arg(long)] + pub name: Option, +} + +#[plugin_fn] +pub fn execute_extension(Json(input): Json) -> FnResult<()> { + let args = parse_args::(&input.args)?; + + if !args.url.starts_with("http") { + return Err(plugin_err!("A valid URL is required for downloading.")); + } + + // Determine destination directory + debug!("Determining destination directory"); + + let dest_dir = virtual_path!( + buf, + input + .context + .get_absolute_path(args.dest.as_deref().unwrap_or_default()) + ); + + if dest_dir.exists() && dest_dir.is_file() { + return Err(plugin_err!( + "Destination {} must be a directory, found a file.", + dest_dir.real_path().display(), + )); + } + + debug!( + "Destination {} will be used", + dest_dir.real_path().display(), + ); + + // Attempt to download the file + host_log!(stdout, "Downloading {}", args.url); + + let dest_file = download_from_url(&args.url, &dest_dir, args.name.as_deref())?; + + host_log!( + stdout, + "Downloaded to {}", + dest_file.real_path().display() + ); + + Ok(()) +} diff --git a/crates/download/src/lib.rs b/crates/download/src/lib.rs index a0842dc..2595763 100644 --- a/crates/download/src/lib.rs +++ b/crates/download/src/lib.rs @@ -1,68 +1,5 @@ -use extism_pdk::*; -use moon_extension_common::download::download_from_url; -use moon_pdk::{ - anyhow, args::*, extension::*, host_log, plugin_err, virtual_path, HostLogInput, HostLogTarget, - VirtualPath, -}; +#[cfg(feature = "wasm")] +mod download_ext; -#[host_fn] -extern "ExtismHost" { - fn host_log(input: Json); - fn to_virtual_path(path: String) -> String; -} - -#[derive(Args)] -pub struct DownloadExtensionArgs { - #[arg(long, short = 'u', required = true)] - pub url: String, - - #[arg(long, short = 'd')] - pub dest: Option, - - #[arg(long)] - pub name: Option, -} - -#[plugin_fn] -pub fn execute_extension(Json(input): Json) -> FnResult<()> { - let args = parse_args::(&input.args)?; - - if !args.url.starts_with("http") { - return Err(plugin_err!("A valid URL is required for downloading.")); - } - - // Determine destination directory - debug!("Determining destination directory"); - - let dest_dir = virtual_path!( - buf, - input - .context - .get_absolute_path(args.dest.as_deref().unwrap_or_default()) - ); - - if dest_dir.exists() && dest_dir.is_file() { - return Err(plugin_err!( - "Destination {} must be a directory, found a file.", - dest_dir.real_path().display(), - )); - } - - debug!( - "Destination {} will be used", - dest_dir.real_path().display(), - ); - - // Attempt to download the file - host_log!(stdout, "Downloading {}", args.url); - - let dest_file = download_from_url(&args.url, &dest_dir, args.name.as_deref())?; - - host_log!( - stdout, - "Downloaded to {}", - dest_file.real_path().display() - ); - - Ok(()) -} +#[cfg(feature = "wasm")] +pub use download_ext::*; diff --git a/crates/unpack/Cargo.toml b/crates/unpack/Cargo.toml index bc93b9f..29486ce 100644 --- a/crates/unpack/Cargo.toml +++ b/crates/unpack/Cargo.toml @@ -20,3 +20,7 @@ starbase_archive = { version = "0.2.5", default-features = false, features = [ [dev-dependencies] moon_pdk_test_utils = { workspace = true } starbase_sandbox = { workspace = true } + +[features] +default = ["wasm"] +wasm = [] diff --git a/crates/unpack/src/lib.rs b/crates/unpack/src/lib.rs index f2ac374..e8b7e0c 100644 --- a/crates/unpack/src/lib.rs +++ b/crates/unpack/src/lib.rs @@ -1,119 +1,5 @@ -use extism_pdk::*; -use moon_extension_common::download::download_from_url; -use moon_pdk::{ - anyhow, args::*, extension::*, host_log, plugin_err, virtual_path, HostLogInput, HostLogTarget, - VirtualPath, -}; -use starbase_archive::Archiver; -use std::fs; +#[cfg(feature = "wasm")] +mod unpack_ext; -#[host_fn] -extern "ExtismHost" { - fn host_log(input: Json); - fn to_virtual_path(path: String) -> String; -} - -#[derive(Args)] -pub struct UnpackExtensionArgs { - #[arg(long, short = 's', required = true)] - pub src: String, - - #[arg(long, short = 'd')] - pub dest: Option, - - #[arg(long)] - pub prefix: Option, -} - -#[plugin_fn] -pub fn execute_extension(Json(input): Json) -> FnResult<()> { - let args = parse_args::(&input.args)?; - - // Determine the correct input. If the input is a URL, attempt to download - // the file, otherwise use the file directly (if within our whitelist). - let src_file = if args.src.starts_with("http") { - debug!("Received a URL as the input source"); - - download_from_url(&args.src, virtual_path!("/moon/temp"), None)? - } else { - debug!( - "Converting source {} to an absolute virtual path", - args.src - ); - - virtual_path!(buf, input.context.get_absolute_path(args.src)) - }; - - if !src_file - .extension() - .is_some_and(|ext| ext == "tar" || ext == "tgz" || ext == "gz" || ext == "zip") - { - return Err(plugin_err!( - "Invalid source, only .tar, .tar.gz, and .zip archives are supported." - )); - } - - if !src_file.exists() || !src_file.is_file() { - return Err(plugin_err!( - "Source {} must be a valid file.", - src_file.real_path().display(), - )); - } - - host_log!( - stdout, - "Opening archive {}", - src_file.real_path().display() - ); - - // Convert the provided output into a virtual file path. - let dest_dir = virtual_path!( - buf, - input - .context - .get_absolute_path(args.dest.as_deref().unwrap_or_default()) - ); - - if dest_dir.exists() && dest_dir.is_file() { - return Err(plugin_err!( - "Destination {} must be a directory, found a file.", - dest_dir.real_path().display(), - )); - } - - fs::create_dir_all(&dest_dir)?; - - host_log!( - stdout, - "Unpacking archive to {}", - dest_dir.real_path().display() - ); - - // Attempt to unpack the archive! - let mut archive = Archiver::new(&dest_dir, &src_file); - - // Diff against all files in the output dir - archive.add_source_glob("**/*"); - - // Remove the prefix from unpacked files - if let Some(prefix) = &args.prefix { - archive.set_prefix(prefix); - } - - // Unpack the files - if let Err(error) = archive.unpack_from_ext() { - let mut message = error.to_string(); - - // Miette hides the real error - if let Some(source) = error.source() { - message.push(' '); - message.push_str(&source.to_string()); - } - - return Err(plugin_err!("{message}")); - }; - - host_log!(stdout, "Unpacked archive!"); - - Ok(()) -} +#[cfg(feature = "wasm")] +pub use unpack_ext::*; diff --git a/crates/unpack/src/unpack_ext.rs b/crates/unpack/src/unpack_ext.rs new file mode 100644 index 0000000..f2ac374 --- /dev/null +++ b/crates/unpack/src/unpack_ext.rs @@ -0,0 +1,119 @@ +use extism_pdk::*; +use moon_extension_common::download::download_from_url; +use moon_pdk::{ + anyhow, args::*, extension::*, host_log, plugin_err, virtual_path, HostLogInput, HostLogTarget, + VirtualPath, +}; +use starbase_archive::Archiver; +use std::fs; + +#[host_fn] +extern "ExtismHost" { + fn host_log(input: Json); + fn to_virtual_path(path: String) -> String; +} + +#[derive(Args)] +pub struct UnpackExtensionArgs { + #[arg(long, short = 's', required = true)] + pub src: String, + + #[arg(long, short = 'd')] + pub dest: Option, + + #[arg(long)] + pub prefix: Option, +} + +#[plugin_fn] +pub fn execute_extension(Json(input): Json) -> FnResult<()> { + let args = parse_args::(&input.args)?; + + // Determine the correct input. If the input is a URL, attempt to download + // the file, otherwise use the file directly (if within our whitelist). + let src_file = if args.src.starts_with("http") { + debug!("Received a URL as the input source"); + + download_from_url(&args.src, virtual_path!("/moon/temp"), None)? + } else { + debug!( + "Converting source {} to an absolute virtual path", + args.src + ); + + virtual_path!(buf, input.context.get_absolute_path(args.src)) + }; + + if !src_file + .extension() + .is_some_and(|ext| ext == "tar" || ext == "tgz" || ext == "gz" || ext == "zip") + { + return Err(plugin_err!( + "Invalid source, only .tar, .tar.gz, and .zip archives are supported." + )); + } + + if !src_file.exists() || !src_file.is_file() { + return Err(plugin_err!( + "Source {} must be a valid file.", + src_file.real_path().display(), + )); + } + + host_log!( + stdout, + "Opening archive {}", + src_file.real_path().display() + ); + + // Convert the provided output into a virtual file path. + let dest_dir = virtual_path!( + buf, + input + .context + .get_absolute_path(args.dest.as_deref().unwrap_or_default()) + ); + + if dest_dir.exists() && dest_dir.is_file() { + return Err(plugin_err!( + "Destination {} must be a directory, found a file.", + dest_dir.real_path().display(), + )); + } + + fs::create_dir_all(&dest_dir)?; + + host_log!( + stdout, + "Unpacking archive to {}", + dest_dir.real_path().display() + ); + + // Attempt to unpack the archive! + let mut archive = Archiver::new(&dest_dir, &src_file); + + // Diff against all files in the output dir + archive.add_source_glob("**/*"); + + // Remove the prefix from unpacked files + if let Some(prefix) = &args.prefix { + archive.set_prefix(prefix); + } + + // Unpack the files + if let Err(error) = archive.unpack_from_ext() { + let mut message = error.to_string(); + + // Miette hides the real error + if let Some(source) = error.source() { + message.push(' '); + message.push_str(&source.to_string()); + } + + return Err(plugin_err!("{message}")); + }; + + host_log!(stdout, "Unpacked archive!"); + + Ok(()) +}