1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::fs::create_dir;

use colored::Colorize;
use quicli::fs::write_to_file;

use crate::consts::DATA_PATH;
use crate::modules::services::databases;
use crate::modules::services::utils::read_replication_file_paths;
use crate::modules::services::utils::{
    create_data_dir, get_config_using_env, get_next_version, read_config_file,
};
use crate::modules::types::cli::Cli;
use crate::modules::types::config::Config;

/// Run replicate command logic
pub fn run_replicate(args: Cli) {
    let config: Config;

    if args.use_env == true {
        config = get_config_using_env();
    } else {
        config = read_config_file();
    }

    create_data_dir();

    let path = DATA_PATH;
    let next_version = get_next_version();

    let file_ending = match config.database.database_type.as_str() {
        "mongodb" => String::from("json"),
        "mysql" => String::from("sql"),
        _ => String::from("sql"),
    };

    create_dir(format!("{}{}", path, next_version)).expect("Could not create directory!");

    let mut error_flag: bool = false;

    for table in config.replicated.clone() {
        let table_data = databases::replicate_table(table.clone(), config.clone());

        if table_data.is_some() {
            let data = &table_data.unwrap();

            if data.len() > 0 {
                write_to_file(
                    format!("{}{}/{}.{}", path, next_version, table.clone(), file_ending),
                    &data,
                )
                .expect("Error while writing to file!");

                let replicated_message = format!("{} replicated", &table);
                println!("{}", replicated_message.green());
            } else {
                // Print message if table is empty
                let empty_message = format!("{} is empty!", &table);
                println!("{}", empty_message.yellow());
            }
        } else {
            // Set error flag
            error_flag = true;

            let error_message = format!("Error while replicating table {}.", &table);
            println!("{}", error_message.red());
        }
    }

    let count = read_replication_file_paths(next_version.clone()).count();

    if error_flag && count <= 0 {
        println!("{}", "Error while replicating database tables!".red());
    } else if error_flag {
        println!("{}", "Partially replicated database tables!".yellow());
        println!("{}", format!("Replication version: {}", next_version));
    } else {
        println!("{}", "Successfully replicated database tables!".green());
        println!("{}", format!("Replication version: {}", next_version));
    }
}