-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathwhoami.rs
85 lines (72 loc) · 2.64 KB
/
whoami.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use ansi_term::Colour::Green;
use rover_client::query::config::who_am_i::{query_runner, Actor, ConfigWhoAmIInput};
use serde::Serialize;
use structopt::StructOpt;
use houston::CredentialOrigin;
use crate::anyhow;
use crate::command::RoverStdout;
use crate::utils::client::StudioClientConfig;
use crate::utils::env::RoverEnvKey;
use crate::Result;
use houston as config;
#[derive(Debug, Serialize, StructOpt)]
pub struct WhoAmI {
/// Name of configuration profile to use
#[structopt(long = "profile", default_value = "default")]
#[serde(skip_serializing)]
profile_name: String,
}
impl WhoAmI {
pub fn run(&self, client_config: StudioClientConfig) -> Result<RoverStdout> {
let client = client_config.get_client(&self.profile_name)?;
eprintln!("Checking identity of your API key against the registry.");
let identity = query_runner::run(ConfigWhoAmIInput {}, &client)?;
let mut message = format!(
"{}: {:?}\n",
Green.normal().paint("Key Type"),
identity.key_actor_type
);
match identity.key_actor_type {
Actor::GRAPH => {
if let Some(graph_title) = identity.graph_title {
message.push_str(&format!(
"{}: {}\n",
Green.normal().paint("Graph Title"),
&graph_title
));
}
message.push_str(&format!(
"{}: {}\n",
Green.normal().paint("Unique Graph ID"),
identity.id
));
Ok(())
}
Actor::USER => {
message.push_str(&format!(
"{}: {}\n",
Green.normal().paint("User ID"),
identity.id
));
Ok(())
}
_ => Err(anyhow!(
"The key provided is invalid. Rover only accepts personal and graph API keys"
)),
}?;
let origin = match client.get_credential_origin() {
CredentialOrigin::ConfigFile(path) => format!("--profile {}", &path),
CredentialOrigin::EnvVar => format!("${}", &RoverEnvKey::Key),
};
message.push_str(&format!("{}: {}", Green.normal().paint("Origin"), &origin));
let credential =
config::Profile::get_credential(&self.profile_name, &client_config.config)?;
message.push_str(&format!(
"\n{}: {}",
Green.normal().paint("API Key"),
credential.api_key
));
eprintln!("{}", message);
Ok(RoverStdout::None)
}
}