Skip to content

Commit

Permalink
feat(cli): Allow "package unpack" to restore packages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
theduke committed Sep 16, 2024
1 parent 372a8e2 commit 7fc43be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 40 additions & 5 deletions lib/cli/src/commands/package/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 '{}'",
Expand Down Expand Up @@ -89,6 +123,7 @@ mod tests {
overwrite: false,
package_path,
quiet: true,
format: Format::Webc,
};

cmd.execute().unwrap();
Expand Down

0 comments on commit 7fc43be

Please sign in to comment.