diff --git a/crates/rover-client/src/query/config/whoami.graphql b/crates/rover-client/src/query/config/whoami.graphql index f4153fbf8..9bf0e18f6 100644 --- a/crates/rover-client/src/query/config/whoami.graphql +++ b/crates/rover-client/src/query/config/whoami.graphql @@ -1,7 +1,9 @@ query WhoAmIQuery { me { __typename - name + ... on Service { + title + } id asActor { type diff --git a/crates/rover-client/src/query/config/whoami.rs b/crates/rover-client/src/query/config/whoami.rs index f90e609d7..20fa59fdb 100644 --- a/crates/rover-client/src/query/config/whoami.rs +++ b/crates/rover-client/src/query/config/whoami.rs @@ -18,8 +18,8 @@ pub struct WhoAmIQuery; #[derive(Debug, PartialEq)] pub struct RegistryIdentity { - pub name: String, pub id: String, + pub graph_title: Option, pub key_actor_type: Actor, } @@ -48,14 +48,21 @@ fn get_identity_from_response_data( // graphs as api key actors, since that's all we _should_ get. // I think it's safe to only include those two kinds of actors in the enum // more here: https://studio-staging.apollographql.com/graph/engine/schema/reference/enums/ActorType?variant=prod + let key_actor_type = match me.as_actor.type_ { who_am_i_query::ActorType::GRAPH => Actor::GRAPH, who_am_i_query::ActorType::USER => Actor::USER, _ => Actor::OTHER, }; + + let graph_title = match me.on { + who_am_i_query::WhoAmIQueryMeOn::Service(s) => Some(s.title), + _ => None, + }; + Ok(RegistryIdentity { id: me.id, - name: me.name, + graph_title, key_actor_type, }) } else { @@ -72,7 +79,7 @@ mod tests { let json_response = json!({ "me": { "__typename": "User", - "name": "Yaboi", + "title": "SearchForTunaService", "id": "gh.nobodydefinitelyhasthisusernamelol", "asActor": { "type": "USER" @@ -83,8 +90,8 @@ mod tests { let output = get_identity_from_response_data(data); let expected_identity = RegistryIdentity { - name: "Yaboi".to_string(), id: "gh.nobodydefinitelyhasthisusernamelol".to_string(), + graph_title: None, key_actor_type: Actor::USER, }; assert!(output.is_ok()); @@ -96,7 +103,7 @@ mod tests { let json_response = json!({ "me": { "__typename": "Service", - "name": "big-ol-graph", + "title": "GraphKeyService", "id": "big-ol-graph-key-lolol", "asActor": { "type": "GRAPH" @@ -107,8 +114,8 @@ mod tests { let output = get_identity_from_response_data(data); let expected_identity = RegistryIdentity { - name: "big-ol-graph".to_string(), id: "big-ol-graph-key-lolol".to_string(), + graph_title: Some("GraphKeyService".to_string()), key_actor_type: Actor::GRAPH, }; assert!(output.is_ok()); diff --git a/src/command/config/whoami.rs b/src/command/config/whoami.rs index f4382dd91..2f64794fb 100644 --- a/src/command/config/whoami.rs +++ b/src/command/config/whoami.rs @@ -1,9 +1,10 @@ -// use ansi_term::Colour::{Cyan, Yellow}; +use ansi_term::Colour::Green; use serde::Serialize; use structopt::StructOpt; use rover_client::query::config::whoami; +use crate::anyhow; use crate::command::RoverStdout; use crate::utils::client::StudioClientConfig; use crate::Result; @@ -23,10 +24,30 @@ impl WhoAmI { let identity = whoami::run(whoami::who_am_i_query::Variables {}, &client)?; - eprintln!( - "Key Info:\n- Name: {}\n- ID: {}\n- Key Type: {:?}", - identity.name, identity.id, identity.key_actor_type - ); + let message = match identity.key_actor_type { + whoami::Actor::GRAPH => Ok(format!( + "Key Info\n{}: {}\n{}: {}\n{}: {:?}", + Green.normal().paint("Graph Title"), + identity.graph_title.unwrap(), + Green.normal().paint("Unique Graph ID"), + identity.id, + Green.normal().paint("Key Type"), + identity.key_actor_type + )), + whoami::Actor::USER => Ok(format!( + "Key Info\n{}: {}\n{}: {:?}", + Green.normal().paint("User ID"), + identity.id, + Green.normal().paint("Key Type"), + identity.key_actor_type + )), + _ => Err(anyhow!( + "The key provided is invalid. Rover only accepts personal and graph API keys" + )), + }?; + + eprintln!("{}", message); + Ok(RoverStdout::None) } }