]> git.r.bdr.sh - rbdr/blog/blob - src/remote/git.rs
a4c1c28d13a1faab79178af9144b0f9e73672814
[rbdr/blog] / src / remote / git.rs
1 use std::io::Result;
2 use std::path::PathBuf;
3 use std::process::Command;
4 use std::time::{SystemTime, UNIX_EPOCH};
5
6 pub struct Git;
7
8 impl Git {
9 pub fn new() -> Self {
10 Git
11 }
12 }
13
14 impl super::Remote for Git {
15 fn can_handle(&self, _: &str) -> bool {
16 // If we ever implement another remote we will want a check strategy
17 true
18 }
19
20 fn sync_up(&self, remote: &str, directory: &PathBuf) -> Result<()> {
21 let timestamp = SystemTime::now().duration_since(UNIX_EPOCH)
22 .expect("Invalid time")
23 .as_millis();
24
25 let commands = vec![
26 format!("cd {} && git init -b main", directory.display()),
27 format!("cd {} && git add .", directory.display()),
28 format!("cd {} && git commit --allow-empty -m blog-sync-up-{}", directory.display(), timestamp),
29 format!("cd {} && git push {} main --force", directory.display(), remote),
30 ];
31
32 for command in commands {
33 Command::new("sh")
34 .arg("-c")
35 .arg(&command)
36 .status()
37 .expect("Failed while performing sync up with git");
38 }
39
40 Ok(())
41 }
42
43 fn sync_down(&self, remote: &str, directory: &PathBuf) -> Result<()> {
44 let commands = vec![
45 format!("cd {} && git init -b main", directory.display()),
46 format!("cd {} && git checkout .", directory.display()),
47 format!("cd {} && git clean . -f", directory.display()),
48 format!("cd {} && git pull {} main", directory.display(), remote),
49 ];
50
51 for command in commands {
52 Command::new("sh")
53 .arg("-c")
54 .arg(&command)
55 .status()
56 .expect("Failed while performing sync down with git");
57 }
58 Ok(())
59 }
60 }