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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::utils::log::{log, LogLevel};

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

enum ConfigParam {
    DefaultCompiler(String),
    DefaultLanguage(String),
    License(String),
}

fn set_config_param(param: ConfigParam, config_file: &PathBuf) {
    let mut global_conf = GlobalConfig::from_file(config_file);
    match param {
        ConfigParam::DefaultCompiler(value) => {
            global_conf.default_compiler = value;
        }
        ConfigParam::DefaultLanguage(value) => {
            global_conf.default_language = value;
        }
        ConfigParam::License(value) => {
            global_conf.license = value;
        }
    }

    std::fs::write(config_file, toml::to_string(&global_conf).unwrap()).unwrap();
}

#[derive(Serialize, Deserialize)]
pub struct GlobalConfig {
    default_compiler: String,
    default_language: String,
    license: String,
}

impl GlobalConfig {
    pub fn set_defaults(config: &PathBuf, parameter: &str, value: &str) {
        match parameter {
            "default_compiler" => {
                if value == "gcc" || value == "clang" {
                    set_config_param(ConfigParam::DefaultCompiler(value.to_string()), config);
                } else {
                    log(
                        LogLevel::Error,
                        "Invalid compiler. See `builder-cpp config --help` for more info",
                    );
                    std::process::exit(1);
                }
            }
            "default_language" => {
                if value == "c" || value == "cpp" {
                    set_config_param(ConfigParam::DefaultLanguage(value.to_string()), config);
                } else {
                    log(
                        LogLevel::Error,
                        "Invalid language. See `builder-cpp config --help` for more info",
                    );
                    std::process::exit(1);
                }
            }
            "license" => {
                if std::path::Path::new(value).exists() {
                    let value = std::fs::read_to_string(value).unwrap();
                    set_config_param(ConfigParam::License(value), config);
                } else {
                    log(
                        LogLevel::Error,
                        "Invalid license file. See `builder-cpp config --help` for more info",
                    );
                    std::process::exit(1);
                }
            }
            _ => {
                log(
                    LogLevel::Error,
                    "Invalid parameter. See `builder-cpp config --help` for more info",
                );
                std::process::exit(1);
            }
        }
    }
    pub fn from_file(path: &PathBuf) -> Self {
        let config = std::fs::read_to_string(path).unwrap();
        let mut config: toml::Value = toml::from_str(&config).unwrap();
        let config = config.as_table_mut().unwrap();
        GlobalConfig {
            default_compiler: config
                .get("default_compiler")
                .unwrap()
                .as_str()
                .unwrap()
                .to_string(),
            default_language: config
                .get("default_language")
                .unwrap()
                .as_str()
                .unwrap()
                .to_string(),
            license: config.get("license").unwrap().as_str().unwrap().to_string(),
        }
    }
    pub fn get_default_compiler(&self) -> String {
        self.default_compiler.clone()
    }

    pub fn get_default_language(&self) -> String {
        self.default_language.clone()
    }

    pub fn get_license(&self) -> String {
        self.license.clone()
    }
}