From 9d7ed9e04a66849fb39c1d02738acdd168f86d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 17 Nov 2022 11:27:49 +0100 Subject: [PATCH 01/11] Add wasmer whoami --- lib/cli/src/cli.rs | 5 +++++ lib/cli/src/commands.rs | 1 + lib/cli/src/commands/whoami.rs | 18 +++++++++++++++++ lib/registry/src/lib.rs | 35 ++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 lib/cli/src/commands/whoami.rs diff --git a/lib/cli/src/cli.rs b/lib/cli/src/cli.rs index 969d541a95f..5475e2ac043 100644 --- a/lib/cli/src/cli.rs +++ b/lib/cli/src/cli.rs @@ -150,6 +150,10 @@ enum WasmerCLIOptions { #[cfg(target_os = "linux")] #[clap(name = "binfmt")] Binfmt(Binfmt), + + /// Shows the current logged in user for the current active registry + #[clap(name = "whoami")] + Whoami(Whoami), } impl WasmerCLIOptions { @@ -173,6 +177,7 @@ impl WasmerCLIOptions { Self::Wast(wast) => wast.execute(), #[cfg(target_os = "linux")] Self::Binfmt(binfmt) => binfmt.execute(), + Self::Whoami(whoami) => whoami.execute(), } } } diff --git a/lib/cli/src/commands.rs b/lib/cli/src/commands.rs index cff06ea16ba..08c82be95ee 100644 --- a/lib/cli/src/commands.rs +++ b/lib/cli/src/commands.rs @@ -17,6 +17,7 @@ mod self_update; mod validate; #[cfg(feature = "wast")] mod wast; +mod whoami; #[cfg(target_os = "linux")] pub use binfmt::*; diff --git a/lib/cli/src/commands/whoami.rs b/lib/cli/src/commands/whoami.rs new file mode 100644 index 00000000000..275467fc524 --- /dev/null +++ b/lib/cli/src/commands/whoami.rs @@ -0,0 +1,18 @@ +use clap::Parser; + +#[derive(Debug, Parser)] +/// The options for the `wasmer whoami` subcommand +pub struct Whoami { + /// Which registry to check the logged in username for + #[clap(long, name = "registry")] + pub registry: Option, +} + +impl Whoami { + /// Execute `wasmer whoami` + pub fn execute(&self) -> Result<(), anyhow::Error> { + let (registry, username) = wasmer_registry::whoami(self.registry.as_deref())?; + println!("logged into registry {registry:?} as user {username:?}"); + Ok(()) + } +} diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index b50c0a03f86..349ef36e7cd 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -855,6 +855,41 @@ pub fn install_package( )) } +pub fn whoami(registry: Option<&str>) -> Result<(String, String), anyhow::Error> { + use crate::graphql::{who_am_i_query, WhoAmIQuery}; + use anyhow::Context; + use graphql_client::GraphQLQuery; + + let config = PartialWapmConfig::from_file() + .map_err(|e| anyhow::anyhow!("{e}")) + .context(anyhow::anyhow!("{registry:?}"))?; + + let registry = match registry { + Some(s) => format_graphql(s), + None => config.registry.get_current_registry(), + }; + + let login_token = config + .registry + .get_login_token_for_registry(®istry) + .ok_or_else(|| anyhow::anyhow!("not logged into registry {:?}", registry))?; + + let q = WhoAmIQuery::build_query(who_am_i_query::Variables {}); + let response: who_am_i_query::ResponseData = + crate::graphql::execute_query(®istry, &login_token, &q) + .map_err(|e| anyhow::anyhow!("{e}")) + .context(anyhow::anyhow!("{registry:?}"))?; + + let username = response + .viewer + .as_ref() + .ok_or_else(|| anyhow::anyhow!("not logged into registry {:?}", registry))? + .username + .to_string(); + + Ok((registry, username)) +} + pub fn test_if_registry_present(registry: &str) -> Result { use crate::graphql::{test_if_registry_present, TestIfRegistryPresent}; use graphql_client::GraphQLQuery; From 3bb8ffd620e8657a9f305b459955ad754da157bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 17 Nov 2022 11:33:51 +0100 Subject: [PATCH 02/11] Add integration test for wasmer whoami --- tests/integration/cli/tests/run.rs | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/integration/cli/tests/run.rs b/tests/integration/cli/tests/run.rs index dafb0d3f960..afe71d4e298 100644 --- a/tests/integration/cli/tests/run.rs +++ b/tests/integration/cli/tests/run.rs @@ -21,6 +21,53 @@ fn test_no_start_wat_path() -> String { format!("{}/{}", ASSET_PATH, "no_start.wat") } +#[test] +fn run_whoami_works() -> anyhow::Result<()> { + let ciuser_token = std::env::var("WAPM_DEV_TOKEN").expect("no CIUSER / WAPM_DEV_TOKEN token"); + + let output = Command::new(get_wasmer_path()) + .arg("login") + .arg("--registry") + .arg("wapm.dev") + .arg(ciuser_token) + .output()?; + + if !output.status.success() { + bail!( + "wasmer login failed with: stdout: {}\n\nstderr: {}", + std::str::from_utf8(&output.stdout) + .expect("stdout is not utf8! need to handle arbitrary bytes"), + std::str::from_utf8(&output.stderr) + .expect("stderr is not utf8! need to handle arbitrary bytes") + ); + } + + let output = Command::new(get_wasmer_path()) + .arg("whoami") + .arg("--registry") + .arg("wapm.dev") + .output()?; + + let stdout = std::str::from_utf8(&output.stdout) + .expect("stdout is not utf8! need to handle arbitrary bytes"); + + if !output.status.success() { + bail!( + "linking failed with: stdout: {}\n\nstderr: {}", + stdout, + std::str::from_utf8(&output.stderr) + .expect("stderr is not utf8! need to handle arbitrary bytes") + ); + } + + assert_eq!( + stdout, + "logged into registry \"https://registry.wapm.dev/graphql\" as user \"ciuser\"\n" + ); + + Ok(()) +} + #[test] fn run_wasi_works() -> anyhow::Result<()> { let output = Command::new(get_wasmer_path()) From 33a2585b1424527684e37b5b9340751cf4e353bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 17 Nov 2022 15:28:59 +0100 Subject: [PATCH 03/11] Update from master, fix merge conflicts --- lib/cli/src/cli.rs | 2 +- lib/cli/src/commands.rs | 2 +- lib/registry/src/graphql.rs | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/cli/src/cli.rs b/lib/cli/src/cli.rs index 5475e2ac043..6350feefd7f 100644 --- a/lib/cli/src/cli.rs +++ b/lib/cli/src/cli.rs @@ -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, Login, Run, SelfUpdate, Validate}; +use crate::commands::{Cache, Config, Inspect, List, Login, Run, SelfUpdate, Validate, Whoami}; use crate::error::PrettyError; use clap::{CommandFactory, ErrorKind, Parser}; use std::fmt; diff --git a/lib/cli/src/commands.rs b/lib/cli/src/commands.rs index 08c82be95ee..93dfaae9533 100644 --- a/lib/cli/src/commands.rs +++ b/lib/cli/src/commands.rs @@ -29,7 +29,7 @@ pub use create_exe::*; pub use create_obj::*; #[cfg(feature = "wast")] pub use wast::*; -pub use {cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, validate::*}; +pub use {cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, validate::*, whoami::*}; /// The kind of object format to emit. #[derive(Debug, Copy, Clone, clap::Parser)] diff --git a/lib/registry/src/graphql.rs b/lib/registry/src/graphql.rs index 26cf05c5313..09d93de999b 100644 --- a/lib/registry/src/graphql.rs +++ b/lib/registry/src/graphql.rs @@ -84,6 +84,14 @@ mod proxy { )] pub(crate) struct GetPackageVersionQuery; +#[derive(GraphQLQuery)] +#[graphql( + schema_path = "graphql/schema.graphql", + query_path = "graphql/queries/whoami.graphql", + response_derives = "Debug" +)] +pub(crate) struct WhoAmIQuery; + #[derive(GraphQLQuery)] #[graphql( schema_path = "graphql/schema.graphql", From fff2917ba92dc2e981c78f5997d065194a53cdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 17 Nov 2022 15:32:07 +0100 Subject: [PATCH 04/11] cargo fmt --- lib/cli/src/commands.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/commands.rs b/lib/cli/src/commands.rs index 93dfaae9533..012369b2ad2 100644 --- a/lib/cli/src/commands.rs +++ b/lib/cli/src/commands.rs @@ -29,7 +29,10 @@ pub use create_exe::*; pub use create_obj::*; #[cfg(feature = "wast")] pub use wast::*; -pub use {cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, validate::*, whoami::*}; +pub use { + cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, validate::*, + whoami::*, +}; /// The kind of object format to emit. #[derive(Debug, Copy, Clone, clap::Parser)] From 8644fc256691be09195826953b0d7332543bd435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Fri, 18 Nov 2022 12:10:53 +0100 Subject: [PATCH 05/11] Fix make lint --- lib/cli/src/cli.rs | 4 +++- lib/cli/src/commands.rs | 4 ++-- lib/registry/src/lib.rs | 1 - 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/cli/src/cli.rs b/lib/cli/src/cli.rs index 494a5c42bb8..57115cc700d 100644 --- a/lib/cli/src/cli.rs +++ b/lib/cli/src/cli.rs @@ -10,7 +10,9 @@ use crate::commands::CreateExe; use crate::commands::CreateObj; #[cfg(feature = "wast")] use crate::commands::Wast; -use crate::commands::{Add, Cache, Config, Inspect, List, Login, Run, SelfUpdate, Validate, Whoami}; +use crate::commands::{ + Add, Cache, Config, Inspect, List, Login, Run, SelfUpdate, Validate, Whoami, +}; use crate::error::PrettyError; use clap::{CommandFactory, ErrorKind, Parser}; use std::{fmt, str::FromStr}; diff --git a/lib/cli/src/commands.rs b/lib/cli/src/commands.rs index 9fefdc605e9..d59d5c78cf4 100644 --- a/lib/cli/src/commands.rs +++ b/lib/cli/src/commands.rs @@ -31,8 +31,8 @@ pub use create_obj::*; #[cfg(feature = "wast")] pub use wast::*; pub use { - add::*, cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, validate::*, - whoami::*, + add::*, cache::*, config::*, inspect::*, list::*, login::*, run::*, self_update::*, + validate::*, whoami::*, }; /// The kind of object format to emit. diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 5cf1515be7f..8bc3d4fc607 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -874,7 +874,6 @@ pub fn install_package( pub fn whoami(registry: Option<&str>) -> Result<(String, String), anyhow::Error> { use crate::graphql::{who_am_i_query, WhoAmIQuery}; - use anyhow::Context; use graphql_client::GraphQLQuery; let config = PartialWapmConfig::from_file() From 8dfe8daffd0953b3468657ab0e80a44ddd0c57cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Fri, 18 Nov 2022 17:08:10 +0100 Subject: [PATCH 06/11] Fix compilation error --- lib/cli/src/commands/whoami.rs | 3 +++ lib/registry/src/lib.rs | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/cli/src/commands/whoami.rs b/lib/cli/src/commands/whoami.rs index 275467fc524..9c5afb549c7 100644 --- a/lib/cli/src/commands/whoami.rs +++ b/lib/cli/src/commands/whoami.rs @@ -11,6 +11,9 @@ pub struct Whoami { impl Whoami { /// Execute `wasmer whoami` pub fn execute(&self) -> Result<(), anyhow::Error> { + #[cfg(test)] + let (registry, username) = wasmer_registry::whoami("whoami", self.registry.as_deref())?; + #[cfg(not(test))] let (registry, username) = wasmer_registry::whoami(self.registry.as_deref())?; println!("logged into registry {registry:?} as user {username:?}"); Ok(()) diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 8bc3d4fc607..3bc6e3e55b6 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -872,11 +872,19 @@ pub fn install_package( )) } -pub fn whoami(registry: Option<&str>) -> Result<(String, String), anyhow::Error> { +pub fn whoami( + #[cfg(test)] test_name: &str, + registry: Option<&str>, +) -> Result<(String, String), anyhow::Error> { use crate::graphql::{who_am_i_query, WhoAmIQuery}; use graphql_client::GraphQLQuery; - let config = PartialWapmConfig::from_file() + #[cfg(test)] + let config = PartialWapmConfig::from_file(test_name); + #[cfg(not(test))] + let config = PartialWapmConfig::from_file(); + + let config = config .map_err(|e| anyhow::anyhow!("{e}")) .context(anyhow::anyhow!("{registry:?}"))?; From 6141043c28fd15108267d53d1cc025b3e172aeb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Sun, 20 Nov 2022 11:23:29 +0100 Subject: [PATCH 07/11] Fix unit tests --- lib/cli/src/commands/whoami.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/cli/src/commands/whoami.rs b/lib/cli/src/commands/whoami.rs index 9c5afb549c7..275467fc524 100644 --- a/lib/cli/src/commands/whoami.rs +++ b/lib/cli/src/commands/whoami.rs @@ -11,9 +11,6 @@ pub struct Whoami { impl Whoami { /// Execute `wasmer whoami` pub fn execute(&self) -> Result<(), anyhow::Error> { - #[cfg(test)] - let (registry, username) = wasmer_registry::whoami("whoami", self.registry.as_deref())?; - #[cfg(not(test))] let (registry, username) = wasmer_registry::whoami(self.registry.as_deref())?; println!("logged into registry {registry:?} as user {username:?}"); Ok(()) From 04eda8d5462bc38be36a1b932ab8e65dc2fa51de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 21 Nov 2022 12:12:18 +0100 Subject: [PATCH 08/11] Remove unnecessary clap(name = ) --- lib/cli/src/cli.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/cli/src/cli.rs b/lib/cli/src/cli.rs index 57115cc700d..5829fb16cd3 100644 --- a/lib/cli/src/cli.rs +++ b/lib/cli/src/cli.rs @@ -39,28 +39,23 @@ use std::{fmt, str::FromStr}; /// The options for the wasmer Command Line Interface enum WasmerCLIOptions { /// List all locally installed packages - #[clap(name = "list")] List(List), /// Run a WebAssembly file. Formats accepted: wasm, wat - #[clap(name = "run")] Run(Run), /// Login into a wapm.io-like registry - #[clap(name = "login")] Login(Login), /// Wasmer cache - #[clap(subcommand, name = "cache")] + #[clap(subcommand)] Cache(Cache), /// Validate a WebAssembly binary - #[clap(name = "validate")] Validate(Validate), /// Compile a WebAssembly binary #[cfg(feature = "compiler")] - #[clap(name = "compile")] Compile(Compile), /// Compile a WebAssembly binary into a native executable @@ -132,7 +127,6 @@ enum WasmerCLIOptions { /// Get various configuration information needed /// to compile programs which use Wasmer - #[clap(name = "config")] Config(Config), /// Update wasmer to the latest version @@ -140,21 +134,17 @@ enum WasmerCLIOptions { SelfUpdate(SelfUpdate), /// Inspect a WebAssembly file - #[clap(name = "inspect")] Inspect(Inspect), /// Run spec testsuite #[cfg(feature = "wast")] - #[clap(name = "wast")] Wast(Wast), /// Unregister and/or register wasmer as binfmt interpreter #[cfg(target_os = "linux")] - #[clap(name = "binfmt")] Binfmt(Binfmt), /// Shows the current logged in user for the current active registry - #[clap(name = "whoami")] Whoami(Whoami), /// Add a WAPM package's bindings to your application. From 0e9ff36e50a1ea744657d6b0e2e5c66f43270a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= <12084016+fschutt@users.noreply.github.com> Date: Mon, 21 Nov 2022 12:14:26 +0100 Subject: [PATCH 09/11] Update lib/registry/src/lib.rs Co-authored-by: Michael Bryan --- lib/registry/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index b623aa52e2a..80f0fc065a4 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -907,8 +907,7 @@ pub fn whoami( let config = PartialWapmConfig::from_file(); let config = config - .map_err(|e| anyhow::anyhow!("{e}")) - .context(anyhow::anyhow!("{registry:?}"))?; + .with_context(|| format!("{registry:?}"))?; let registry = match registry { Some(s) => format_graphql(s), From fd8e321a8498b39287838f0458e0e1ac74ce4b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= <12084016+fschutt@users.noreply.github.com> Date: Mon, 21 Nov 2022 12:14:46 +0100 Subject: [PATCH 10/11] Fix context -> with_context Co-authored-by: Michael Bryan --- lib/registry/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 80f0fc065a4..4915fbbca40 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -922,8 +922,7 @@ pub fn whoami( let q = WhoAmIQuery::build_query(who_am_i_query::Variables {}); let response: who_am_i_query::ResponseData = crate::graphql::execute_query(®istry, &login_token, &q) - .map_err(|e| anyhow::anyhow!("{e}")) - .context(anyhow::anyhow!("{registry:?}"))?; + .with_context(|| format!("{registry:?}"))?; let username = response .viewer From 9beaf8ef237d1286da355924b42d41a0181f8575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 21 Nov 2022 12:28:59 +0100 Subject: [PATCH 11/11] Fix integration tests failing locally + fix make lint --- lib/registry/src/lib.rs | 3 ++- tests/integration/cli/tests/login.rs | 5 +++++ tests/integration/cli/tests/run.rs | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 4915fbbca40..2045fe0d470 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -907,6 +907,7 @@ pub fn whoami( let config = PartialWapmConfig::from_file(); let config = config + .map_err(|e| anyhow::anyhow!("{e}")) .with_context(|| format!("{registry:?}"))?; let registry = match registry { @@ -922,7 +923,7 @@ pub fn whoami( let q = WhoAmIQuery::build_query(who_am_i_query::Variables {}); let response: who_am_i_query::ResponseData = crate::graphql::execute_query(®istry, &login_token, &q) - .with_context(|| format!("{registry:?}"))?; + .with_context(|| format!("{registry:?}"))?; let username = response .viewer diff --git a/tests/integration/cli/tests/login.rs b/tests/integration/cli/tests/login.rs index 7a3cdb9ad5f..554ac8f5c59 100644 --- a/tests/integration/cli/tests/login.rs +++ b/tests/integration/cli/tests/login.rs @@ -5,6 +5,11 @@ use wasmer_integration_tests_cli::{get_repo_root_path, get_wasmer_path, ASSET_PA #[test] fn login_works() -> anyhow::Result<()> { + // running test locally: should always pass since + // developers don't have access to WAPM_DEV_TOKEN + if std::env::var("GITHUB_TOKEN").is_err() { + return Ok(()); + } let wapm_dev_token = std::env::var("WAPM_DEV_TOKEN").expect("WAPM_DEV_TOKEN env var not set"); let output = Command::new(get_wasmer_path()) .arg("login") diff --git a/tests/integration/cli/tests/run.rs b/tests/integration/cli/tests/run.rs index f11795ded1d..489af406a4e 100644 --- a/tests/integration/cli/tests/run.rs +++ b/tests/integration/cli/tests/run.rs @@ -83,6 +83,12 @@ fn test_cross_compile_python_windows() -> anyhow::Result<()> { #[test] fn run_whoami_works() -> anyhow::Result<()> { + // running test locally: should always pass since + // developers don't have access to WAPM_DEV_TOKEN + if std::env::var("GITHUB_TOKEN").is_err() { + return Ok(()); + } + let ciuser_token = std::env::var("WAPM_DEV_TOKEN").expect("no CIUSER / WAPM_DEV_TOKEN token"); let output = Command::new(get_wasmer_path())