summaryrefslogtreecommitdiff
path: root/src/cfg/config.rs
blob: cf6188ef9e8437cb02bb2267dd84084985ac85da (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
use serde::{Deserialize, Serialize};
use std::fs;
use toml;

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Loglevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

/// `mesk.toml` configuration fields here
#[derive(Deserialize, Debug, Serialize)]
pub struct Config {
    pub repo: Repo,
    pub log: Log,
    pub paths: Paths,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Log {
    #[serde(rename = "log_file")]
    pub log_file: String,
    #[serde(rename = "log_level")]
    pub log_level: Loglevel,
}

// Rename needed for editing mesk.toml file fields but dont touch code.
#[derive(Deserialize, Debug, Serialize)]
pub struct Repo {
    #[serde(rename = "repo_url")]
    pub repo_url: String,
    #[serde(rename = "auto_update")]
    pub auto_update: bool,
    #[serde(rename = "destination")]
    pub destination: (String, String),
    //    #[serde(rename = "arch")]
    //    pub arch = arch;
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Paths {
    #[serde(rename = "cache_dir")]
    pub cache_dir: String,
    #[serde(rename = "build_dir")]
    pub build_dir: String,
}

impl Config {
    /// Parse the /etc/mesk.toml file and return the Config object.
    ///
    /// This function reads the /etc/mesk.toml file, parses it and returns the Config object.
    pub fn parse() -> Result<Config, toml::de::Error> {
        let contents = fs::read_to_string("/etc/mesk/mesk.toml").unwrap();
        let result: Config = toml::from_str(&contents)?;
        Ok(result)
    }

    /// Return the default configuration as a toml string.
    ///
    /// This function returns the default configuration as a toml string.
    pub fn default() -> Result<String, toml::ser::Error> {
        let default: Config = Config {
            repo: Repo {
                repo_url: format!("https://mesk.anthrill.i2p/repo/{}/", std::env::consts::ARCH),
                auto_update: true,
                destination: (String::from("mesk"), String::from("mesk")),
                // Its a hurt place, you need to generate destinations by i2pd and paste here (to mesk.toml)
                // Better to leave it empty or set it to (mesk, mesk), now destination conn not implemented
            },
            log: Log {
                log_file: String::from("/var/log/mesk.log"),
                log_level: Loglevel::Info,
            },
            paths: Paths {
                cache_dir: String::from("/var/cache/mesk"),
                build_dir: String::from("/var/lib/mesk"),
            },
        };

        let toml_str = toml::to_string(&default)?;
        Ok(toml_str)
    }

    pub fn generate(
        repo: &Option<String>,
        cachedir: &Option<String>,
        buildir: &Option<String>,
    ) -> Result<String, toml::ser::Error> {
        let generator: Config = Config {
            repo: Repo {
                repo_url: if repo.is_none() {
                    format!("https://mesk.anthrill.i2p/repo/{}/", std::env::consts::ARCH)
                } else {
                    repo.clone().unwrap()
                },
                auto_update: true,
                destination: (String::from("mesk"), String::from("mesk")),
            },
            log: Log {
                log_file: String::from("/var/log/mesk.log"),
                log_level: Loglevel::Info,
            },
            paths: Paths {
                cache_dir: if cachedir.is_none() {
                    String::from("/var/cache/mesk")
                } else {
                    cachedir.clone().unwrap()
                },
                build_dir: if buildir.is_none() {
                    String::from("/var/cache/mesk/build")
                } else {
                    buildir.clone().unwrap()
                },
                /*
                 FIXME: I can leave this parameter, but I think it would be better to make the build
                        path in the /var/cache/mesk/$pkgname-$pkgver/BUILD/
                */
            },
        };
        Ok(toml::to_string(&generator)?)
    }
}