Added RunWithoutFile to prepare downloading + GraphQL schema

This commit is contained in:
Felix Schütt
2022-10-04 12:10:31 +02:00
parent 6cd80f4937
commit 771e1e33a1
15 changed files with 1646 additions and 35 deletions

View File

@@ -10,9 +10,10 @@ use crate::commands::CreateExe;
use crate::commands::CreateObj;
#[cfg(feature = "wast")]
use crate::commands::Wast;
use crate::commands::{Cache, Config, Inspect, Run, SelfUpdate, Validate};
use crate::commands::{Cache, Config, Inspect, Run, RunWithoutFile, SelfUpdate, Validate};
use crate::error::PrettyError;
use anyhow::Result;
use clap::Parser;
/// The main function for the Wasmer CLI tool.
pub fn wasmer_main() {
@@ -26,7 +27,6 @@ pub fn wasmer_main() {
}
fn parse_cli_args() -> Result<(), anyhow::Error> {
let args = std::env::args().collect::<Vec<_>>();
let binpath = args.get(0).map(|s| s.as_ref()).unwrap_or("");
@@ -36,46 +36,76 @@ fn parse_cli_args() -> Result<(), anyhow::Error> {
return Run::from_binfmt_args().execute();
}
match (args.get(1).map(|s| s.as_str()), args.get(2).map(|s| s.as_str())) {
(None, _) |
(Some("help"), _) |
(Some("--help"), _) => return print_help(),
(Some("-vV"), _) |
(Some("version"), Some("--verbose")) |
(Some("--version"), Some("--verbose")) => return print_version(false),
match (
args.get(1).map(|s| s.as_str()),
args.get(2).map(|s| s.as_str()),
) {
(None, _) | (Some("help"), _) | (Some("--help"), _) => return print_help(),
(Some("-v"), _) |
(Some("version"), _) |
(Some("--version"), _) => return print_version(false),
(Some("cache"), Some(_)) |
(Some("compile"), Some(_)) |
(Some("config"), Some(_)) |
(Some("create-exe"), Some(_)) |
(Some("inspect"), Some(_)) |
(Some("self-update"), _) |
(Some("validate"), Some(_)) |
(Some("wast"), Some(_)) |
(Some("binfmt"), Some(_)) => {
println!("running {:?}", args.get(1));
return Ok(())
},
(Some("run"), Some(_)) |
(Some(_), _) => {
use clap::Parser;
// wasmer run file
// wasmer run [package]
(Some("-vV"), _)
| (Some("version"), Some("--verbose"))
| (Some("--version"), Some("--verbose")) => return print_version(false),
(Some("-v"), _) | (Some("-V"), _) | (Some("version"), _) | (Some("--version"), _) => {
return print_version(false)
}
(Some("cache"), _) => Cache::try_parse_from(args.iter())?.execute(),
(Some("compile"), _) => Compile::try_parse_from(args.iter())?.execute(),
(Some("config"), _) => Config::try_parse_from(args.iter())?.execute(),
(Some("create-exe"), _) => CreateExe::try_parse_from(args.iter())?.execute(),
(Some("inspect"), _) => Inspect::try_parse_from(args.iter())?.execute(),
(Some("self-update"), _) => SelfUpdate::try_parse_from(args.iter())?.execute(),
(Some("validate"), _) => Validate::try_parse_from(args.iter())?.execute(),
(Some("wast"), _) => Wast::try_parse_from(args.iter())?.execute(),
#[cfg(feature = "binfmt")]
(Some("binfmt"), _) => Binfmt::try_parse_from(args.iter())?.execute(),
(Some("run"), Some(package)) | (Some(package), _) => {
if let Ok(run) = Run::try_parse() {
return run.execute();
} else if let Ok((package, version)) = split_version(package) {
if let Ok(o) = wasmer_registry::get_package_local(
&package,
version.as_ref().map(|s| s.as_str()),
) {
// Try finding the local package
let mut args_without_package = args.clone();
args_without_package.remove(1);
return RunWithoutFile::try_parse_from(args_without_package.iter())?
.into_run_args(o)
.execute();
} else if let Ok(o) =
wasmer_registry::install_package(&package, version.as_ref().map(|s| s.as_str()))
{
// Try auto-installing the remote package
let mut args_without_package = args.clone();
args_without_package.remove(1);
return RunWithoutFile::try_parse_from(args_without_package.iter())?
.into_run_args(o)
.execute();
} else {
// Failed to find the remote package
//
// TODO: print list of similar packages
return print_help();
}
} else {
return print_help();
}
},
}
}
}
fn print_help() -> Result<(), anyhow::Error>{
fn split_version(s: &str) -> Result<(String, Option<String>), anyhow::Error> {
let package_version = s.split("@").collect::<Vec<_>>();
match package_version.as_slice() {
&[p, v] => Ok((p.trim().to_string(), Some(v.trim().to_string()))),
&[p] => Ok((p.trim().to_string(), None)),
_ => Err(anyhow!("Invalid package / version: {s:?}")),
}
}
fn print_help() -> Result<(), anyhow::Error> {
println!("help");
Ok(())
}
@@ -83,4 +113,4 @@ fn print_help() -> Result<(), anyhow::Error>{
fn print_version(_: bool) -> Result<(), anyhow::Error> {
println!("version");
Ok(())
}
}

View File

@@ -21,6 +21,83 @@ mod wasi;
#[cfg(feature = "wasi")]
use wasi::Wasi;
/// Same as `wasmer run`, but without the required `path` argument (injected previously)
#[derive(Debug, Parser, Clone, Default)]
pub struct RunWithoutFile {
/// Disable the cache
#[cfg(feature = "cache")]
#[clap(long = "disable-cache")]
disable_cache: bool,
/// Invoke a specified function
#[clap(long = "invoke", short = 'i')]
invoke: Option<String>,
/// The command name is a string that will override the first argument passed
/// to the wasm program. This is used in wapm to provide nicer output in
/// help commands and error messages of the running wasm program
#[clap(long = "command-name", hide = true)]
command_name: Option<String>,
/// A prehashed string, used to speed up start times by avoiding hashing the
/// wasm module. If the specified hash is not found, Wasmer will hash the module
/// as if no `cache-key` argument was passed.
#[cfg(feature = "cache")]
#[clap(long = "cache-key", hide = true)]
cache_key: Option<String>,
#[clap(flatten)]
store: StoreOptions,
// TODO: refactor WASI structure to allow shared options with Emscripten
#[cfg(feature = "wasi")]
#[clap(flatten)]
wasi: Wasi,
/// Enable non-standard experimental IO devices
#[cfg(feature = "io-devices")]
#[clap(long = "enable-io-devices")]
enable_experimental_io_devices: bool,
/// Enable debug output
#[cfg(feature = "debug")]
#[clap(long = "debug", short = 'd')]
debug: bool,
#[cfg(feature = "debug")]
#[clap(short, long, parse(from_occurrences))]
verbose: u8,
/// Application arguments
#[clap(value_name = "ARGS")]
args: Vec<String>,
}
impl RunWithoutFile {
/// Given a local path, returns the `Run` command (overriding the `--path` argument).
pub fn into_run_args(self, pathbuf: PathBuf) -> Run {
Run {
#[cfg(feature = "cache")]
disable_cache: self.disable_cache,
path: pathbuf,
invoke: self.invoke,
command_name: self.command_name,
#[cfg(feature = "cache")]
cache_key: self.cache_key,
store: self.store,
#[cfg(feature = "wasi")]
wasi: self.wasi,
#[cfg(feature = "io-devices")]
enable_experimental_io_devices: self.enable_experimental_io_devices,
#[cfg(feature = "debug")]
debug: self.debug,
#[cfg(feature = "debug")]
verbose: self.verbose,
args: self.args,
}
}
}
#[derive(Debug, Parser, Clone, Default)]
/// The options for the `wasmer run` subcommand
pub struct Run {