eol_deployer/
git_client.rs

1pub struct GitClient;
2
3impl GitClient {
4    pub async fn clone_repository_to_versioned_path(
5        &self,
6        repo_url: &str,
7        tag: &str,
8        base_path: &str,
9    ) -> Result<String, Box<dyn std::error::Error>> {
10        let versioned_path = format!("{}/traefik-config-{}", base_path, tag);
11
12        println!(
13            "Cloning repository {} at tag {} to {}",
14            repo_url, tag, versioned_path
15        );
16
17        // Remove existing directory if it exists
18        if std::path::Path::new(&versioned_path).exists() {
19            std::fs::remove_dir_all(&versioned_path)?;
20        }
21
22        // Create parent directory if it doesn't exist
23        if let Some(parent) = std::path::Path::new(&versioned_path).parent() {
24            std::fs::create_dir_all(parent)?;
25        }
26
27        // Clone the repository
28        let output = std::process::Command::new("git")
29            .args(&[
30                "clone",
31                "--depth",
32                "1",
33                "--branch",
34                tag,
35                repo_url,
36                &versioned_path,
37            ])
38            .output()?;
39
40        if !output.status.success() {
41            return Err(format!(
42                "Git clone failed: {}",
43                String::from_utf8_lossy(&output.stderr)
44            )
45            .into());
46        }
47
48        println!(
49            "Successfully cloned {} at tag {} to {}",
50            repo_url, tag, versioned_path
51        );
52        Ok(versioned_path)
53    }
54
55    pub async fn fetch_latest(&self, repo_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
56        println!("Fetching latest changes in {}", repo_dir);
57
58        let output = std::process::Command::new("git")
59            .args(&["fetch", "--all"])
60            .current_dir(repo_dir)
61            .output()?;
62
63        if !output.status.success() {
64            return Err(format!(
65                "Git fetch failed: {}",
66                String::from_utf8_lossy(&output.stderr)
67            )
68            .into());
69        }
70
71        Ok(())
72    }
73
74    pub async fn checkout_tag(
75        &self,
76        repo_dir: &str,
77        tag: &str,
78    ) -> Result<(), Box<dyn std::error::Error>> {
79        println!("Checking out tag {} in {}", tag, repo_dir);
80
81        let output = std::process::Command::new("git")
82            .args(&["checkout", tag])
83            .current_dir(repo_dir)
84            .output()?;
85
86        if !output.status.success() {
87            return Err(format!(
88                "Git checkout failed: {}",
89                String::from_utf8_lossy(&output.stderr)
90            )
91            .into());
92        }
93
94        println!("Successfully checked out tag {}", tag);
95        Ok(())
96    }
97}