kermit_bench/
downloader.rs1use std::{env, path::PathBuf};
2
3pub struct Downloader;
4
5pub enum DownloadMethod {
6 CLONE,
7}
8
9pub struct DownloadSpec {
10 pub name: &'static str,
11 pub url: &'static str,
12 pub method: DownloadMethod,
13}
14
15impl Downloader {
16 fn tmp_dir() -> std::path::PathBuf { env::temp_dir().join("kermit") }
17
18 fn ensure_init() {
19 let tmp_dir = Self::tmp_dir();
20 if !tmp_dir.exists() {
21 std::fs::create_dir_all(&tmp_dir).expect("Failed to create temporary directory");
22 }
23 }
24
25 pub fn cleanall() {
26 let tmp_dir = Self::tmp_dir();
27 if tmp_dir.exists() {
28 std::fs::remove_dir_all(&tmp_dir).expect("Failed to clean up temporary directory");
29 }
30 }
31
32 pub fn clean(spec: &DownloadSpec) {
33 let dest = Self::tmp_dir().join(spec.name);
34 if dest.exists() {
35 std::fs::remove_dir_all(&dest).expect("Failed to clean up temporary directory");
36 }
37 }
38
39 pub fn download(spec: &DownloadSpec) -> Result<PathBuf, Box<dyn std::error::Error>> {
40 Self::ensure_init();
41 let dest = Self::tmp_dir().join(spec.name);
42 if dest.exists() {
43 return Ok(dest.to_path_buf());
44 }
45 match spec.method {
46 | DownloadMethod::CLONE => {
47 let status = std::process::Command::new("git")
48 .args(["clone", spec.url, dest.to_str().unwrap()])
49 .status()?;
50 if !status.success() {
51 Self::clean(spec);
52 return Err(format!("Git clone failed with status: {}", status).into());
53 }
54 },
55 }
56 Ok(dest.to_path_buf())
57 }
58}