1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use anyhow::Result;
use git2::Repository;
use std::{env, fs};
use walkdir::WalkDir;
pub struct WorkHub {
pub root: String,
}
impl WorkHub {
pub fn new() -> Result<Self> {
let wh_root = "WHROOT";
let wh_root_path = match env::var(wh_root) {
Ok(path) => path,
Err(_) => {
let home = "HOME";
let path = env::var(home)?;
path + "/wh"
}
};
Ok(Self { root: wh_root_path })
}
pub fn list(self) -> Result<Vec<String>> {
let mut workdir_list: Vec<String> = vec![];
let whroot_slash = self.root + "/";
WalkDir::new(&whroot_slash)
.min_depth(1)
.max_depth(4)
.into_iter()
.filter_map(|a| a.ok())
.filter(|b| b.file_type().is_dir())
.filter(|d| d.path().ends_with(".git"))
.for_each(|e| {
workdir_list.push(
e.path()
.strip_prefix(&whroot_slash)
.unwrap()
.parent()
.unwrap()
.display()
.to_string(),
)
});
Ok(workdir_list)
}
pub fn create(self, targetdir_path: &str) -> Result<()> {
let targetdir_full_path = self.root + "/" + &targetdir_path.to_string();
fs::create_dir(targetdir_full_path)?;
Ok(())
}
pub fn get(self, repository_path: &str) -> Result<()> {
let repository_vec: Vec<&str> = repository_path.split('/').collect();
if repository_vec.len() != 3 {
panic!("Invalid input. Make sure that the input value satisfies git_repo_manager/author/project");
}
let git_repo_url = String::from("https://") + repository_path;
let targetdir_full_path = self.root + "/" + repository_path;
if let Err(e) = Repository::clone(&git_repo_url, &targetdir_full_path) {
panic!("failed to clone: {}", e);
};
Ok(())
}
}