Files
topgrade/src/config.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2018-07-07 02:18:19 +03:00
use directories::BaseDirs;
use failure;
use shellexpand;
use std::collections::BTreeMap;
use std::fs;
use toml;
2018-06-20 20:26:08 +03:00
type Commands = BTreeMap<String, String>;
#[derive(Deserialize, Default)]
pub struct Config {
2018-06-20 20:26:08 +03:00
pre_commands: Option<Commands>,
commands: Option<Commands>,
git_repos: Option<Vec<String>>,
}
impl Config {
2018-07-07 02:18:19 +03:00
pub fn read(base_dirs: &BaseDirs) -> Result<Config, failure::Error> {
let config_path = base_dirs.config_dir().join("topgrade.toml");
if !config_path.exists() {
return Ok(Default::default());
}
let mut result: Self = toml::from_str(&fs::read_to_string(config_path)?)?;
if let Some(ref mut paths) = &mut result.git_repos {
for path in paths.iter_mut() {
*path = shellexpand::tilde::<&str>(&path.as_ref()).into_owned();
}
}
Ok(result)
}
2018-06-20 20:26:08 +03:00
pub fn pre_commands(&self) -> &Option<Commands> {
&self.pre_commands
}
pub fn commands(&self) -> &Option<Commands> {
&self.commands
}
pub fn git_repos(&self) -> &Option<Vec<String>> {
&self.git_repos
}
}