Support --json for the whoami command (#658)

This commit is contained in:
Brody Over 2025-08-14 19:17:48 -04:00 committed by GitHub
parent c0e98357c0
commit 3ffa987b62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View file

@ -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) => {

View file

@ -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),
}
}