summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 61db1c75116b9670cefa58c73ad259d52c5d24d6 (plain) (blame)
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
use std::{
    fs::{create_dir_all, File},
    io::{Read, Write},
    path::Path,
};

use serde::{Deserialize, Serialize};

use crate::{db::db_setup, error::MLE};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cfg {
    pub data: String,
    pub cache: String,
    pub apis: Apis,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Apis {
    pub modrinth: String,
}

impl Cfg {
    pub fn init(path: Option<String>) -> MLE<Self> {
        let configfile = match path.clone() {
            Some(p) => String::from(p),
            None => dirs::config_dir()
                .unwrap()
                .join("modlist.toml")
                .to_string_lossy()
                .to_string(),
        };

        let mut file = match File::open(&configfile) {
            Ok(file) => file,
            Err(err) => {
                if err.kind() == std::io::ErrorKind::NotFound && path.is_none() {
                    create_config(&configfile)?;
                    File::open(&configfile)?
                } else {
                    return Err(err.into());
                }
            }
        };
        let mut content = String::new();
        file.read_to_string(&mut content)?;
        let config = toml::from_str::<Cfg>(&content)?;
        //Check cache
        if !Path::new(&config.cache).exists() {
            create_cache(&config.cache)?;
        };
        //Check database
        //TODO check file
        let datafile = format!("{}/data.db", config.data);
        match File::open(&datafile) {
            Ok(..) => (),
            Err(..) => create_database(&datafile)?,
        };
        Ok(config)
    }
}

fn create_config(path: &str) -> MLE<()> {
    print!("No config file found, create default");
    //Force flush of stdout, else print! doesn't print instantly
    std::io::stdout().flush()?;
    let default_cfg = Cfg {
        //TODO get home dir
        data: String::from("$HOME/.cache/modlist/"),
        cache: String::from("$HOME/.cache/modlist/cache"),
        apis: Apis {
            modrinth: String::from("https://api.modrinth.com/v2/"),
        },
    };
    let mut file = File::create(path)?;
    file.write_all(toml::to_string(&default_cfg)?.as_bytes())?;
    println!(" ✓");
    Ok(())
}

fn create_database(path: &str) -> MLE<()> {
    print!("No database found, create base");
    //Force flush of stdout, else print! doesn't print instantly
    std::io::stdout().flush()?;

    File::create(path)?;
    db_setup(path)?;
    println!(" ✓");
    Ok(())
}

fn create_cache(path: &str) -> MLE<()> {
    print!("No cache direcory found, create one");
    //Force flush of stdout, else print! doesn't print instantly
    std::io::stdout().flush()?;

    create_dir_all(path)?;
    println!(" ✓");
    Ok(())
}