2018-07-07 02:18:19 +03:00
|
|
|
use directories::BaseDirs;
|
2018-06-08 18:19:07 +03:00
|
|
|
use failure;
|
2018-06-11 13:56:57 +03:00
|
|
|
use shellexpand;
|
2018-06-08 18:19:07 +03:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use toml;
|
|
|
|
|
|
2018-06-20 20:26:08 +03:00
|
|
|
type Commands = BTreeMap<String, String>;
|
|
|
|
|
|
2018-06-08 18:19:07 +03:00
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
|
pub struct Config {
|
2018-06-20 20:26:08 +03:00
|
|
|
pre_commands: Option<Commands>,
|
|
|
|
|
commands: Option<Commands>,
|
2018-06-08 18:19:07 +03:00
|
|
|
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> {
|
2018-06-08 18:19:07 +03:00
|
|
|
let config_path = base_dirs.config_dir().join("topgrade.toml");
|
|
|
|
|
if !config_path.exists() {
|
|
|
|
|
return Ok(Default::default());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-11 13:56:57 +03:00
|
|
|
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-08 18:19:07 +03:00
|
|
|
}
|
|
|
|
|
|
2018-06-20 20:26:08 +03:00
|
|
|
pub fn pre_commands(&self) -> &Option<Commands> {
|
|
|
|
|
&self.pre_commands
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn commands(&self) -> &Option<Commands> {
|
2018-06-08 18:19:07 +03:00
|
|
|
&self.commands
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn git_repos(&self) -> &Option<Vec<String>> {
|
|
|
|
|
&self.git_repos
|
|
|
|
|
}
|
|
|
|
|
}
|