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_database_completer,
13
        protocol::{
14
            ClientToServerMessageStream, DropDatabaseError, Request, Response,
15
            print_drop_databases_output_status, print_drop_databases_output_status_json,
16
            request_validation::ValidationError,
17
        },
18
        types::MySQLDatabase,
19
    },
20
};
21

            
22
#[derive(Parser, Debug, Clone)]
23
pub struct DropDbArgs {
24
    /// The `MySQL` database(s) to drop
25
    #[arg(num_args = 1.., value_name = "DB_NAME")]
26
    #[cfg_attr(not(feature = "suid-sgid-mode"), arg(add = ArgValueCompleter::new(mysql_database_completer)))]
27
    name: Vec<MySQLDatabase>,
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_databases(
39
    args: DropDbArgs,
40
    mut server_connection: ClientToServerMessageStream,
41
) -> anyhow::Result<()> {
42
    if args.name.is_empty() {
43
        anyhow::bail!("No database names 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 databases?\n\n{}\n\nThis action cannot be undone",
56
                args.name
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::DropDatabases(args.name.clone());
73
    server_connection.send(message).await?;
74

            
75
    let result = match server_connection.next().await {
76
        Some(Ok(Response::DropDatabases(result))) => result,
77
        response => return erroneous_server_response(response),
78
    };
79

            
80
    if args.json {
81
        print_drop_databases_output_status_json(&result);
82
    } else {
83
        print_drop_databases_output_status(&result);
84

            
85
        if result.iter().any(|(_, res)| {
86
            matches!(
87
                res,
88
                Err(DropDatabaseError::ValidationError(
89
                    ValidationError::AuthorizationError(_)
90
                ))
91
            )
92
        }) {
93
            print_authorization_owner_hint(&mut server_connection).await?;
94
        }
95
    }
96

            
97
    server_connection.send(Request::Exit).await?;
98

            
99
    if result.values().any(std::result::Result::is_err) {
100
        std::process::exit(1);
101
    }
102

            
103
    Ok(())
104
}