-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael-F-Bryan
committed
Jun 20, 2023
1 parent
95c4352
commit 1dd7068
Showing
10 changed files
with
287 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,122 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
#[cfg(not(test))] | ||
use dialoguer::Input; | ||
use wasmer_registry::WasmerConfig; | ||
|
||
use crate::{Registry, WasmerDir}; | ||
|
||
/// Subcommand for listing packages | ||
#[derive(Debug, Clone, Parser)] | ||
pub struct Login { | ||
/// Registry to log into (default: wasmer.io) | ||
#[clap(long, default_value = "wasmer.io")] | ||
pub registry: String, | ||
/// Login token | ||
#[clap(name = "TOKEN")] | ||
/// Set Wasmer's home directory | ||
#[clap(long, env = "WASMER_DIR", default_value = crate::WASMER_DIR.as_os_str())] | ||
pub wasmer_dir: PathBuf, | ||
/// The registry to fetch packages from (inferred from the environment by | ||
/// default) | ||
#[clap(long, env = "WASMER_REGISTRY")] | ||
pub registry: Option<Registry>, | ||
/// The API token to use when communicating with the registry (inferred from | ||
/// the environment by default) | ||
pub token: Option<String>, | ||
} | ||
|
||
impl Login { | ||
fn get_token_or_ask_user(&self) -> Result<String, std::io::Error> { | ||
match self.token.as_ref() { | ||
Some(s) => Ok(s.clone()), | ||
None => { | ||
let registry_host = wasmer_registry::format_graphql(&self.registry); | ||
let registry_tld = tldextract::TldExtractor::new(tldextract::TldOption::default()) | ||
.extract(®istry_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 from https://{d}.{s}/settings/access-tokens") | ||
} | ||
_ => "Please paste the login token".to_string(), | ||
}; | ||
#[cfg(test)] | ||
{ | ||
Ok(login_prompt) | ||
} | ||
#[cfg(not(test))] | ||
{ | ||
Input::new().with_prompt(&login_prompt).interact_text() | ||
} | ||
fn get_token_or_ask_user(&self, wasmer_dir: &WasmerDir) -> Result<String, anyhow::Error> { | ||
if let Some(token) = &self.token { | ||
return Ok(token.clone()); | ||
} | ||
|
||
let registry_host = wasmer_dir.registry_endpoint()?; | ||
let registry_tld = tldextract::TldExtractor::new(tldextract::TldOption::default()) | ||
.extract(registry_host.as_str()) | ||
.map_err(|e| { | ||
std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
format!("Invalid registry for login {}: {e}", registry_host), | ||
) | ||
})?; | ||
let login_prompt = match ( | ||
registry_tld.domain.as_deref(), | ||
registry_tld.suffix.as_deref(), | ||
) { | ||
(Some(d), Some(s)) => { | ||
format!("Please paste the login token from https://{d}.{s}/settings/access-tokens") | ||
} | ||
_ => "Please paste the login token".to_string(), | ||
}; | ||
#[cfg(test)] | ||
{ | ||
Ok(login_prompt) | ||
} | ||
#[cfg(not(test))] | ||
{ | ||
let token = Input::new().with_prompt(&login_prompt).interact_text()?; | ||
Ok(token) | ||
} | ||
} | ||
|
||
fn wasmer_dir(&self) -> WasmerDir { | ||
WasmerDir::new( | ||
self.wasmer_dir.clone(), | ||
self.registry.clone(), | ||
self.token.clone(), | ||
) | ||
} | ||
|
||
/// execute [List] | ||
pub fn execute(&self) -> Result<(), anyhow::Error> { | ||
let token = self.get_token_or_ask_user()?; | ||
let wasmer_dir = | ||
WasmerConfig::get_wasmer_dir().map_err(|e| anyhow::anyhow!("no wasmer dir: {e}"))?; | ||
match wasmer_registry::login::login_and_save_token(&wasmer_dir, &self.registry, &token)? { | ||
let wasmer_dir = self.wasmer_dir(); | ||
let token = self.get_token_or_ask_user(&wasmer_dir)?; | ||
|
||
let registry = wasmer_dir.registry_endpoint()?; | ||
match wasmer_registry::login::login_and_save_token( | ||
wasmer_dir.dir(), | ||
registry.as_str(), | ||
&token, | ||
)? { | ||
Some(s) => println!("Login for Wasmer user {:?} saved", s), | ||
None => println!( | ||
"Error: no user found on registry {:?} with token {:?}. Token saved regardless.", | ||
self.registry, token | ||
registry, token | ||
), | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_login_2() { | ||
let login = Login { | ||
registry: "wasmer.wtf".to_string(), | ||
token: None, | ||
}; | ||
#[cfg(test)] | ||
mod tests { | ||
use tempfile::TempDir; | ||
|
||
use super::*; | ||
|
||
assert_eq!( | ||
login.get_token_or_ask_user().unwrap(), | ||
"Please paste the login token from https://wasmer.wtf/settings/access-tokens" | ||
); | ||
#[test] | ||
fn interactive_login() { | ||
let temp = TempDir::new().unwrap(); | ||
let login = Login { | ||
registry: Some("wasmer.wtf".into()), | ||
wasmer_dir: temp.path().to_path_buf(), | ||
token: None, | ||
}; | ||
let wasmer_dir = login.wasmer_dir(); | ||
|
||
let login = Login { | ||
registry: "wasmer.wtf".to_string(), | ||
token: Some("abc".to_string()), | ||
}; | ||
assert_eq!( | ||
login.get_token_or_ask_user(&wasmer_dir).unwrap(), | ||
"Please paste the login token from https://wasmer.wtf/settings/access-tokens" | ||
); | ||
} | ||
|
||
#[test] | ||
fn login_with_token() { | ||
let temp = TempDir::new().unwrap(); | ||
let login = Login { | ||
registry: Some("wasmer.wtf".into()), | ||
wasmer_dir: temp.path().to_path_buf(), | ||
token: Some("abc".to_string()), | ||
}; | ||
let wasmer_dir = login.wasmer_dir(); | ||
|
||
assert_eq!(login.get_token_or_ask_user().unwrap(), "abc"); | ||
assert_eq!(login.get_token_or_ask_user(&wasmer_dir).unwrap(), "abc"); | ||
} | ||
} |
Oops, something went wrong.