From 4568d95ec279e3723c2296cde54e7ab6d6371fed Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Thu, 8 Aug 2024 12:46:43 +0200 Subject: [PATCH] refactor(cli): Rename "container unpack" command to "package unpack" More sensible naming. The old command is kept for now , returning an error message that it was renamed. --- lib/cli/src/commands/container/unpack.rs | 115 +--------------------- lib/cli/src/commands/mod.rs | 1 + lib/cli/src/commands/package/mod.rs | 2 + lib/cli/src/commands/package/unpack.rs | 118 +++++++++++++++++++++++ 4 files changed, 124 insertions(+), 112 deletions(-) create mode 100644 lib/cli/src/commands/package/unpack.rs diff --git a/lib/cli/src/commands/container/unpack.rs b/lib/cli/src/commands/container/unpack.rs index 0e851c68203..d1b7113cf5e 100644 --- a/lib/cli/src/commands/container/unpack.rs +++ b/lib/cli/src/commands/container/unpack.rs @@ -1,118 +1,9 @@ -use std::path::PathBuf; - -use anyhow::Context; -use dialoguer::console::{style, Emoji}; -use indicatif::ProgressBar; - -/// Extract contents of a container to a directory. +/// RENAMED: the 'container unpack' command has been renamed to 'package unpack'! #[derive(clap::Parser, Debug)] -pub struct PackageUnpack { - /// The output directory. - #[clap(short = 'o', long)] - out_dir: PathBuf, - - /// Overwrite existing directories/files. - #[clap(long)] - overwrite: bool, - - /// Run the unpack command without any output - #[clap(long)] - pub quiet: bool, - - /// Path to the package. - package_path: PathBuf, -} - -static PACKAGE_EMOJI: Emoji<'_, '_> = Emoji("📦 ", ""); -static EXTRACTED_TO_EMOJI: Emoji<'_, '_> = Emoji("📂 ", ""); +pub struct PackageUnpack {} impl PackageUnpack { pub(crate) fn execute(&self) -> Result<(), anyhow::Error> { - // Setup the progress bar - let pb = if self.quiet { - ProgressBar::hidden() - } else { - ProgressBar::new_spinner() - }; - - pb.println(format!( - "{} {}Unpacking...", - style("[1/2]").bold().dim(), - PACKAGE_EMOJI - )); - - let pkg = webc::compat::Container::from_disk(&self.package_path).with_context(|| { - format!( - "could not open package at '{}'", - self.package_path.display() - ) - })?; - - let outdir = &self.out_dir; - 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())?; - - pb.println(format!( - "{} {}Extracted package contents to '{}'", - style("[2/2]").bold().dim(), - EXTRACTED_TO_EMOJI, - self.out_dir.display() - )); - - pb.finish(); - - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Download a package from the dev registry. - #[test] - fn test_cmd_package_extract() { - let dir = tempfile::tempdir().unwrap(); - - let package_path = std::env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap() - .parent().unwrap() - .parent().unwrap() - .join("tests/integration/cli/tests/webc/hello-0.1.0-665d2ddc-80e6-4845-85d3-4587b1693bb7.webc"); - - assert!(package_path.is_file()); - - let cmd = PackageUnpack { - out_dir: dir.path().to_owned(), - overwrite: false, - package_path, - quiet: true, - }; - - cmd.execute().unwrap(); - - let mut items = std::fs::read_dir(dir.path()) - .unwrap() - .map(|x| { - x.unwrap() - .path() - .file_name() - .unwrap() - .to_str() - .unwrap() - .to_string() - }) - .collect::>(); - items.sort(); - assert_eq!( - items, - vec![ - "atom".to_string(), - "manifest.json".to_string(), - "metadata".to_string(), - ] - ); + anyhow::bail!("This command was renamed: use 'wasmer package unpack instead'"); } } diff --git a/lib/cli/src/commands/mod.rs b/lib/cli/src/commands/mod.rs index 07b0706b7e1..5b56d25b081 100644 --- a/lib/cli/src/commands/mod.rs +++ b/lib/cli/src/commands/mod.rs @@ -193,6 +193,7 @@ impl WasmerCmd { Package::Tag(cmd) => cmd.run(), Package::Push(cmd) => cmd.run(), Package::Publish(cmd) => cmd.run().map(|_| ()), + Package::Unpack(cmd) => cmd.execute(), }, Some(Cmd::Container(cmd)) => match cmd { crate::commands::Container::Unpack(cmd) => cmd.execute(), diff --git a/lib/cli/src/commands/package/mod.rs b/lib/cli/src/commands/package/mod.rs index 1126e34c918..508ef304408 100644 --- a/lib/cli/src/commands/package/mod.rs +++ b/lib/cli/src/commands/package/mod.rs @@ -4,6 +4,7 @@ mod download; pub mod publish; mod push; mod tag; +mod unpack; pub use build::PackageBuild; pub use common::wait::PublishWait; @@ -20,4 +21,5 @@ pub enum Package { Tag(tag::PackageTag), Push(push::PackagePush), Publish(publish::PackagePublish), + Unpack(unpack::PackageUnpack), } diff --git a/lib/cli/src/commands/package/unpack.rs b/lib/cli/src/commands/package/unpack.rs new file mode 100644 index 00000000000..0f4784886e8 --- /dev/null +++ b/lib/cli/src/commands/package/unpack.rs @@ -0,0 +1,118 @@ +use std::path::PathBuf; + +use anyhow::Context; +use dialoguer::console::{style, Emoji}; +use indicatif::ProgressBar; + +/// Extract contents of a webc image to a directory. +#[derive(clap::Parser, Debug)] +pub struct PackageUnpack { + /// The output directory. + #[clap(short = 'o', long)] + out_dir: PathBuf, + + /// Overwrite existing directories/files. + #[clap(long)] + overwrite: bool, + + /// Run the unpack command without any output + #[clap(long)] + pub quiet: bool, + + /// Path to the package. + package_path: PathBuf, +} + +static PACKAGE_EMOJI: Emoji<'_, '_> = Emoji("📦 ", ""); +static EXTRACTED_TO_EMOJI: Emoji<'_, '_> = Emoji("📂 ", ""); + +impl PackageUnpack { + pub(crate) fn execute(&self) -> Result<(), anyhow::Error> { + // Setup the progress bar + let pb = if self.quiet { + ProgressBar::hidden() + } else { + ProgressBar::new_spinner() + }; + + pb.println(format!( + "{} {}Unpacking...", + style("[1/2]").bold().dim(), + PACKAGE_EMOJI + )); + + let pkg = webc::compat::Container::from_disk(&self.package_path).with_context(|| { + format!( + "could not open package at '{}'", + self.package_path.display() + ) + })?; + + let outdir = &self.out_dir; + 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())?; + + pb.println(format!( + "{} {}Extracted package contents to '{}'", + style("[2/2]").bold().dim(), + EXTRACTED_TO_EMOJI, + self.out_dir.display() + )); + + pb.finish(); + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Download a package from the dev registry. + #[test] + fn test_cmd_package_extract() { + let dir = tempfile::tempdir().unwrap(); + + let package_path = std::env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap() + .parent().unwrap() + .parent().unwrap() + .join("tests/integration/cli/tests/webc/hello-0.1.0-665d2ddc-80e6-4845-85d3-4587b1693bb7.webc"); + + assert!(package_path.is_file()); + + let cmd = PackageUnpack { + out_dir: dir.path().to_owned(), + overwrite: false, + package_path, + quiet: true, + }; + + cmd.execute().unwrap(); + + let mut items = std::fs::read_dir(dir.path()) + .unwrap() + .map(|x| { + x.unwrap() + .path() + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_string() + }) + .collect::>(); + items.sort(); + assert_eq!( + items, + vec![ + "atom".to_string(), + "manifest.json".to_string(), + "metadata".to_string(), + ] + ); + } +}