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(())
}
}