sagiegurari / cargo-make
1
//! # config
2
//!
3
//! Enable to load/store user level configuration for cargo-make.
4
//!
5

6
#[cfg(test)]
7
#[path = "./config_test.rs"]
8
mod config_test;
9

10
use crate::storage;
11
use crate::types::GlobalConfig;
12
use dirs;
13
use fsio::file::read_text_file;
14
use fsio::path::from_path::FromPath;
15
use std::path::{Path, PathBuf};
16
use toml;
17

18
static CONFIG_FILE: &'static str = "config.toml";
19

20 8
fn get_config_directory() -> Option<PathBuf> {
21 8
    let os_directory = dirs::config_dir();
22 8
    storage::get_storage_directory(os_directory, CONFIG_FILE, true)
23 8
}
24

25 8
fn load_from_path(directory: PathBuf) -> GlobalConfig {
26 8
    let file_path = Path::new(&directory).join(CONFIG_FILE);
27 8
    debug!("Loading config from: {:#?}", &file_path);
28

29 8
    if file_path.exists() {
30 8
        match read_text_file(&file_path) {
31 8
            Ok(config_str) => {
32 8
                let mut global_config: GlobalConfig = match toml::from_str(&config_str) {
33 8
                    Ok(value) => value,
34 0
                    Err(error) => panic!("Unable to parse global configuration file, {}", error),
35 2
                };
36

37 8
                global_config.file_name = Some(FromPath::from_path(&file_path));
38

39 8
                global_config
40 6
            }
41 2
            Err(error) => panic!(
42 0
                "Unable to read config file: {:?} error: {}",
43 0
                &file_path, error
44 0
            ),
45
        }
46 2
    } else {
47 8
        GlobalConfig::new()
48
    }
49 8
}
50

51
/// Returns the configuration
52 8
pub(crate) fn load() -> GlobalConfig {
53 8
    match get_config_directory() {
54 8
        Some(directory) => load_from_path(directory),
55 0
        None => GlobalConfig::new(),
56
    }
57 8
}

Read our documentation on viewing source code .

Loading