Skip to content

Commit

Permalink
Merge #3339
Browse files Browse the repository at this point in the history
3339: Fixes for wasmer login / wasmer add r=fschutt a=fschutt

- wasmer add now accounts for python2 / python3
- wasmer login shows the correct TLD for `{url}/me`
- wasmer add doesn't panic anymore if none of `--pip`, `--yarn` or `-npm` is installed

Co-authored-by: Felix Schütt <[email protected]>
  • Loading branch information
bors[bot] and fschutt authored Nov 20, 2022
2 parents ee8c67e + b37c79e commit fef44ae
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 39 deletions.
34 changes: 33 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ nuke-dir = { version = "0.1.0", optional = true }
webc = { version = "3.0.1", optional = true }
isatty = "0.1.9"
dialoguer = "0.10.2"
tldextract = "0.6.0"

[build-dependencies]
chrono = { version = "^0.4", default-features = false, features = [ "std", "clock" ] }
Expand Down
67 changes: 50 additions & 17 deletions lib/cli/src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Add {

let bindings = self.lookup_bindings(&registry)?;

let mut cmd = self.target().command(&bindings);
let mut cmd = self.target()?.command(&bindings)?;

#[cfg(feature = "debug")]
log::debug!("Running {cmd:?}");
Expand All @@ -67,7 +67,7 @@ impl Add {
log::debug!("Querying WAPM for the bindings packages");

let mut bindings_to_add = Vec::new();
let language = self.target().language();
let language = self.target()?.language();

for pkg in &self.packages {
let bindings = lookup_bindings_for_package(registry, pkg, &language)
Expand All @@ -90,14 +90,17 @@ impl Add {
}
}

fn target(&self) -> Target {
fn target(&self) -> Result<Target, Error> {
match (self.pip, self.npm, self.yarn) {
(true, false, false) => Target::Pip,
(false, true, false) => Target::Npm { dev: self.dev },
(false, false, true) => Target::Yarn { dev: self.dev },
_ => unreachable!(
"Clap should ensure at least one item in the \"bindings\" group is specified"
),
(false, false, false) => Err(anyhow::anyhow!(
"at least one of --npm, --pip or --yarn has to be specified"
)),
(true, false, false) => Ok(Target::Pip),
(false, true, false) => Ok(Target::Npm { dev: self.dev }),
(false, false, true) => Ok(Target::Yarn { dev: self.dev }),
_ => Err(anyhow::anyhow!(
"only one of --npm, --pip or --yarn has to be specified"
)),
}
}
}
Expand Down Expand Up @@ -153,13 +156,43 @@ impl Target {
/// `npm.cmd` or `yarn.ps1`).
///
/// See <https://github.com/wasmerio/wapm-cli/issues/291> for more.
fn command(self, packages: &[Bindings]) -> Command {
fn command(self, packages: &[Bindings]) -> Result<Command, Error> {
let command_line = match self {
Target::Pip => "pip install",
Target::Yarn { dev: true } => "yarn add --dev",
Target::Yarn { dev: false } => "yarn add",
Target::Npm { dev: true } => "npm install --dev",
Target::Npm { dev: false } => "npm install",
Target::Pip => {
if Command::new("pip").arg("--version").output().is_ok() {
"pip install"
} else if Command::new("pip3").arg("--version").output().is_ok() {
"pip3 install"
} else if Command::new("python").arg("--version").output().is_ok() {
"python -m pip install"
} else if Command::new("python3").arg("--version").output().is_ok() {
"python3 -m pip install"
} else {
return Err(anyhow::anyhow!(
"neither pip, pip3, python or python3 installed"
));
}
}
Target::Yarn { dev } => {
if Command::new("yarn").arg("--version").output().is_err() {
return Err(anyhow::anyhow!("yarn not installed"));
}
if dev {
"yarn add --dev"
} else {
"yarn add"
}
}
Target::Npm { dev } => {
if Command::new("npm").arg("--version").output().is_err() {
return Err(anyhow::anyhow!("yarn not installed"));
}
if dev {
"npm install --dev"
} else {
"npm install"
}
}
};
let mut command_line = command_line.to_string();

Expand All @@ -171,11 +204,11 @@ impl Target {
if cfg!(windows) {
let mut cmd = Command::new("cmd");
cmd.arg("/C").arg(command_line);
cmd
Ok(cmd)
} else {
let mut cmd = Command::new("sh");
cmd.arg("-c").arg(command_line);
cmd
Ok(cmd)
}
}
}
67 changes: 46 additions & 21 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,32 @@ impl Login {
match self.token.as_ref() {
Some(s) => Ok(s.clone()),
None => {
let registry_host = url::Url::parse(&wasmer_registry::format_graphql(
&self.registry,
))
.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Invalid registry for login {}: {e}", self.registry),
)
})?;
let registry_host = registry_host.host_str().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Invalid registry for login {}: no host", self.registry),
)
})?;
Input::new()
.with_prompt(&format!(
"Please paste the login token from https://{}/me:\"",
registry_host
))
.interact_text()
let registry_host = wasmer_registry::format_graphql(&self.registry);
let registry_tld = tldextract::TldExtractor::new(tldextract::TldOption::default())
.extract(&registry_host)
.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Invalid registry for login {}: {e}", self.registry),
)
})?;
let login_prompt = match (
registry_tld.domain.as_deref(),
registry_tld.suffix.as_deref(),
) {
(Some(d), Some(s)) => {
format!("Please paste the login token for https://{d}.{s}/me")
}
_ => "Please paste the login token".to_string(),
};
#[cfg(test)]
{
Ok(login_prompt)
}
#[cfg(not(test))]
{
Input::new().with_prompt(&login_prompt).interact_text()
}
}
}
}
Expand All @@ -49,3 +54,23 @@ impl Login {
.map_err(|e| anyhow::anyhow!("{e}"))
}
}

#[test]
fn test_login_2() {
let login = Login {
registry: "wapm.dev".to_string(),
token: None,
};

assert_eq!(
login.get_token_or_ask_user().unwrap(),
"Please paste the login token for https://wapm.dev/me"
);

let login = Login {
registry: "wapm.dev".to_string(),
token: Some("abc".to_string()),
};

assert_eq!(login.get_token_or_ask_user().unwrap(), "abc");
}

0 comments on commit fef44ae

Please sign in to comment.