Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add package commands #4287

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ version = "4.2.3"
[workspace.dependencies]
memoffset = "0.9.0"
wasmer-toml = "0.9.2"
webc = { version = "5.6.0", default-features = false, features = ["package"] }
webc = { version = "5.8.0", default-features = false, features = ["package"] }

[build-dependencies]
test-generator = { path = "tests/lib/test-generator" }
Expand Down
60 changes: 10 additions & 50 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ once_cell = "1.17.1"
indicatif = "0.17.5"
opener = "0.6.1"
hyper = { version = "0.14.27", features = ["server"] }
http = "0.2.9"
futures = "0.3.29"

# NOTE: Must use different features for clap because the "color" feature does not
# work on wasi due to the anstream dependency not compiling.
Expand Down Expand Up @@ -155,71 +157,29 @@ unix_mode = "0.1.3"
[features]
# Don't add the compiler features in default, please add them on the Makefile
# since we might want to autoconfigure them depending on the availability on the host.
default = [
"sys",
"wat",
"wast",
"compiler",
"wasmer-artifact-create",
"static-artifact-create",
]
default = ["sys", "wat", "wast", "compiler", "wasmer-artifact-create", "static-artifact-create"]
backend = []
coredump = ["wasm-coredump-builder"]
sys = ["compiler", "wasmer-vm"]
jsc = ["backend", "wasmer/jsc", "wasmer/std"]
wast = ["wasmer-wast"]
host-net = ["virtual-net/host-net"]
wat = ["wasmer/wat"]
compiler = [
"backend",
"wasmer/compiler",
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
]
wasmer-artifact-create = [
"compiler",
"wasmer/wasmer-artifact-load",
"wasmer/wasmer-artifact-create",
"wasmer-compiler/wasmer-artifact-load",
"wasmer-compiler/wasmer-artifact-create",
"wasmer-object",
]
static-artifact-create = [
"compiler",
"wasmer/static-artifact-load",
"wasmer/static-artifact-create",
"wasmer-compiler/static-artifact-load",
"wasmer-compiler/static-artifact-create",
"wasmer-object",
]
wasmer-artifact-load = [
"compiler",
"wasmer/wasmer-artifact-load",
"wasmer-compiler/wasmer-artifact-load",
]
static-artifact-load = [
"compiler",
"wasmer/static-artifact-load",
"wasmer-compiler/static-artifact-load",
]
compiler = ["backend", "wasmer/compiler", "wasmer-compiler/translator", "wasmer-compiler/compiler"]
wasmer-artifact-create = ["compiler", "wasmer/wasmer-artifact-load", "wasmer/wasmer-artifact-create", "wasmer-compiler/wasmer-artifact-load", "wasmer-compiler/wasmer-artifact-create", "wasmer-object"]
static-artifact-create = ["compiler", "wasmer/static-artifact-load", "wasmer/static-artifact-create", "wasmer-compiler/static-artifact-load", "wasmer-compiler/static-artifact-create", "wasmer-object"]
wasmer-artifact-load = ["compiler", "wasmer/wasmer-artifact-load", "wasmer-compiler/wasmer-artifact-load"]
static-artifact-load = ["compiler", "wasmer/static-artifact-load", "wasmer-compiler/static-artifact-load"]
experimental-io-devices = ["wasmer-wasix-experimental-io-devices"]
singlepass = ["wasmer-compiler-singlepass", "compiler"]
cranelift = ["wasmer-compiler-cranelift", "compiler"]
llvm = ["wasmer-compiler-llvm", "compiler"]
disable-all-logging = [
"wasmer-wasix/disable-all-logging",
"log/release_max_level_off",
]
disable-all-logging = ["wasmer-wasix/disable-all-logging", "log/release_max_level_off"]
headless = []
headless-minimal = ["headless", "disable-all-logging"]

# Optional
enable-serde = [
"wasmer/enable-serde",
"wasmer-vm/enable-serde",
"wasmer-compiler/enable-serde",
"wasmer-wasix/enable-serde",
]
enable-serde = ["wasmer/enable-serde", "wasmer-vm/enable-serde", "wasmer-compiler/enable-serde", "wasmer-wasix/enable-serde"]

