Files
topgrade/src/config.rs

87 lines
2.2 KiB
Rust
Raw Normal View History

2018-12-11 16:43:26 +02:00
use super::error::{Error, ErrorKind};
2018-07-07 02:18:19 +03:00
use directories::BaseDirs;
2018-12-11 16:43:26 +02:00
use failure::ResultExt;
2018-11-18 14:25:16 +02:00
use serde_derive::Deserialize;
use shellexpand;
use std::collections::BTreeMap;
use std::fs;
2018-11-18 14:25:16 +02:00
use structopt::StructOpt;
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-12-11 16:43:26 +02:00
pub fn read(base_dirs: &BaseDirs) -> Result<Config, Error> {
let config_path = base_dirs.config_dir().join("topgrade.toml");
if !config_path.exists() {
return Ok(Default::default());
}
2018-12-11 16:43:26 +02:00
let mut result: Self = toml::from_str(&fs::read_to_string(config_path).context(ErrorKind::Configuration)?)
.context(ErrorKind::Configuration)?;
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
}
}
2018-09-06 14:42:56 +03:00
#[derive(StructOpt, Debug)]
#[structopt(name = "Topgrade")]
pub struct Opt {
2018-12-05 13:29:31 +02:00
/// Run inside tmux
#[structopt(short = "t", long = "tmux")]
2018-09-06 14:42:56 +03:00
pub run_in_tmux: bool,
2018-12-05 13:29:31 +02:00
/// Cleanup temporary or old files
#[structopt(short = "c", long = "cleanup")]
pub cleanup: bool,
2018-12-05 13:29:31 +02:00
/// Don't perform system upgrade
#[structopt(long = "no-system")]
2018-09-06 14:42:56 +03:00
pub no_system: bool,
2018-12-05 13:29:31 +02:00
/// Don't perform updates on configured git repos
#[structopt(long = "no-git-repos")]
2018-09-06 14:42:56 +03:00
pub no_git_repos: bool,
2018-12-05 13:29:31 +02:00
/// Don't upgrade Emacs packages or configuration files
#[structopt(long = "no-emacs")]
2018-09-06 14:42:56 +03:00
pub no_emacs: bool,
/// Don't upgrade Vim packages or configuration files
#[structopt(long = "no-vim")]
pub no_vim: bool,
2018-12-05 13:29:31 +02:00
/// Print what would be done
#[structopt(short = "n", long = "dry-run")]
2018-09-06 14:42:56 +03:00
pub dry_run: bool,
2018-12-05 13:38:18 +02:00
/// Do not ask to retry failed steps
#[structopt(long = "no-retry")]
pub no_retry: bool,
2018-09-06 14:42:56 +03:00
}