wasmer_cli/commands/package/
unpack.rs

1use std::path::PathBuf;
2
3use anyhow::Context;
4use dialoguer::console::{Emoji, style};
5use indicatif::ProgressBar;
6use wasmer_package::utils::from_disk;
7
8/// Extract contents of a webc image to a directory.
9///
10/// See --format flag for available output formats.
11#[derive(clap::Parser, Debug)]
12pub struct PackageUnpack {
13    /// The output directory.
14    #[clap(short = 'o', long)]
15    pub out_dir: PathBuf,
16
17    /// Overwrite existing directories/files.
18    #[clap(long)]
19    pub overwrite: bool,
20
21    /// Run the unpack command without any output
22    #[clap(long)]
23    pub quiet: bool,
24
25    /// Path to the package.
26    pub package_path: PathBuf,
27
28    /// Output format.
29    ///
30    /// * package
31    ///   Restore a package directory with a wasmer.toml
32    ///   NOTE: this conversion is lossy, because webcs don't store the original
33    ///   wasmer.toml and the full contents can not be restored.
34    ///
35    /// * webc
36    ///   Directly unpack the webc contents.
37    ///   - Volumes will be placed in subdirectories.
38    ///   - atoms will be placed in the root directory
39    ///   - the full webc manifest will be placed in a manifest.json file
40    #[clap(short, long, default_value = "package")]
41    pub format: Format,
42}
43
44static PACKAGE_EMOJI: Emoji<'_, '_> = Emoji("📦 ", "");
45static EXTRACTED_TO_EMOJI: Emoji<'_, '_> = Emoji("📂 ", "");
46
47/// Webc unpack format.
48#[derive(clap::ValueEnum, Clone, Debug)]
49pub enum Format {
50    /// See [`PackageUnpack::format`] for details.
51    Package,
52    /// See [`PackageUnpack::format`] for details.
53    Webc,
54}
55
56impl PackageUnpack {
57    pub(crate) fn execute(&self) -> Result<(), anyhow::Error> {
58        // Setup the progress bar
59        let pb = if self.quiet {
60            ProgressBar::hidden()
61        } else {
62            ProgressBar::new_spinner()
63        };
64
65        pb.println(format!(
66            "{} {}Unpacking...",
67            style("[1/2]").bold().dim(),
68            PACKAGE_EMOJI
69        ));
70
71        let pkg = from_disk(&self.package_path).with_context(|| {
72            format!(
73                "could not open package at '{}'",
74                self.package_path.display()
75            )
76        })?;
77
78        let outdir = &self.out_dir;
79        std::fs::create_dir_all(outdir)
80            .with_context(|| format!("could not create output directory '{}'", outdir.display()))?;
81
82        match self.format {
83            Format::Package => {
84                wasmer_package::convert::webc_to_package_dir(&pkg, outdir)
85                    .with_context(|| "could not extract package")?;
86            }
87            Format::Webc => {
88                pkg.unpack(outdir, self.overwrite)
89                    .with_context(|| "could not extract package".to_string())?;
90            }
91        }
92
93        pb.println(format!(
94            "{} {}Extracted package contents to '{}'",
95            style("[2/2]").bold().dim(),
96            EXTRACTED_TO_EMOJI,
97            self.out_dir.display()
98        ));
99
100        pb.finish();
101
102        Ok(())
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    /// Download a package from the dev registry.
111    #[test]
112    fn test_cmd_package_extract() {
113        let dir = tempfile::tempdir().unwrap();
114
115        let package_path = std::env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap()
116            .parent().unwrap()
117            .parent().unwrap()
118            .join("tests/integration/cli/tests/webc/hello-0.1.0-665d2ddc-80e6-4845-85d3-4587b1693bb7.webc");
119
120        assert!(package_path.is_file());
121
122        let cmd = PackageUnpack {
123            out_dir: dir.path().to_owned(),
124            overwrite: false,
125            package_path,
126            quiet: true,
127            format: Format::Webc,
128        };
129
130        cmd.execute().unwrap();
131
132        let mut items = std::fs::read_dir(dir.path())
133            .unwrap()
134            .map(|x| {
135                x.unwrap()
136                    .path()
137                    .file_name()
138                    .unwrap()
139                    .to_str()
140                    .unwrap()
141                    .to_string()
142            })
143            .collect::<Vec<_>>();
144        items.sort();
145        assert_eq!(
146            items,
147            vec![
148                "atom".to_string(),
149                "manifest.json".to_string(),
150                "metadata".to_string(),
151            ]
152        );
153    }
154}