Skip to main content

iggy_cli/commands/binary_users/
update_user.rs

1/* Licensed to the Apache Software Foundation (ASF) under one
2 * or more contributor license agreements.  See the NOTICE file
3 * distributed with this work for additional information
4 * regarding copyright ownership.  The ASF licenses this file
5 * to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance
7 * with the License.  You may obtain a copy of the License at
8 *
9 *   http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied.  See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19use 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}