aboutsummaryrefslogtreecommitdiff
path: root/src/remote/git.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2024-03-09 14:17:34 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2024-03-09 14:17:34 +0100
commit172f4c8807d44ebe38c7f227b7fdc2d6a9dbe323 (patch)
treeb880761cf254fbc668cea0d577d5331a28af67cc /src/remote/git.rs
parent36a4680d18de012e2e5c732f9db161dafa884344 (diff)
Allow sync up and down
Diffstat (limited to 'src/remote/git.rs')
-rw-r--r--src/remote/git.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/remote/git.rs b/src/remote/git.rs
new file mode 100644
index 0000000..a4c1c28
--- /dev/null
+++ b/src/remote/git.rs
@@ -0,0 +1,60 @@
+use std::io::Result;
+use std::path::PathBuf;
+use std::process::Command;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+pub struct Git;
+
+impl Git {
+ pub fn new() -> Self {
+ Git
+ }
+}
+
+impl super::Remote for Git {
+ fn can_handle(&self, _: &str) -> bool {
+ // If we ever implement another remote we will want a check strategy
+ true
+ }
+
+ fn sync_up(&self, remote: &str, directory: &PathBuf) -> Result<()> {
+ let timestamp = SystemTime::now().duration_since(UNIX_EPOCH)
+ .expect("Invalid time")
+ .as_millis();
+
+ let commands = vec![
+ format!("cd {} && git init -b main", directory.display()),
+ format!("cd {} && git add .", directory.display()),
+ format!("cd {} && git commit --allow-empty -m blog-sync-up-{}", directory.display(), timestamp),
+ format!("cd {} && git push {} main --force", directory.display(), remote),
+ ];
+
+ for command in commands {
+ Command::new("sh")
+ .arg("-c")
+ .arg(&command)
+ .status()
+ .expect("Failed while performing sync up with git");
+ }
+
+ Ok(())
+ }
+
+ fn sync_down(&self, remote: &str, directory: &PathBuf) -> Result<()> {
+ let commands = vec![
+ format!("cd {} && git init -b main", directory.display()),
+ format!("cd {} && git checkout .", directory.display()),
+ format!("cd {} && git clean . -f", directory.display()),
+ format!("cd {} && git pull {} main", directory.display(), remote),
+ ];
+
+ for command in commands {
+ Command::new("sh")
+ .arg("-c")
+ .arg(&command)
+ .status()
+ .expect("Failed while performing sync down with git");
+ }
+ Ok(())
+ }
+}