new(sync): add option to use git cli

This commit is contained in:
qwjyh 2025-03-02 01:03:49 +09:00
parent f98ea1afec
commit 0412233f86
2 changed files with 72 additions and 16 deletions

View file

@ -1,6 +1,7 @@
use std::{ use std::{
io::{self, Write}, io::{self, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
process,
}; };
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result, anyhow};
@ -14,12 +15,58 @@ pub(crate) fn cmd_sync(
use_cl: bool, use_cl: bool,
) -> Result<()> { ) -> Result<()> {
if use_cl { if use_cl {
todo!("do here next") cmd_sync_cl(config_dir, remote_name, ssh_key)
} else { } else {
cmd_sync_custom(config_dir, remote_name, use_sshagent, ssh_key) cmd_sync_custom(config_dir, remote_name, use_sshagent, ssh_key)
} }
} }
fn cmd_sync_cl(
config_dir: &PathBuf,
remote_name: Option<String>,
ssh_key: Option<PathBuf>,
) -> 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( fn cmd_sync_custom(
config_dir: &PathBuf, config_dir: &PathBuf,
remote_name: Option<String>, remote_name: Option<String>,

View file

@ -5,8 +5,8 @@ mod integrated_test {
path, path,
}; };
use anyhow::{anyhow, Context, Ok, Result}; use anyhow::{Context, Ok, Result, anyhow};
use assert_cmd::{assert::OutputAssertExt, Command}; use assert_cmd::{Command, assert::OutputAssertExt};
use git2::Repository; use git2::Repository;
use log::{debug, trace}; use log::{debug, trace};
use predicates::{boolean::PredicateBooleanExt, prelude::predicate}; use predicates::{boolean::PredicateBooleanExt, prelude::predicate};
@ -73,13 +73,22 @@ mod integrated_test {
Ok(()) Ok(())
} }
fn run_sync_cmd(config_dir: &path::Path) -> Result<()> { fn run_sync_cmd(config_dir: &path::Path, use_cl: bool) -> Result<()> {
Command::cargo_bin("xdbm")? if use_cl {
.arg("-c") Command::cargo_bin("xdbm")?
.arg(config_dir) .arg("-c")
.args(["sync", "-vvvv"]) .arg(config_dir)
.assert() .args(["sync", "-vvvv", "-u"])
.success(); .assert()
.success();
} else {
Command::cargo_bin("xdbm")?
.arg("-c")
.arg(config_dir)
.args(["sync", "-vvvv"])
.assert()
.success();
}
Ok(()) Ok(())
} }
@ -391,8 +400,8 @@ mod integrated_test {
std::fs::read_to_string(config_dir_1.join("storages.yml"))?.contains("parent: gdrive1") 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_1, false)?;
run_sync_cmd(&config_dir_2)?; run_sync_cmd(&config_dir_2, false)?;
// bind // bind
// //
@ -606,8 +615,8 @@ mod integrated_test {
.and(predicate::str::contains("foodoc").not()), .and(predicate::str::contains("foodoc").not()),
); );
run_sync_cmd(&config_dir_2)?; run_sync_cmd(&config_dir_2, true)?;
run_sync_cmd(&config_dir_1)?; run_sync_cmd(&config_dir_1, true)?;
// bind // bind
// //
@ -722,8 +731,8 @@ mod integrated_test {
.assert() .assert()
.success(); .success();
run_sync_cmd(&config_dir_1)?; run_sync_cmd(&config_dir_1, false)?;
run_sync_cmd(&config_dir_2)?; run_sync_cmd(&config_dir_2, false)?;
// backup add // backup add
// //