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

Fix error message when trying to run a nonexistent path #3407

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
07f5aba
Improve error message when package / path wasn't found
fschutt Dec 5, 2022
c29ee26
Fixed error that led to not being able to run wasmerio/wasmer
fschutt Dec 5, 2022
ea26f95
Fix merge errors
fschutt Dec 6, 2022
a2e9496
Move SplitVersion parsing to separate file
fschutt Dec 6, 2022
0888e5a
Remove split_version from registry
fschutt Dec 6, 2022
6555e5e
Finish moving SplitVersion to own file
fschutt Dec 6, 2022
c357571
Refactor SplitVersion and add unit tests for parsing
fschutt Dec 6, 2022
7de0210
Adjust unit test and handle parsing in clap instead
fschutt Dec 6, 2022
c15adf1
Temporarily remove all the run() code and refactor it to compile
fschutt Dec 6, 2022
b4b84be
Get wasmer to compile again
fschutt Dec 6, 2022
6d3aeb4
Fix CLI parsing of wasmer run command
fschutt Dec 6, 2022
26f4b43
Add code to lookup package information from SplitVersion
fschutt Dec 7, 2022
92cefac
Get wasmer run working for all command types
fschutt Dec 7, 2022
4ee0c97
Fix running a package directory instead of a file
fschutt Dec 7, 2022
8ca5a5b
Fix SplitVersion::parse test
fschutt Dec 7, 2022
8892a61
Fix run-url test
fschutt Dec 7, 2022
de7118d
Fix make lint
fschutt Dec 7, 2022
627285d
Fix registry not being recongnized on Linux
fschutt Dec 7, 2022
6cb47c1
Linux: rework from_binfmt_args_fallible
fschutt Dec 7, 2022
2a4e91a
Disable some tests on Windows (already tested as part of integration)
fschutt Dec 7, 2022
71ff457
Fix make lint
fschutt Dec 7, 2022
9af38d1
Fix test_split_version
fschutt Dec 7, 2022
8cb9fca
Add fetching for webc files back, rework SplitVewrsion -> PackageSource
fschutt Dec 8, 2022
9751fbc
Fix test_split_version
fschutt Dec 8, 2022
f633e61
Debug why redirects aren't working for application/webc
fschutt Dec 8, 2022
ab6f6b9
Add functions for tar.gz fetching back
fschutt Dec 8, 2022
50c75d4
Add caching for installed commands
fschutt Dec 8, 2022
7a48f96
Work on error messages for non-existent file paths
fschutt Dec 8, 2022
7c3b0eb
Update spinner when installing packages
fschutt Dec 8, 2022
a7ff5db
Add support for command immediately executing commands
fschutt Dec 8, 2022
cb312bc
Fix caching for local packages
fschutt Dec 8, 2022
4010316
Fix caching + fix make lint
fschutt Dec 8, 2022
886392f
Fix CLI integration test + add more tests
fschutt Dec 8, 2022
5d40796
split_version -> package_source
fschutt Dec 8, 2022
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ reqwest = { version = "^0.11", default-features = false, features = ["rustls-tls
serde = { version = "1.0.147", features = ["derive"] }
dirs = { version = "4.0" }
serde_json = { version = "1.0" }
serde_cbor = { version = "0.11.2" }
target-lexicon = { version = "0.12", features = ["std"] }
prettytable-rs = "0.9.0"
wapm-toml = "0.2.0"
Expand All @@ -73,6 +74,7 @@ webc = { version = "3.0.1", optional = true }
isatty = "0.1.9"
dialoguer = "0.10.2"
tldextract = "0.6.0"
semver = "1.0.14"

[build-dependencies]
chrono = { version = "^0.4", default-features = false, features = [ "std", "clock" ] }
Expand Down
211 changes: 3 additions & 208 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::commands::{
};
use crate::error::PrettyError;
use clap::{CommandFactory, ErrorKind, Parser};
use std::{fmt, str::FromStr};

#[derive(Parser, Debug)]
#[cfg_attr(
Expand All @@ -37,7 +36,7 @@ use std::{fmt, str::FromStr};
)
)]
/// The options for the wasmer Command Line Interface
enum WasmerCLIOptions {
pub(crate) enum WasmerCLIOptions {
/// List all locally installed packages
List(List),

Expand Down Expand Up @@ -154,7 +153,7 @@ enum WasmerCLIOptions {
impl WasmerCLIOptions {
fn execute(&self) -> Result<(), anyhow::Error> {
match self {
Self::Run(options) => options.execute(),
Self::Run(options) => options.resolve()?.execute(),
Self::SelfUpdate(options) => options.execute(),
Self::Cache(cache) => cache.execute(),
Self::Validate(validate) => validate.execute(),
Expand Down Expand Up @@ -245,216 +244,12 @@ fn wasmer_main_inner() -> Result<(), anyhow::Error> {

// Check if the file is a package name
if let WasmerCLIOptions::Run(r) = &options {
#[cfg(not(feature = "debug"))]
let debug = false;
#[cfg(feature = "debug")]
let debug = r.options.debug;
return crate::commands::try_run_package_or_file(&args, r, debug);
return r.resolve()?.execute();
}

options.execute()
}

#[derive(Debug, Clone, PartialEq, Default)]
pub(crate) struct SplitVersion {
pub(crate) original: String,
pub(crate) registry: Option<String>,
pub(crate) package: String,
pub(crate) version: Option<String>,
pub(crate) command: Option<String>,
}

impl fmt::Display for SplitVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let version = self.version.as_deref().unwrap_or("latest");
let command = self
.command
.as_ref()
.map(|s| format!(":{s}"))
.unwrap_or_default();
write!(f, "{}@{version}{command}", self.package)
}
}

#[test]
fn test_split_version() {
assert_eq!(
SplitVersion::parse("registry.wapm.io/graphql/python/python").unwrap(),
SplitVersion {
original: "registry.wapm.io/graphql/python/python".to_string(),
registry: Some("https://registry.wapm.io/graphql".to_string()),
package: "python/python".to_string(),
version: None,
command: None,
}
);
assert_eq!(
SplitVersion::parse("registry.wapm.io/python/python").unwrap(),
SplitVersion {
original: "registry.wapm.io/python/python".to_string(),
registry: Some("https://registry.wapm.io/graphql".to_string()),
package: "python/python".to_string(),
version: None,
command: None,
}
);
assert_eq!(
SplitVersion::parse("namespace/name@version:command").unwrap(),
SplitVersion {
original: "namespace/name@version:command".to_string(),
registry: None,
package: "namespace/name".to_string(),
version: Some("version".to_string()),
command: Some("command".to_string()),
}
);
assert_eq!(
SplitVersion::parse("namespace/name@version").unwrap(),
SplitVersion {
original: "namespace/name@version".to_string(),
registry: None,
package: "namespace/name".to_string(),
version: Some("version".to_string()),
command: None,
}
);
assert_eq!(
SplitVersion::parse("namespace/name").unwrap(),
SplitVersion {
original: "namespace/name".to_string(),
registry: None,
package: "namespace/name".to_string(),
version: None,
command: None,
}
);
assert_eq!(
SplitVersion::parse("registry.wapm.io/namespace/name").unwrap(),
SplitVersion {
original: "registry.wapm.io/namespace/name".to_string(),
registry: Some("https://registry.wapm.io/graphql".to_string()),
package: "namespace/name".to_string(),
version: None,
command: None,
}
);
assert_eq!(
format!("{}", SplitVersion::parse("namespace").unwrap_err()),
"Invalid package version: \"namespace\"".to_string(),
);
}

impl SplitVersion {
pub fn parse(s: &str) -> Result<SplitVersion, anyhow::Error> {
s.parse()
}
}

impl FromStr for SplitVersion {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let command = WasmerCLIOptions::command();
let mut prohibited_package_names = command.get_subcommands().map(|s| s.get_name());

let re1 = regex::Regex::new(r#"(.*)/(.*)@(.*):(.*)"#).unwrap();
let re2 = regex::Regex::new(r#"(.*)/(.*)@(.*)"#).unwrap();
let re3 = regex::Regex::new(r#"(.*)/(.*)"#).unwrap();
let re4 = regex::Regex::new(r#"(.*)/(.*):(.*)"#).unwrap();

let mut no_version = false;

let captures = if re1.is_match(s) {
re1.captures(s)
.map(|c| {
c.iter()
.flatten()
.map(|m| m.as_str().to_owned())
.collect::<Vec<_>>()
})
.unwrap_or_default()
} else if re2.is_match(s) {
re2.captures(s)
.map(|c| {
c.iter()
.flatten()
.map(|m| m.as_str().to_owned())
.collect::<Vec<_>>()
})
.unwrap_or_default()
} else if re4.is_match(s) {
no_version = true;
re4.captures(s)
.map(|c| {
c.iter()
.flatten()
.map(|m| m.as_str().to_owned())
.collect::<Vec<_>>()
})
.unwrap_or_default()
} else if re3.is_match(s) {
re3.captures(s)
.map(|c| {
c.iter()
.flatten()
.map(|m| m.as_str().to_owned())
.collect::<Vec<_>>()
})
.unwrap_or_default()
} else {
return Err(anyhow::anyhow!("Invalid package version: {s:?}"));
};

let mut namespace = match captures.get(1).cloned() {
Some(s) => s,
None => {
return Err(anyhow::anyhow!(
"Invalid package version: {s:?}: no namespace"
))
}
};

let name = match captures.get(2).cloned() {
Some(s) => s,
None => return Err(anyhow::anyhow!("Invalid package version: {s:?}: no name")),
};

let mut registry = None;
if namespace.contains('/') {
let (r, n) = namespace.rsplit_once('/').unwrap();
let mut real_registry = r.to_string();
if !real_registry.ends_with("graphql") {
real_registry = format!("{real_registry}/graphql");
}
if !real_registry.contains("://") {
real_registry = format!("https://{real_registry}");
}
registry = Some(real_registry);
namespace = n.to_string();
}

let sv = SplitVersion {
original: s.to_string(),
registry,
package: format!("{namespace}/{name}"),
version: if no_version {
None
} else {
captures.get(3).cloned()
},
command: captures.get(if no_version { 3 } else { 4 }).cloned(),
};

let svp = sv.package.clone();
anyhow::ensure!(
!prohibited_package_names.any(|s| s == sv.package.trim()),
"Invalid package name {svp:?}"
);

Ok(sv)
}
}

fn print_help(verbose: bool) -> Result<(), anyhow::Error> {
let mut cmd = WasmerCLIOptions::command();
if verbose {
Expand Down
11 changes: 7 additions & 4 deletions lib/cli/src/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::process::{Command, Stdio};

use crate::split_version::{ResolvedSplitVersion, SplitVersion};
use anyhow::{Context, Error};
use clap::Parser;
use wasmer_registry::{Bindings, PartialWapmConfig, ProgrammingLanguage};

use crate::cli::SplitVersion;

/// Add a WAPM package's bindings to your application.
#[derive(Debug, Parser)]
pub struct Add {
Expand Down Expand Up @@ -66,7 +65,11 @@ impl Add {
let language = self.target()?.language();

for pkg in &self.packages {
let bindings = lookup_bindings_for_package(registry, pkg, &language)
let resolved = match pkg.get_package_info(registry, false) {
Ok(o) => o,
Err(_) => continue,
};
let bindings = lookup_bindings_for_package(registry, &resolved, &language)
.with_context(|| format!("Unable to find bindings for {pkg}"))?;
bindings_to_add.push(bindings);
}
Expand Down Expand Up @@ -103,7 +106,7 @@ impl Add {

fn lookup_bindings_for_package(
registry: &str,
pkg: &SplitVersion,
pkg: &ResolvedSplitVersion,
language: &ProgrammingLanguage,
) -> Result<Bindings, Error> {
let all_bindings =
Expand Down
Loading