From b1de728eb93f9a2476e74dd2c0f3601e6a3dbb55 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 21 Sep 2024 08:13:11 +0000 Subject: [PATCH] git begin --- src/git.rs | 39 +++++++++++++++++++++++++++++++++------ src/paths.rs | 6 +++--- src/storage.rs | 3 +-- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/git.rs b/src/git.rs index 41f3639..2656e62 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,5 +1,6 @@ -use std::process::Command; +use crate::paths; +use std::process::Command; use std::io; @@ -7,12 +8,38 @@ pub struct Git { } impl Git { - pub fn commit(db_path: String) -> io::Result<()> { - // TODO: impllement - // Command::new("git") - // .arg("add") + pub fn sync() -> io::Result<()> { + let sp = paths::get_storage_path()?; + Command::new("git") + .arg("add") + .arg(paths::PATH_DB) + .current_dir(&sp) + .status() + .map_err(|e| io::Error::new( + io::ErrorKind::Other, + format!("Failed to execute git add: {}", e) + ))?; + Command::new("git") + .arg("commit") + .arg("-m") + .arg("\"sync\"") + .current_dir(&sp) + .status() + .map_err(|e| io::Error::new( + io::ErrorKind::Other, + format!("Failed to execute git commit: {}", e) + ))?; + + Command::new("git") + .arg("push") + .arg(paths::PATH_DB) + .current_dir(&sp) + .status() + .map_err(|e| io::Error::new( + io::ErrorKind::Other, + format!("Failed to execute git push: {}", e) + ))?; Ok(()) - } } diff --git a/src/paths.rs b/src/paths.rs index d5975da..c0af9e9 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -2,10 +2,10 @@ use std::env; use std::io; -static ENV_MPS_HOME: &str = "MPS_HOME"; +pub static ENV_MPS_HOME: &str = "MPS_HOME"; -static PATH_STORAGE: &str = "storage"; // should be under git -static PATH_DB: &str = "db.mps"; +pub static PATH_STORAGE: &str = "storage"; // should be under git +pub static PATH_DB: &str = "db.mps"; pub fn get_storage_path() -> io::Result { diff --git a/src/storage.rs b/src/storage.rs index 3d578d1..8d7bdf1 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -3,7 +3,6 @@ use crate::encoder; use crate::git; use encoder::Encoder; -use git::Git; use std::collections::HashSet; use std::hash::{Hash, Hasher}; @@ -202,7 +201,7 @@ impl Storage { let content = self.encoder.encrypt(&item.content)?; writeln!(file, "{}", content)?; } - // TODO: run git commit + git::Git::sync()?; Ok(()) } }