cli/
config.rs

1//! # config
2//!
3//! Enable to load/store user level configuration for cargo-make.
4//!
5
6#[cfg(test)]
7#[path = "config_test.rs"]
8mod config_test;
9
10use crate::error::CargoMakeError;
11use crate::storage;
12use crate::types::GlobalConfig;
13use fsio::file::read_text_file;
14use fsio::path::from_path::FromPath;
15use std::path::{Path, PathBuf};
16
17pub static CONFIG_FILE: &'static str = "config.toml";
18
19pub fn get_config_directory() -> Option<PathBuf> {
20    let os_directory = dirs_next::config_dir();
21    storage::get_storage_directory(os_directory, CONFIG_FILE, true)
22}
23
24pub fn load_from_path(directory: PathBuf) -> Result<GlobalConfig, CargoMakeError> {
25    let file_path = Path::new(&directory).join(CONFIG_FILE);
26    debug!("Loading config from: {:#?}", &file_path);
27
28    if file_path.exists() {
29        let config_str = read_text_file(&file_path)?;
30        let mut global_config: GlobalConfig = toml::from_str(&config_str)?;
31
32        global_config.file_name = Some(FromPath::from_path(&file_path));
33
34        Ok(global_config)
35    } else {
36        Ok(GlobalConfig::new())
37    }
38}
39
40/// Returns the configuration
41pub fn load() -> Result<GlobalConfig, CargoMakeError> {
42    match get_config_directory() {
43        Some(directory) => load_from_path(directory),
44        None => Ok(GlobalConfig::new()),
45    }
46}