]> git.r.bdr.sh - rbdr/blog/blame - src/remote/git.rs
Set rust
[rbdr/blog] / src / remote / git.rs
CommitLineData
50f53dc4 1use std::io::{Error, ErrorKind::Other, Result};
172f4c88 2use std::path::PathBuf;
50f53dc4 3use std::process::{Command, Stdio};
172f4c88
RBR
4use std::time::{SystemTime, UNIX_EPOCH};
5
6pub struct Git;
7
8impl Git {
9 pub fn new() -> Self {
10 Git
11 }
12}
13
14impl 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)
50f53dc4 22 .map_err(|_| Error::new(Other, "Invalid time"))?
172f4c88
RBR
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)
50f53dc4
RBR
36 .stdout(Stdio::null())
37 .stderr(Stdio::null())
172f4c88 38 .status()
50f53dc4 39 .map_err(|_| Error::new(Other, "Failed while performing sync up with git"))?;
172f4c88
RBR
40 }
41
42 Ok(())
43 }
44
45 fn sync_down(&self, remote: &str, directory: &PathBuf) -> Result<()> {
46 let commands = vec![
47 format!("cd {} && git init -b main", directory.display()),
48 format!("cd {} && git checkout .", directory.display()),
49 format!("cd {} && git clean . -f", directory.display()),
50 format!("cd {} && git pull {} main", directory.display(), remote),
51 ];
52
53 for command in commands {
54 Command::new("sh")
55 .arg("-c")
56 .arg(&command)
50f53dc4
RBR
57 .stdout(Stdio::null())
58 .stderr(Stdio::null())
172f4c88 59 .status()
50f53dc4 60 .map_err(|_| Error::new(Other, "Failed while performing sync down with git"))?;
172f4c88
RBR
61 }
62 Ok(())
63 }
64}