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)]
38 password: Option<String>,
39 #[arg(long)]
41 admin: bool,
42 },
43 Delete {
45 id: Uuid,
47 #[arg(long)]
49 yes: bool,
50 },
51 #[command(group(ArgGroup::new("role").required(true).args(["admin", "member"])))]
53 SetRole {
54 id: Uuid,
56 #[arg(long)]
58 admin: bool,
59 #[arg(long)]
61 member: bool,
62 },
63 Groups {
65 id: Uuid,
67 },
68 SetGroups {
74 id: Uuid,
76 #[arg(long = "group")]
78 groups: Vec<String>,
79 },
80}
81
82pub async fn execute(client: &IronflowClient, args: &UserArgs, json_mode: bool) -> Result<()> {
89 match &args.command {
90 UserCommands::List => {
91 let response = client.list_users().await?;
92 output::print_output(json_mode, &response, || output::users_table(&response.data))?;
93 }
94 UserCommands::Create {
95 username,
96 email,
97 password,
98 admin,
99 } => {
100 let password = resolve_secret_value(password.as_deref(), "password")?;
101 let request: CreateUserRequest = CreateUserRequest::builder()
102 .username(username.clone())
103 .email(email.clone())
104 .password(password)
105 .is_admin(*admin)
106 .try_into()
107 .context("failed to build CreateUserRequest")?;
108
109 let response = client.create_user(&request).await?;
110 output::print_output(json_mode, &response, || {
111 output::users_table(slice::from_ref(&response.data))
112 })?;
113 }
114 UserCommands::Delete { id, yes } => {
115 confirm(&format!("Delete user '{id}'?"), *yes)?;
116 client.delete_user(*id).await?;
117 output::report_deletion(json_mode, "user", id.to_string())?;
118 }
119 UserCommands::SetRole { id, admin, .. } => {
122 let request: UpdateRoleRequest = UpdateRoleRequest::builder()
123 .is_admin(*admin)
124 .try_into()
125 .context("failed to build UpdateRoleRequest")?;
126
127 let response = client.update_role(*id, &request).await?;
128 output::print_output(json_mode, &response, || {
129 output::users_table(slice::from_ref(&response.data))
130 })?;
131 }
132 UserCommands::Groups { id } => {
133 let response = client.get_user_groups(*id).await?;
134 output::print_output(json_mode, &response, || {
135 output::user_groups_table(&response.data)
136 })?;
137 }
138 UserCommands::SetGroups { id, groups } => {
139 let response = client.update_user_groups(*id, groups).await?;
140 output::print_output(json_mode, &response, || {
141 output::user_groups_table(&response.data)
142 })?;
143 }
144 }
145 Ok(())
146}