1
use std::io::IsTerminal;
2

            
3
use clap::Parser;
4
use clap_complete::ArgValueCompleter;
5
use dialoguer::Confirm;
6
use futures_util::SinkExt;
7
use tokio_stream::StreamExt;
8

            
9
use crate::{
10
    client::commands::{erroneous_server_response, print_authorization_owner_hint},
11
    core::{
12
        completion::mysql_user_completer,
13
        protocol::{
14
            ClientToServerMessageStream, DropUserError, Request, Response,
15
            print_drop_users_output_status, print_drop_users_output_status_json,
16
            request_validation::ValidationError,
17
        },
18
        types::MySQLUser,
19
    },
20
};
21

            
22
#[derive(Parser, Debug, Clone)]
23
pub struct DropUserArgs {
24
    /// The `MySQL` user(s) to drop
25
    #[arg(num_args = 1.., value_name = "USER_NAME")]
26
    #[cfg_attr(not(feature = "suid-sgid-mode"), arg(add = ArgValueCompleter::new(mysql_user_completer)))]
27
    username: Vec<MySQLUser>,
28

            
29
    /// Print the information as JSON
30
    #[arg(short, long)]
31
    json: bool,
32

            
33
    /// Automatically confirm action without prompting
34
    #[arg(short, long)]
35
    yes: bool,
36
}
37

            
38
pub async fn drop_users(
39
    args: DropUserArgs,
40
    mut server_connection: ClientToServerMessageStream,
41
) -> anyhow::Result<()> {
42
    if args.username.is_empty() {
43
        anyhow::bail!("No usernames provided");
44
    }
45

            
46
    if !std::io::stdin().is_terminal() && !args.yes {
47
        anyhow::bail!(
48
            "Cannot prompt for confirmation in non-interactive mode. Use --yes to automatically confirm."
49
        );
50
    }
51

            
52
    if !args.yes {
53
        let confirmation = Confirm::new()
54
            .with_prompt(format!(
55
                "Are you sure you want to drop the users?\n\n{}\n\nThis action cannot be undone",
56
                args.username
57
                    .iter()
58
                    .map(|d| format!("- {d}"))
59
                    .collect::<Vec<_>>()
60
                    .join("\n")
61
            ))
62
            .interact()?;
63

            
64
        if !confirmation {
65
            // TODO: should we return with an error code here?
66
            println!("Aborting drop operation.");
67
            server_connection.send(Request::Exit).await?;
68
            return Ok(());
69
        }
70
    }
71

            
72
    let message = Request::DropUsers(args.username.clone());
73

            
74
    if let Err(err) = server_connection.send(message).await {
75
        server_connection.close().await.ok();
76
        anyhow::bail!(err);
77
    }
78

            
79
    let result = match server_connection.next().await {
80
        Some(Ok(Response::DropUsers(result))) => result,
81
        response => return erroneous_server_response(response),
82
    };
83

            
84
    if args.json {
85
        print_drop_users_output_status_json(&result);
86
    } else {
87
        print_drop_users_output_status(&result);
88

            
89
        if result.iter().any(|(_, res)| {
90
            matches!(
91
                res,
92
                Err(DropUserError::ValidationError(
93
                    ValidationError::AuthorizationError(_)
94
                ))
95
            )
96
        }) {
97
            print_authorization_owner_hint(&mut server_connection).await?;
98
        }
99
    }
100

            
101
    server_connection.send(Request::Exit).await?;
102

            
103
    if result.values().any(std::result::Result::is_err) {
104
        std::process::exit(1);
105
    }
106

            
107
    Ok(())
108
}