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
use crate::gitutil::{clone, FetchMessage};
use crate::{repo, Config, Exec, Location};
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(about = "Clone repositories")]
pub struct Opt {
    #[structopt(short, long, default_value = "10", help = "Count of parallel jobs")]
    pub parallel: usize,
    #[structopt(required = true, min_values = 1, help = Location::about())]
    pub targets: Vec<Location>,
    #[structopt(short, long, help = "Show detailed output")]
    pub verbose: bool,
}

impl Exec for Opt {
    fn exec(self, config: Config) -> i32 {
        let mut errors = 0;

        let receiver = clone(
            self.targets
                .iter()
                .map(|location| (location.url(), config.src.join(&location.id())))
                .collect(),
            self.parallel,
        );
        while let Ok(msg) = receiver.recv() {
            match msg {
                FetchMessage::Done((path, res, prog)) => {
                    let id = path
                        .strip_prefix(&config.src)
                        .unwrap()
                        .display()
                        .to_string();
                    println!(
                        "{} {}",
                        id,
                        match res {
                            Ok(_) => "done",
                            Err(err) => {
                                if self.verbose {
                                    println!("{}", err);
                                }
                                errors += 1;
                                repo::remove(&config.bin, &path).ok();
                                "failed"
                            }
                        }
                    );
                    prog.print();
                }
                FetchMessage::Progress(prog) => prog.print(),
            }
        }

        errors
    }
}