~/selfhut

selfhut/src/utils/config.rs -rw-r--r-- 1.81 kB
0016c2cc — arthurmelton clippy 2 years ago
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
use lazy_static::lazy_static;
use serde_derive::{Deserialize, Serialize};
use std::{fs, path::PathBuf};

lazy_static! {
    pub static ref CONFIG: Config = {
        let mut config = dirs::config_dir().expect("Could not get the config directory");
        config.push("selfhut");
        config.push("selfhut.toml");
        if config.exists() {
            let config: Config = toml::from_str(&String::from_utf8_lossy(
                &fs::read(config.as_path()).expect("Failed to read the config file"),
            ))
            .expect("Could not parse the toml in the config file");
            config
        } else {
            let config_struct = Config {
                name: "Example User".to_string(),
                description: "This is billy and he loves his [website](https://example.com)!!!"
                    .to_string(),
                git_location: dirs::home_dir().unwrap_or_else(|| PathBuf::from("/")),
                domain: "127.0.0.1".to_string(),
                payment_link: None,
                mailing_list: None,
            };
            config.pop();
            let _ = fs::create_dir_all(config.clone());
            config.push("git-server.toml");
            fs::write(
                config,
                toml::to_string(&config_struct).expect("Failed to stringify config"),
            )
            .expect("Failed to set the config content");
            config_struct
        }
    };
}

#[derive(Serialize, Deserialize)]
pub struct Config {
    pub name: String,
    pub description: String,
    pub git_location: PathBuf,
    pub domain: String,
    pub payment_link: Option<String>,
    pub mailing_list: Option<Email>,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Email {
    pub password: String,
    pub imap_url: String,
    pub port: u16,
}