Add config file with support for custom commands and git repos, fix #1

This commit is contained in:
Dror Levin
2018-06-08 18:19:07 +03:00
committed by Roey Darwish Dror
parent 13812ec250
commit bf301f51cd
6 changed files with 153 additions and 13 deletions

31
src/config.rs Normal file
View File

@@ -0,0 +1,31 @@
use directories;
use failure;
use std::collections::BTreeMap;
use std::fs;
use toml;
#[derive(Deserialize, Default)]
pub struct Config {
commands: Option<BTreeMap<String, String>>,
git_repos: Option<Vec<String>>,
}
impl Config {
pub fn read() -> Result<Config, failure::Error> {
let base_dirs = directories::BaseDirs::new();
let config_path = base_dirs.config_dir().join("topgrade.toml");
if !config_path.exists() {
return Ok(Default::default());
}
Ok(toml::from_str(&fs::read_to_string(config_path)?)?)
}
pub fn commands(&self) -> &Option<BTreeMap<String, String>> {
&self.commands
}
pub fn git_repos(&self) -> &Option<Vec<String>> {
&self.git_repos
}
}

View File

@@ -1,5 +1,6 @@
use super::Check;
use failure::Error;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use which::which;
@@ -55,4 +56,10 @@ impl Git {
Ok(None)
}
pub fn insert_if_valid<P: AsRef<Path>>(&self, git_repos: &mut HashSet<String>, path: P) {
if let Some(repo) = self.get_repo_root(path) {
git_repos.insert(repo);
}
}
}

View File

@@ -1,9 +1,15 @@
extern crate directories;
extern crate failure;
extern crate which;
#[macro_use]
extern crate failure_derive;
extern crate termion;
extern crate toml;
#[macro_use]
extern crate serde_derive;
extern crate serde;
mod config;
mod git;
mod linux;
mod npm;
@@ -12,6 +18,7 @@ mod steps;
mod terminal;
mod vim;
use config::Config;
use failure::Error;
use git::Git;
use report::{Report, Reporter};
@@ -76,20 +83,19 @@ fn main() -> Result<(), Error> {
let mut git_repos: HashSet<String> = HashSet::new();
let terminal = Terminal::new();
let mut reports = Report::new();
let config = Config::read()?;
{
let mut collect_repo = |path| {
if let Some(repo) = git.get_repo_root(path) {
git_repos.insert(repo);
}
};
git.insert_if_valid(&mut git_repos, home_path(".emacs.d"));
collect_repo(home_path(".emacs.d"));
if cfg!(unix) {
git.insert_if_valid(&mut git_repos, home_path(".zshrc"));
git.insert_if_valid(&mut git_repos, home_path(".oh-my-zsh"));
git.insert_if_valid(&mut git_repos, home_path(".tmux"));
}
if cfg!(unix) {
collect_repo(home_path(".zshrc"));
collect_repo(home_path(".oh-my-zsh"));
collect_repo(home_path(".tmux"));
if let Some(custom_git_repos) = config.git_repos() {
for git_repo in custom_git_repos {
git.insert_if_valid(&mut git_repos, git_repo);
}
}
@@ -201,6 +207,13 @@ fn main() -> Result<(), Error> {
upgrade_macos().report("System upgrade", &mut reports);;
}
if let Some(commands) = config.commands() {
for (name, command) in commands {
terminal.print_separator(name);
run_custom_command(&command).report(name.as_ref(), &mut reports);
}
}
let mut reports: Vec<_> = reports.into_iter().collect();
reports.sort();

View File

@@ -220,3 +220,14 @@ pub fn upgrade_debian(
Ok(())
}
pub fn run_custom_command(command: &str) -> Result<(), failure::Error> {
Command::new("sh")
.arg("-c")
.arg(command)
.spawn()?
.wait()?
.check()?;
Ok(())
}