diff --git a/src/config.rs b/src/config.rs index 6924822..1c76a6f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,13 @@ use crate::common; use crate::refractr::Refractr; + use core::fmt; -use std::io::Read; -use std::path::PathBuf; +use serde_derive::Deserialize; use std::env; use std::fs::{self, File, Metadata}; +use std::io::Read; +use std::path::PathBuf; use toml; -use serde_derive::Deserialize; pub struct ConfigFile { pub path: String, @@ -129,11 +130,11 @@ pub fn read_config(paths: Vec, refractr: &Refractr) -> Result &'static str { @@ -44,6 +47,7 @@ fn main() -> Result<(), String> { None => false }, pid: process::id(), + strict: args.strict, unix: cfg!(unix), verbose: args.verbose }; diff --git a/src/refractr.rs b/src/refractr.rs index 8feb22a..5f8cb6a 100644 --- a/src/refractr.rs +++ b/src/refractr.rs @@ -1,25 +1,26 @@ -use git2::build::CheckoutBuilder; -use git2::{CertificateCheckStatus, Cred, PushOptions, RemoteCallbacks, Repository}; -use sha2::{Sha256, Digest}; - use crate::freak_out; use crate::common; use crate::config::{Config, ConfigFile}; -use std::fs; + +use git2::{CertificateCheckStatus, Cred, PushOptions, RemoteCallbacks, Repository}; +use git2::{Error, ErrorCode}; +use git2::build::CheckoutBuilder; +use hex; +use sha2::{Sha256, Digest}; use std::env; +use std::fs; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; use std::time; -use std::path::{Path, PathBuf}; -use git2::{Error, ErrorCode}; -use hex; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; pub struct Refractr { pub docker: bool, - pub verbose: u8, pub pid: u32, - pub unix: bool + pub strict: bool, + pub unix: bool, + pub verbose: u8 } struct OpenedRepository { @@ -83,7 +84,7 @@ impl Refractr { Ok(_) => remote_list.push(remote_id), Err(e) => { if e.code() == ErrorCode::Exists { - eprintln!("refractr: warning: remote {} already exists, skipping", remote_id); + common::warning(format!("remote {} already exists, skipping", remote_id)); remote_list.push(remote_id) } else { freak_out!(format!("failed to create remote: {}", e)); @@ -95,7 +96,7 @@ impl Refractr { Ok(remote_list) } - fn push_remotes(&self, cfg: &Config, repo: &Repository, remote_list: &Vec) { + fn push_remotes(&self, cfg: &Config, repo: &Repository, remote_list: &Vec) -> Result<(), String> { for id in remote_list { let mut remote = repo.find_remote(&id).unwrap(); common::verbose( @@ -133,13 +134,19 @@ impl Refractr { match remote.push::<&str>(&refs, Some(&mut push_options)) { Ok(_) => (), Err(e) => { - eprintln!("refractr: failed to push to remote: {}: {}", remote.url().unwrap(), e) + if self.strict { + return Err(format!("failed to push to remote: {}: {}", remote.url().unwrap(), e)) + } else { + common::warning(format!("failed to push to remote: {}: {}", remote.url().unwrap(), e)) + } } } } + + Ok(()) } - fn looper(&self, repos: Vec) { + fn looper(&self, repos: Vec) -> Result<(), String> { let mut current_ints = Vec::new(); let running = Arc::new(AtomicBool::new(true)); let r = running.clone(); @@ -178,7 +185,12 @@ impl Refractr { 2, format!("Interval for {} has arrived, pulling", repos[i].cfg.from)); let _ = self.fast_forward(&repos[i].path, &repos[i].cfg.branches); - self.push_remotes(&repos[i].cfg, &repos[i].repo, &repos[i].remotes); + if let Err(e) = self.push_remotes( + &repos[i].cfg, + &repos[i].repo, + &repos[i].remotes) { + return Err(e) + } } } do_break = false; @@ -186,8 +198,9 @@ impl Refractr { } } } - common::verbose(self.verbose, 1, format!("Exited looper due to ^C")); + common::verbose(self.verbose, 1, format!("Exited looper due to ^C")); + Ok(()) } pub fn run(&self, cfgs: Vec) -> Result<(), String> { @@ -229,7 +242,7 @@ impl Refractr { let repo = match Repository::clone(&cfg.config.from, Path::new(&repo_dir)) { Ok(repo) => repo, Err(_) => { - eprintln!("refractr: warning: found existing repo at {}, attempting to use", repo_dir); + common::warning(format!("found existing repo at {}, attempting to use", repo_dir)); match self.fast_forward(&repo_dir, &cfg.config.branches) { Ok(_) => if let Ok(repo) = Repository::open(Path::new(&repo_dir)) { repo @@ -246,7 +259,9 @@ impl Refractr { Ok(v) => v, Err(e) => return Err(e) }; - self.push_remotes(&cfg.config, &repo, &remotes); + if let Err(e) = self.push_remotes(&cfg.config, &repo, &remotes) { + return Err(e) + } if cfg.config.schedule.enabled { loop_repos.push(OpenedRepository { repo, @@ -262,7 +277,7 @@ impl Refractr { self.verbose, 2, format!("{} configs have schedules enabled, setting up looper", loop_repos.len())); - self.looper(loop_repos); + return self.looper(loop_repos); } else { common::verbose( self.verbose,