From 58b56cca499203990e35163d34461892316ead56 Mon Sep 17 00:00:00 2001 From: Spencer Kohan Date: Sun, 19 Apr 2020 10:49:11 +0200 Subject: [PATCH] made sync not fail if .gitignore is missing --- src/main.rs | 78 +++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/src/main.rs b/src/main.rs index bf2dec1..f28858d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,67 +13,52 @@ use crate::cli::SubCommand; use crate::cli::CliOptions; use structopt::StructOpt; use std::io::prelude::*; +use std::path::Path; // Perform rsync from source to destination -fn rsync(config: &config::SessionConfig) { +fn sync(config: &config::SessionConfig) { use std::process::Command; - // we sync actions explicitly here, since they might be ignored otherwise - let dirsync_dir_local = &format!("{}/.dirsync", &config.local_root); - let dirsync_dir_remote = &format!("{}", &config.destination()); - - let output = &Command::new("rsync") - .arg("-v") // verbose output - .arg("-ar") - .arg(dirsync_dir_local) - .arg(dirsync_dir_remote) - .output() - .expect("failed to execute process"); - - println!("executing rsync: {} {}", &dirsync_dir_local, &dirsync_dir_remote); - println!("status: {}", output.status); - std::io::stdout().write_all(&output.stdout).unwrap(); - std::io::stderr().write_all(&output.stderr).unwrap(); - assert!(output.status.success()); - - if config.ignore_gitignore { - + fn rsync(source: &str, destinatin: &str, args: &Vec) { let output = &Command::new("rsync") .arg("-v") // verbose output - .arg("-ar") - .arg(format!("--exclude-from={}", config.exclude_path().to_str().unwrap())) - .arg("--exclude-from=.gitignore") - .arg(&config.local_root) - .arg(&config.destination()) + .arg("-a") // archived: we use this to only sync files which have changed + .arg("-r") // recursive + .args(args) + .arg(source) + .arg(destinatin) .output() - .expect("failed to execute process"); + .expect("failed to execute rsync"); - println!("executing rsync: {} {}", &config.local_root, &config.destination()); - println!("status: {}", output.status); std::io::stdout().write_all(&output.stdout).unwrap(); std::io::stderr().write_all(&output.stderr).unwrap(); assert!(output.status.success()); + } - } else { - - let output = &Command::new("rsync") - .arg("-v") // verbose output - .arg("-ar") - .arg(format!("--exclude-from={}", config.exclude_path().to_str().unwrap())) - .arg(&config.local_root) - .arg(&config.destination()) - .output() - .expect("failed to execute process"); + // we sync actions explicitly here, since they might be ignored otherwise + let dirsync_dir_local = &format!("{}/.dirsync", &config.local_root); + let dirsync_dir_remote = &format!("{}", &config.destination()); + rsync(dirsync_dir_local, dirsync_dir_remote, &Vec::new()); - println!("executing rsync: {} {}", &config.local_root, &config.destination()); - println!("status: {}", output.status); - std::io::stdout().write_all(&output.stdout).unwrap(); - std::io::stderr().write_all(&output.stderr).unwrap(); - assert!(output.status.success()); + let exclude_gitignore = config.ignore_gitignore && Path::new(".gitignore").exists(); + let exclude_file = Path::new(config.exclude_path().to_str().unwrap()).exists(); + let mut args: Vec = Vec::new(); + if exclude_gitignore { + args.push( + String::from("--exclude-from=.gitignore") + ); + } + if exclude_file { + args.push( + String::from( + format!("--exclude-from={}", config.exclude_path().to_str().unwrap()) + ) + ); } + rsync(&config.local_root, &config.destination(), &args) } fn filter(event: DebouncedEvent) -> Option { @@ -86,11 +71,10 @@ fn filter(event: DebouncedEvent) -> Option { } } - fn start_main_loop(config: &SessionConfig) { println!("config: {:?}", config); - rsync(&config); + sync(&config); let mut remote = remote::Remote::connect(config); remote.execute_if_exists("onSessionDidStart"); @@ -105,7 +89,7 @@ fn start_main_loop(config: &SessionConfig) { println!("handling event: {:?}", event); match filter(event) { Some(_) => { - rsync(&config); + sync(&config); println!("Executing onSyncDidFinish action"); remote.execute_if_exists("onSyncDidFinish"); },