1use crate::{List, ReadJson, Search};
2use git2::Error as GitError;
3use git2::Repository;
4use serde_derive::{Deserialize, Serialize};
5use serde_json::from_str;
6use std::fs::read_to_string;
7use std::io::Error;
8
9#[derive(Debug, Clone, Deserialize, Serialize)]
10pub struct Clone {
11 pub repo: String,
12}
13impl List for Clone{
14 fn list(item: &Vec<Self>) where Self: Sized {
15 println!("//// AVAILABLE REPOS");
16 for i in item{
17 println!("// {}", i.repo)
18 }
19 }
20}
21
22impl ReadJson for Clone {
23 fn read_json(path: &str) -> Result<Vec<Self>, Error>
24 where
25 Self: Sized,
26 {
27 let s = read_to_string(path)?;
28 let v: Vec<Self> = from_str(&s).unwrap();
29 return Ok(v);
30 }
31}
32
33impl Search for Clone {
34 fn search(to_check: &str, object: &Vec<Self>) -> Result<Self, String>
35 where
36 Self: Sized,
37 {
38 for i in object {
39 if i.repo == to_check {
40 return Ok(i.to_owned());
41 }
42 }
43 let s = format!("Couldn't find repo with name: {}", to_check);
44 return Err(s);
45 }
46}
47
48impl Clone {
49 pub fn clone_repo(&self, path: Option<String>) -> Result<(), GitError> {
50 let path = match path {
51 Some(s) => s,
52 None => "".to_owned(),
53 };
54 let url = format!("http://github.com/MKProj/{}.git", &self.repo);
55 Repository::clone(&url, path)?;
56 Ok(())
57 }
58}