iggy_cli/commands/binary_users/
update_user.rs1use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use iggy_common::Client;
23use iggy_common::Identifier;
24use iggy_common::UserStatus;
25use iggy_common::update_user::UpdateUser;
26use tracing::{Level, event};
27
28#[derive(Debug, Clone)]
29pub enum UpdateUserType {
30 Name(String),
31 Status(UserStatus),
32}
33
34pub struct UpdateUserCmd {
35 update_type: UpdateUserType,
36 update_user: UpdateUser,
37}
38
39impl UpdateUserCmd {
40 pub fn new(user_id: Identifier, update_type: UpdateUserType) -> Self {
41 let (username, status) = match update_type.clone() {
42 UpdateUserType::Name(username) => (Some(username), None),
43 UpdateUserType::Status(status) => (None, Some(status)),
44 };
45
46 UpdateUserCmd {
47 update_type,
48 update_user: UpdateUser {
49 user_id,
50 username,
51 status,
52 },
53 }
54 }
55
56 fn get_message(&self) -> String {
57 match &self.update_type {
58 UpdateUserType::Name(username) => format!("username: {username}"),
59 UpdateUserType::Status(status) => format!("status: {status}"),
60 }
61 }
62}
63
64#[async_trait]
65impl CliCommand for UpdateUserCmd {
66 fn explain(&self) -> String {
67 format!(
68 "update user with ID: {} with {}",
69 self.update_user.user_id,
70 self.get_message()
71 )
72 }
73
74 async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
75 client
76 .update_user(
77 &self.update_user.user_id,
78 self.update_user.username.as_deref(),
79 self.update_user.status,
80 )
81 .await
82 .with_context(|| {
83 format!(
84 "Problem updating user with ID: {} with {}",
85 self.update_user.user_id,
86 self.get_message()
87 )
88 })?;
89
90 event!(target: PRINT_TARGET, Level::INFO,
91 "User with ID: {} updated with {}",
92 self.update_user.user_id, self.get_message()
93 );
94
95 Ok(())
96 }
97}