ironflow_cli/commands/
user.rs1use std::slice;
4
5use anyhow::{Context, Result};
6use clap::{ArgGroup, Args, Subcommand};
7use ironflow_sdk::IronflowClient;
8use ironflow_sdk::types::{CreateUserRequest, UpdateRoleRequest};
9use uuid::Uuid;
10
11use crate::confirm::{confirm, resolve_secret_value};
12use crate::output;
13
14#[derive(Debug, Args)]
16pub struct UserArgs {
17 #[command(subcommand)]
19 pub command: UserCommands,
20}
21
22#[derive(Debug, Subcommand)]
24pub enum UserCommands {
25 List,
27 Create {
29 username: String,
31 #[arg(long)]
33 email: String,
34 #[arg(long)]
37 password: Option<String>,
38 #[arg(long)]
40 admin: bool,
41 },
42 Delete {
44 id: Uuid,
46 #[arg(long)]
48 yes: bool,
49 },
50 #[command(group(ArgGroup::new("role").required(true).args(["admin", "member"])))]
52 SetRole {
53 id: Uuid,
55 #[arg(long)]
57 admin: bool,
58 #[arg(long)]
60 member: bool,
61 },
62 Groups {
64 id: Uuid,
66 },
67 SetGroups {
73 id: Uuid,
75 #[arg(long = "group")]
77 groups: Vec<String>,
78 },
79}
80
81pub async fn execute(client: &IronflowClient, args: &UserArgs, json_mode: bool) -> Result<()> {
88 match &args.command {
89 UserCommands::List => {
90 let response = client.list_users().await?;
91 output::print_output(json_mode, &response, || output::users_table(&response.data))?;
92 }
93 UserCommands::Create {
94 username,
95 email,
96 password,
97 admin,
98 } => {
99 let password = resolve_secret_value(password.as_deref(), "password")?;
100 let request: CreateUserRequest = CreateUserRequest::builder()
101 .username(username.clone())
102 .email(email.clone())
103 .password(password)
104 .is_admin(*admin)
105 .try_into()
106 .context("failed to build CreateUserRequest")?;
107
108 let response = client.create_user(&request).await?;
109 output::print_output(json_mode, &response, || {
110 output::users_table(slice::from_ref(&response.data))
111 })?;
112 }
113 UserCommands::Delete { id, yes } => {
114 confirm(&format!("Delete user '{id}'?"), *yes)?;
115 client.delete_user(*id).await?;
116 output::report_deletion(json_mode, "user", id.to_string())?;
117 }
118 UserCommands::SetRole { id, admin, .. } => {
121 let request: UpdateRoleRequest = UpdateRoleRequest::builder()
122 .is_admin(*admin)
123 .try_into()
124 .context("failed to build UpdateRoleRequest")?;
125
126 let response = client.update_role(*id, &request).await?;
127 output::print_output(json_mode, &response, || {
128 output::users_table(slice::from_ref(&response.data))
129 })?;
130 }
131 UserCommands::Groups { id } => {
132 let response = client.get_user_groups(*id).await?;
133 output::print_output(json_mode, &response, || {
134 output::user_groups_table(&response.data)
135 })?;
136 }
137 UserCommands::SetGroups { id, groups } => {
138 let response = client.update_user_groups(*id, groups).await?;
139 output::print_output(json_mode, &response, || {
140 output::user_groups_table(&response.data)
141 })?;
142 }
143 }
144 Ok(())
145}