(WIP) implement sync

This commit is contained in:
qwjyh 2024-03-13 05:36:37 +09:00
parent 498dd2db9b
commit d1f7a4787e
3 changed files with 28 additions and 4 deletions

View file

@ -45,7 +45,10 @@ pub(crate) enum Commands {
Path {},
/// Sync with git repo.
Sync {},
Sync {
/// Remote name to sync.
remote_name: Option<String>,
},
/// Check config files.
Check {},

22
src/cmd_sync.rs Normal file
View file

@ -0,0 +1,22 @@
use std::path::PathBuf;
use anyhow::{anyhow, Result};
use git2::Repository;
pub(crate) fn cmd_sync(config_dir: &PathBuf, remote_name: Option<String>) -> Result<()> {
warn!("Experimental");
let repo = Repository::open(config_dir)?;
let remote_name = match remote_name {
Some(remote_name) => remote_name,
None => {
let remotes = repo.remotes()?;
if remotes.len() != 1 {
return Err(anyhow!("Please specify remote name"));
}
remotes.get(0).unwrap().to_string()
}
};
let mut remote = repo.find_remote(&remote_name)?;
remote.push(&[] as &[&str], None)?;
Ok(())
}

View file

@ -33,6 +33,7 @@ use devices::{Device, DEVICESFILE, *};
mod cmd_args;
mod cmd_init;
mod cmd_storage;
mod cmd_sync;
mod devices;
mod inquire_filepath_completer;
mod storages;
@ -91,9 +92,7 @@ fn main() -> Result<()> {
Commands::Path {} => {
println!("{}", &config_dir.display());
}
Commands::Sync {} => {
unimplemented!("Sync is not implemented")
}
Commands::Sync { remote_name } => cmd_sync::cmd_sync(&config_dir, remote_name)?,
Commands::Check {} => {
println!("Config dir: {}", &config_dir.display());
let _storages = Storages::read(&config_dir)?;