Files
topgrade/src/config.rs

123 lines
3.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;
use lazy_static::lazy_static;
2018-12-24 10:09:46 +02:00
use serde::Deserialize;
use shellexpand;
use std::collections::{BTreeMap, HashMap};
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>;
lazy_static! {
// While this is used to automatically generate possible value list everywhere in the code, the
// README.md file still needs to be manually updated.
static ref STEPS_MAPPING: HashMap<&'static str, Step> = {
let mut m = HashMap::new();
m.insert("system", Step::System);
m.insert("git-repos", Step::GitRepos);
m.insert("vim", Step::Vim);
m.insert("emacs", Step::Emacs);
m.insert("gem", Step::Gem);
m
};
}
#[derive(Debug, Clone, PartialEq)]
pub enum Step {
/// Don't perform system upgrade
System,
/// Don't perform updates on configured git repos
GitRepos,
/// Don't upgrade Vim packages or configuration files
Vim,
/// Don't upgrade Emacs packages or configuration files
Emacs,
/// Don't upgrade ruby gems
Gem,
#[cfg(windows)]
/// Don't update Powershell modules
Powershell,
}
impl Step {
fn possible_values() -> Vec<&'static str> {
STEPS_MAPPING.keys().cloned().collect()
}
}
impl std::str::FromStr for Step {
type Err = structopt::clap::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(STEPS_MAPPING.get(s).unwrap().clone())
}
}
#[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
/// Print what would be done
#[structopt(short = "n", long = "dry-run")]
2018-12-31 14:05:15 +02: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,
/// Do not perform upgrades for the given steps
#[structopt(long = "disable", raw(possible_values = "&Step::possible_values()"))]
pub disable: Vec<Step>,
2018-09-06 14:42:56 +03:00
}