From bf40de3e6e32c31607e441f608db06fdb826a2e7 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Sun, 15 Sep 2024 11:08:13 +0200 Subject: [PATCH] feat(cli): Allow "package unpack" to restore packages Add a new --format option to package unpack. * package: Try to restore a usable package dir with a wasmer.toml * webc: just unpack the webc --- lib/cli/Cargo.toml | 2 ++ lib/cli/src/commands/package/unpack.rs | 45 +++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index a71aa595e32..538735e6736 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -117,6 +117,8 @@ wasmer-compiler-cranelift = { version = "=4.3.7", path = "../compiler-cranelift" wasmer-compiler-singlepass = { version = "=4.3.7", path = "../compiler-singlepass", optional = true } wasmer-compiler-llvm = { version = "=4.3.7", path = "../compiler-llvm", optional = true } wasmer-emscripten = { version = "=4.3.7", path = "../emscripten" } +wasmer-package = { version = "=0.1.0", path = "../package" } + wasmer-vm = { version = "=4.3.7", path = "../vm", optional = true } wasmer-wasix = { path = "../wasix", version = "=0.27.0", features = [ "logging", diff --git a/lib/cli/src/commands/package/unpack.rs b/lib/cli/src/commands/package/unpack.rs index 0f4784886e8..23d53b90164 100644 --- a/lib/cli/src/commands/package/unpack.rs +++ b/lib/cli/src/commands/package/unpack.rs @@ -5,27 +5,53 @@ use dialoguer::console::{style, Emoji}; use indicatif::ProgressBar; /// Extract contents of a webc image to a directory. +/// +/// See --format flag for available output formats. #[derive(clap::Parser, Debug)] pub struct PackageUnpack { /// The output directory. #[clap(short = 'o', long)] - out_dir: PathBuf, + pub out_dir: PathBuf, /// Overwrite existing directories/files. #[clap(long)] - overwrite: bool, + pub overwrite: bool, /// Run the unpack command without any output #[clap(long)] pub quiet: bool, /// Path to the package. - package_path: PathBuf, + pub package_path: PathBuf, + + /// Output format. + /// + /// * package + /// Restore a package directory with a wasmer.toml + /// NOTE: this conversion is lossy, because webcs don't store the original + /// wasmer.toml and the full contents can not be restored. + /// + /// * webc + /// Directly unpack the webc contents. + /// - Volumes will be placed in subdirectories. + /// - atoms will be placed in the root directory + /// - the full webc manifest will be placed in a manifest.json file + #[clap(short, long, default_value = "package")] + pub format: Format, } static PACKAGE_EMOJI: Emoji<'_, '_> = Emoji("📦 ", ""); static EXTRACTED_TO_EMOJI: Emoji<'_, '_> = Emoji("📂 ", ""); +/// Webc unpack format. +#[derive(clap::ValueEnum, Clone, Debug)] +pub enum Format { + /// See [`PackageUnpack::format`] for details. + Package, + /// See [`PackageUnpack::format`] for details. + Webc, +} + impl PackageUnpack { pub(crate) fn execute(&self) -> Result<(), anyhow::Error> { // Setup the progress bar @@ -52,8 +78,16 @@ impl PackageUnpack { std::fs::create_dir_all(outdir) .with_context(|| format!("could not create output directory '{}'", outdir.display()))?; - pkg.unpack(outdir, self.overwrite) - .with_context(|| "could not extract package".to_string())?; + match self.format { + Format::Package => { + wasmer_package::convert::webc_to_package_dir(&pkg, outdir) + .with_context(|| "could not extract package")?; + } + Format::Webc => { + pkg.unpack(outdir, self.overwrite) + .with_context(|| "could not extract package".to_string())?; + } + } pb.println(format!( "{} {}Extracted package contents to '{}'", @@ -89,6 +123,7 @@ mod tests { overwrite: false, package_path, quiet: true, + format: Format::Webc, }; cmd.execute().unwrap();