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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use failure::{Error, ResultExt};
use std::io::Write;
use std::path::Path;
use config::Config;
use github::GitHub;
use gitlab_provider::GitLab;
use {Provider, Repo};
#[derive(Debug, Clone)]
pub struct Driver {
config: Config,
}
impl Driver {
pub fn with_config(config: Config) -> Driver {
Driver { config }
}
pub fn run(&self) -> Result<(), Error> {
info!("Starting repository backup");
let providers = get_providers(&self.config)?;
let repos = self.get_repos_from_providers(&providers)?;
self.update_repos(&repos)?;
info!("Finished repository backup");
Ok(())
}
pub fn update_repos(&self, repos: &[Repo]) -> Result<(), UpdateFailure> {
info!("Updating repositories");
let mut errors = Vec::new();
for repo in repos {
if let Err(e) = self.update_repo(repo) {
warn!("Updating {} failed, {}", repo.name, e);
errors.push((repo.clone(), e));
}
if errors.len() >= 10 {
error!("Too many errors, bailing...");
break;
}
}
if errors.is_empty() {
Ok(())
} else {
Err(UpdateFailure { errors })
}
}
fn update_repo(&self, repo: &Repo) -> Result<(), Error> {
let dest_dir = self.config.general.dest_dir.join(repo.full_name());
if dest_dir.exists() {
debug!("Fetching updates for {}", repo.full_name());
fetch_updates(&dest_dir)?;
} else {
debug!("Cloning into {}", dest_dir.display());
clone_repo(&dest_dir, repo)?;
}
Ok(())
}
pub fn get_repos_from_providers(
&self,
providers: &[Box<Provider>],
) -> Result<Vec<Repo>, Error> {
let mut repos = Vec::new();
for provider in providers {
info!("Fetching repositories from {}", provider.name());
let found = provider
.repositories()
.context("Unable to fetch repositories")?;
info!("Found {} repos from {}", found.len(), provider.name());
repos.extend(found);
}
Ok(repos)
}
}
fn clone_repo(dest_dir: &Path, repo: &Repo) -> Result<(), Error> {
cmd!("git clone --quiet {} {}", &repo.url, dest_dir.display())
}
fn fetch_updates(dest_dir: &Path) -> Result<(), Error> {
cmd!(in dest_dir; "git pull --ff-only --prune --quiet --recurse-submodules")
}
#[derive(Debug, Fail)]
#[fail(display = "One or more errors ecountered while updating repos")]
pub struct UpdateFailure {
errors: Vec<(Repo, Error)>,
}
impl UpdateFailure {
pub fn display<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
writeln!(
writer,
"There were {} errors updating repositories",
self.errors.len()
)?;
for &(ref repo, ref err) in &self.errors {
writeln!(
writer,
"Error: {} failed with {}",
repo.full_name(),
err
)?;
for cause in err.iter_causes() {
writeln!(writer, "\tCaused By: {}", cause)?;
}
}
Ok(())
}
}
fn get_providers(cfg: &Config) -> Result<Vec<Box<Provider>>, Error> {
let mut providers: Vec<Box<Provider>> = Vec::new();
if let Some(gh_config) = cfg.github.as_ref() {
let gh = GitHub::with_config(gh_config.clone());
providers.push(Box::new(gh));
}
if let Some(gl_config) = cfg.gitlab.as_ref() {
let gl = GitLab::with_config(gl_config.clone())?;
providers.push(Box::new(gl));
}
if providers.is_empty() {
warn!("No providers found");
}
Ok(providers)
}