Skip to content

Commit

Permalink
Merge #3297
Browse files Browse the repository at this point in the history
3297: Implement wasmer login r=Michael-F-Bryan a=fschutt



Co-authored-by: Felix Schütt <[email protected]>
Co-authored-by: Felix Schütt <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2022
2 parents 0d5e7d9 + 63fe99a commit b424c9e
Show file tree
Hide file tree
Showing 14 changed files with 957 additions and 441 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-sys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ jobs:
TARGET: ${{ matrix.target }}
TARGET_DIR: target/${{ matrix.target }}/release
CARGO_TARGET: --target ${{ matrix.target }}
WAPM_DEV_TOKEN: ${{ secrets.WAPM_DEV_TOKEN }}
- name: Test integration CLI
if: matrix.run_test && matrix.os == 'windows-2019'
shell: bash
Expand Down
51 changes: 50 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 @@ -71,6 +71,7 @@ libc = { version = "^0.2", default-features = false }
nuke-dir = { version = "0.1.0", optional = true }
webc = { version = "3.0.1", optional = true }
isatty = "0.1.9"
dialoguer = "0.10.2"

[build-dependencies]
chrono = { version = "^0.4", default-features = false, features = [ "std", "clock" ] }
Expand Down
11 changes: 9 additions & 2 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::commands::CreateExe;
use crate::commands::CreateObj;
#[cfg(feature = "wast")]
use crate::commands::Wast;
use crate::commands::{Cache, Config, Inspect, List, Run, SelfUpdate, Validate};
use crate::commands::{Cache, Config, Inspect, List, Login, Run, SelfUpdate, Validate};
use crate::error::PrettyError;
use clap::{CommandFactory, ErrorKind, Parser};
use std::fmt;
Expand Down Expand Up @@ -44,6 +44,10 @@ enum WasmerCLIOptions {
#[clap(name = "run")]
Run(Run),

/// Login into a wapm.io-like registry
#[clap(name = "login")]
Login(Login),

/// Wasmer cache
#[clap(subcommand, name = "cache")]
Cache(Cache),
Expand Down Expand Up @@ -164,6 +168,7 @@ impl WasmerCLIOptions {
Self::Config(config) => config.execute(),
Self::Inspect(inspect) => inspect.execute(),
Self::List(list) => list.execute(),
Self::Login(login) => login.execute(),
#[cfg(feature = "wast")]
Self::Wast(wast) => wast.execute(),
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -220,7 +225,9 @@ fn wasmer_main_inner() -> Result<(), anyhow::Error> {
} else {
match command.unwrap_or(&"".to_string()).as_ref() {
"cache" | "compile" | "config" | "create-exe" | "help" | "inspect" | "run"
| "self-update" | "validate" | "wast" | "binfmt" | "list" => WasmerCLIOptions::parse(),
| "self-update" | "validate" | "wast" | "binfmt" | "list" | "login" => {
WasmerCLIOptions::parse()
}
_ => {
WasmerCLIOptions::try_parse_from(args.iter()).unwrap_or_else(|e| {
match e.kind() {
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod create_exe;
mod create_obj;
mod inspect;
mod list;
mod login;
mod run;
mod self_update;
mod validate;
Expand All @@ -27,7 +28,7 @@ pub use create_exe::*;
pub use create_obj::*;
#[cfg(feature = "wast")]
pub use wast::*;
pub use {cache::*, config::*, inspect::*, list::*, run::*, self_update::*, validate::*};
pub use {cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, validate::*};

/// The kind of object format to emit.
#[derive(Debug, Copy, Clone, clap::Parser)]
Expand Down
51 changes: 51 additions & 0 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use clap::Parser;
use dialoguer::Input;

/// Subcommand for listing packages
#[derive(Debug, Clone, Parser)]
pub struct Login {
/// Registry to log into (default: wapm.io)
#[clap(long, default_value = "wapm.io")]
pub registry: String,
/// Login token
#[clap(name = "TOKEN")]
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 = 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()
}
}
}

/// execute [List]
pub fn execute(&self) -> Result<(), anyhow::Error> {
let token = self.get_token_or_ask_user()?;
wasmer_registry::login::login_and_save_token(&self.registry, &token)
.map_err(|e| anyhow::anyhow!("{e}"))
}
}
3 changes: 3 additions & 0 deletions lib/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = "2021"
license = "MIT"
description = "Crate to interact with the wasmer registry (wapm.io), download packages, etc."

[dev-dependencies]
rand = "0.8.5"

[dependencies]
dirs = "4.0.0"
graphql_client = "0.11.0"
Expand Down
5 changes: 5 additions & 0 deletions lib/registry/graphql/queries/whoami.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query WhoAmIQuery {
viewer {
username
}
}
Loading

0 comments on commit b424c9e

Please sign in to comment.