[dev-dependencies]
assert_cmd = "2.0.11"
Expand Down
17 changes: 15 additions & 2 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::commands::CreateExe;
#[cfg(feature = "wast")]
use crate::commands::Wast;
use crate::commands::{
Add, Cache, Config, Init, Inspect, Login, Publish, Run, SelfUpdate, Validate, Whoami,
Add, Cache, Config, Init, Inspect, Login, Package, Publish, Run, SelfUpdate, Validate, Whoami,
};
#[cfg(feature = "static-artifact-create")]
use crate::commands::{CreateObj, GenCHeader};
Expand Down Expand Up @@ -108,6 +108,13 @@ impl Args {
Some(Cmd::Init(init)) => init.execute(),
Some(Cmd::Login(login)) => login.execute(),
Some(Cmd::Publish(publish)) => publish.execute(),
Some(Cmd::Package(cmd)) => match cmd {
Package::Download(cmd) => cmd.execute(),
Package::Build(cmd) => cmd.execute(),
},
Some(Cmd::Container(cmd)) => match cmd {
crate::commands::Container::Unpack(cmd) => cmd.execute(),
},
/*
Some(Cmd::Connect(connect)) => connect.execute(),
*/
Expand Down Expand Up @@ -258,7 +265,13 @@ enum Cmd {
#[clap(alias = "run-unstable")]
Run(Run),

// DEPLOY commands
#[clap(subcommand)]
Package(crate::commands::Package),

#[clap(subcommand)]
Container(crate::commands::Container),

// Edge commands
/// Deploy apps to the Wasmer Edge.
Deploy(wasmer_deploy_cli::cmd::deploy::CmdDeploy),

Expand Down
12 changes: 12 additions & 0 deletions lib/cli/src/commands/container/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod unpack;

pub use unpack::PackageUnpack;

/// Container related commands. (inspecting, unpacking, ...)
#[derive(clap::Subcommand, Debug)]
// Allowing missing_docs because the comment would override the doc comment on
// the command struct.
#[allow(missing_docs)]
pub enum Container {
Unpack(PackageUnpack),
}
90 changes: 90 additions & 0 deletions lib/cli/src/commands/container/unpack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::path::PathBuf;

use anyhow::Context;

/// Extract contents of a container 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,

/// Path to the package.
package_path: PathBuf,
}

impl PackageUnpack {
pub(crate) fn execute(&self) -> Result<(), anyhow::Error> {
eprintln!("Unpacking...");

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())?;

eprintln!("Extracted package contents to '{}'", self.out_dir.display());

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,
};

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::<Vec<_>>();
items.sort();
assert_eq!(
items,
vec![
"atom".to_string(),
"manifest.json".to_string(),
"metadata".to_string(),
]
);
}
}
11 changes: 7 additions & 4 deletions lib/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod cache;
#[cfg(feature = "compiler")]
mod compile;
mod config;
mod container;
#[cfg(any(feature = "static-artifact-create", feature = "wasmer-artifact-create"))]
mod create_exe;
#[cfg(feature = "static-artifact-create")]
Expand All @@ -15,6 +16,7 @@ mod gen_c_header;
mod init;
mod inspect;
mod login;
mod package;
mod publish;
mod run;
mod self_update;
Expand All @@ -23,6 +25,11 @@ mod validate;
mod wast;
mod whoami;

pub use self::{
add::*, cache::*, config::*, container::*, init::*, inspect::*, login::*, package::*,
publish::*, run::Run, self_update::*, validate::*, whoami::*,
};

#[cfg(target_os = "linux")]
pub use binfmt::*;
#[cfg(feature = "compiler")]
Expand All @@ -31,9 +38,5 @@ pub use compile::*;
pub use create_exe::*;
#[cfg(feature = "wast")]
pub use wast::*;
pub use {
add::*, cache::*, config::*, init::*, inspect::*, login::*, publish::*, run::Run,
self_update::*, validate::*, whoami::*,
};
#[cfg(feature = "static-artifact-create")]
pub use {create_obj::*, gen_c_header::*};
Loading
Loading