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
use crate::errors::ConfigError;
use serde::{Deserialize, Serialize};
use std::fs;

#[derive(Deserialize, Serialize, Clone)]
pub struct AppConfig {
    website: Option<String>,
    jwt_key: Option<String>,
}

impl AppConfig {
    pub fn website(self) -> Option<String> {
        self.website.clone()
    }

    pub fn jwt_key(self) -> Option<String> {
        self.jwt_key.clone()
    }

    pub fn get() -> Result<Self, ConfigError> {
        let cfg_dir = home::home_dir().unwrap().join(".config/chamber");
        let cfg_file = home::home_dir()
            .unwrap()
            .join(".config/chamber/config.toml");
        if !cfg_dir.as_path().exists() {
            fs::create_dir_all(cfg_dir)?;
        }

        if !cfg_file.as_path().exists() {
            fs::File::create(cfg_file.clone())?;
        }

        let file = std::fs::read_to_string(cfg_file)?;

        let cfg: Self = toml::from_str(&file)?;

        Ok(cfg)
    }

    pub fn set_website(mut self, website: &str) -> Result<(), ConfigError> {
        let cfg_file = home::home_dir()
            .unwrap()
            .join(".config/chamber/config.toml");

        let website = if website.starts_with("localhost") {
            format!("http://{website}")
        } else {
            website.to_string()
        };

        if website.starts_with("http://") & !website.contains("localhost") {
            println!("Please note that HTTP is much less secure than using HTTPS.");
        }

        self.website = Some(website);

        let toml_string = toml::to_string_pretty(&self)?;

        fs::write(cfg_file, toml_string)?;

        Ok(())
    }

    pub fn set_token(mut self, token: &str) -> Result<(), ConfigError> {
        let cfg_file = home::home_dir()
            .unwrap()
            .join(".config/chamber/config.toml");

        self.jwt_key = Some(token.to_owned());

        let toml = toml::to_string_pretty(&self)?;

        fs::write(cfg_file, toml)?;

        Ok(())
    }
}