mirror of
https://github.com/railwayapp/cli
synced 2026-04-21 14:07:23 +00:00
Support --json for the whoami command (#658)
This commit is contained in:
parent
c0e98357c0
commit
3ffa987b62
2 changed files with 19 additions and 13 deletions
|
|
@ -34,7 +34,7 @@ pub async fn command(args: Args) -> Result<()> {
|
|||
match get_user(&client, &configs).await {
|
||||
Ok(user) => {
|
||||
println!("{} found", "RAILWAY_TOKEN".bold());
|
||||
print_user(user);
|
||||
print_user(user, false);
|
||||
return Ok(());
|
||||
}
|
||||
Err(_e) => {
|
||||
|
|
|
|||
|
|
@ -5,27 +5,33 @@ use super::*;
|
|||
|
||||
/// Get the current logged in user
|
||||
#[derive(Parser)]
|
||||
pub struct Args {}
|
||||
pub struct Args {
|
||||
/// Output in JSON format
|
||||
#[clap(long)]
|
||||
json: bool,
|
||||
}
|
||||
|
||||
pub async fn command(_args: Args) -> Result<()> {
|
||||
pub async fn command(args: Args) -> Result<()> {
|
||||
let configs = Configs::new()?;
|
||||
let client = GQLClient::new_authorized(&configs)?;
|
||||
|
||||
let user: RailwayUser = get_user(&client, &configs).await?;
|
||||
|
||||
print_user(user);
|
||||
print_user(user, args.json);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn print_user(user: RailwayUser) {
|
||||
if let Some(name) = user.name {
|
||||
println!(
|
||||
"Logged in as {} ({}) 👋",
|
||||
name,
|
||||
user.email.bright_magenta().bold()
|
||||
)
|
||||
} else {
|
||||
println!("Logged in as {} 👋", user.email.bright_magenta().bold())
|
||||
pub fn print_user(user: RailwayUser, use_json: bool) {
|
||||
if use_json {
|
||||
println!("{}", serde_json::to_string_pretty(&user).unwrap());
|
||||
return;
|
||||
}
|
||||
|
||||
let email_colored = user.email.bright_magenta().bold();
|
||||
|
||||
match user.name {
|
||||
Some(name) => println!("Logged in as {} ({}) 👋", name, email_colored),
|
||||
None => println!("Logged in as {} 👋", email_colored),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue