From 0412233f864455812545e7a92f885f8ac75919a1 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sun, 2 Mar 2025 01:03:49 +0900 Subject: [PATCH] new(sync): add option to use git cli --- src/cmd_sync.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- tests/cli.rs | 39 ++++++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/cmd_sync.rs b/src/cmd_sync.rs index 83b9e23..79ce171 100644 --- a/src/cmd_sync.rs +++ b/src/cmd_sync.rs @@ -1,6 +1,7 @@ use std::{ io::{self, Write}, path::{Path, PathBuf}, + process, }; use anyhow::{Context, Result, anyhow}; @@ -14,12 +15,58 @@ pub(crate) fn cmd_sync( use_cl: bool, ) -> Result<()> { if use_cl { - todo!("do here next") + cmd_sync_cl(config_dir, remote_name, ssh_key) } else { cmd_sync_custom(config_dir, remote_name, use_sshagent, ssh_key) } } +fn cmd_sync_cl( + config_dir: &PathBuf, + remote_name: Option, + ssh_key: Option, +) -> Result<()> { + info!("cmd_sync (command line version)"); + + trace!("pull"); + let args = |cmd| { + let mut args = vec![cmd]; + if let Some(ref remote_name) = remote_name { + args.push(remote_name.clone()); + } + if let Some(ref ssh_key) = ssh_key { + args.push("-i".to_string()); + args.push(ssh_key.to_str().unwrap().to_owned()); + } + args + }; + let git_pull_result = process::Command::new("git") + .args(args("pull".to_owned())) + .current_dir(config_dir) + .status() + .context("error while executing git pull")? + .success(); + if git_pull_result { + eprintln!("git pull completed"); + } else { + return Err(anyhow!("failed to complete git pull")); + } + + trace!("push"); + let git_push_result = process::Command::new("git") + .args(args("push".to_owned())) + .current_dir(config_dir) + .status() + .context("error while executing git push")? + .success(); + if git_push_result { + eprintln!("git push completed"); + } else { + return Err(anyhow!("failed to complete git push")); + } + Ok(()) +} + fn cmd_sync_custom( config_dir: &PathBuf, remote_name: Option, diff --git a/tests/cli.rs b/tests/cli.rs index b282410..5563ec9 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -5,8 +5,8 @@ mod integrated_test { path, }; - use anyhow::{anyhow, Context, Ok, Result}; - use assert_cmd::{assert::OutputAssertExt, Command}; + use anyhow::{Context, Ok, Result, anyhow}; + use assert_cmd::{Command, assert::OutputAssertExt}; use git2::Repository; use log::{debug, trace}; use predicates::{boolean::PredicateBooleanExt, prelude::predicate}; @@ -73,13 +73,22 @@ mod integrated_test { Ok(()) } - fn run_sync_cmd(config_dir: &path::Path) -> Result<()> { - Command::cargo_bin("xdbm")? - .arg("-c") - .arg(config_dir) - .args(["sync", "-vvvv"]) - .assert() - .success(); + fn run_sync_cmd(config_dir: &path::Path, use_cl: bool) -> Result<()> { + if use_cl { + Command::cargo_bin("xdbm")? + .arg("-c") + .arg(config_dir) + .args(["sync", "-vvvv", "-u"]) + .assert() + .success(); + } else { + Command::cargo_bin("xdbm")? + .arg("-c") + .arg(config_dir) + .args(["sync", "-vvvv"]) + .assert() + .success(); + } Ok(()) } @@ -391,8 +400,8 @@ mod integrated_test { std::fs::read_to_string(config_dir_1.join("storages.yml"))?.contains("parent: gdrive1") ); - run_sync_cmd(&config_dir_1)?; - run_sync_cmd(&config_dir_2)?; + run_sync_cmd(&config_dir_1, false)?; + run_sync_cmd(&config_dir_2, false)?; // bind // @@ -606,8 +615,8 @@ mod integrated_test { .and(predicate::str::contains("foodoc").not()), ); - run_sync_cmd(&config_dir_2)?; - run_sync_cmd(&config_dir_1)?; + run_sync_cmd(&config_dir_2, true)?; + run_sync_cmd(&config_dir_1, true)?; // bind // @@ -722,8 +731,8 @@ mod integrated_test { .assert() .success(); - run_sync_cmd(&config_dir_1)?; - run_sync_cmd(&config_dir_2)?; + run_sync_cmd(&config_dir_1, false)?; + run_sync_cmd(&config_dir_2, false)?; // backup add //