show-user: add `--json` flag

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-07 17:21:26 +02:00
parent a9b8f7cee7
commit 3d7d001f65
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
2 changed files with 18 additions and 10 deletions

View File

@ -62,6 +62,9 @@ pub struct UserPasswdArgs {
pub struct UserShowArgs {
#[arg(num_args = 0..)]
username: Vec<String>,
#[clap(short, long)]
json: bool,
}
pub async fn handle_command(command: UserCommand, mut conn: MySqlConnection) -> anyhow::Result<()> {
@ -193,16 +196,20 @@ async fn show_users(args: UserShowArgs, conn: &mut MySqlConnection) -> anyhow::R
result
};
for user in users {
println!(
"User '{}': {}",
&user.user,
if user.has_password {
"password set."
} else {
"no password set."
}
);
if args.json {
println!("{}", serde_json::to_string_pretty(&users)?);
} else {
for user in users {
println!(
"User '{}': {}",
&user.user,
if user.has_password {
"password set."
} else {
"no password set."
}
);
}
}
Ok(())

View File

@ -100,6 +100,7 @@ pub struct DatabaseUser {
#[sqlx(rename = "User")]
pub user: String,
#[serde(skip)]
#[sqlx(rename = "Host")]
pub host: String,