diff --git a/lib/cli/src/cli.rs b/lib/cli/src/cli.rs index 969d541a9..5475e2ac0 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 cff06ea16..08c82be95 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 000000000..275467fc5 --- /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 b50c0a03f..349ef36e7 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;