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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::collections::BTreeMap;
use std::path::Path;
use remove_empty_subdirs::remove_empty_subdirs;
use metadata::{self, Metadata};
use util;
use vcs;
pub struct Manager {
root_dir: &'static str,
metadata: Metadata,
}
impl Manager {
pub fn new() -> Result<Self, String> {
let root_path = Path::new(".");
let root_dir = root_path.to_str().unwrap();
let md_file = "Repos.toml";
let md_path = Path::new(&md_file);
match metadata::load(&md_path) {
Ok(md) => {
let manager = Manager {
root_dir: root_dir,
metadata: md,
};
Ok(manager)
}
Err(err) => Err(format!(
"Couldn't load metadata because of: {}",
err.to_string()
)),
}
}
fn _sync(&self, url: &str, repo: &metadata::Repo) {
if !repo.allow_sync {
println!(
"No need to sync respository '{}' because of disallowed.",
url
);
return ();
}
let local_relpath = util::repo_url_to_relpath(url).unwrap();
let relpath = Path::new(&local_relpath);
let _vcs = &repo.vcs;
let bare = repo.bare;
let use_proxy = repo.use_proxy;
let proxy = match use_proxy {
true => Some(&self.metadata.proxy),
false => None,
};
if relpath.is_dir() {
vcs::update(_vcs, &relpath, bare, proxy);
} else {
vcs::clone(_vcs, &url, &relpath, bare, proxy);
}
}
pub fn sync(&self, url: &str) {
let repositories = &self.metadata.repos;
if repositories.contains_key(url) {
let repo = repositories.get(url).unwrap();
if let Some(alternative_url) = util::gen_alternative_url(&repo.vcs, url) {
if repositories.contains_key(&alternative_url) {
println!(
"Warning: repository with alternative url '{}' exists already.",
alternative_url
);
return ();
}
}
self._sync(&url, &repo);
} else {
println!("Repository '{}' has not been put in metadata yet.", url);
}
}
pub fn sync_all(&self) {
for (url, repo) in &self.metadata.repos {
println!("Sync repository '{}'...", &url);
self._sync(&url, &repo);
}
}
pub fn remove(&self, url: &str) {
let repositories = &self.metadata.repos;
if repositories.contains_key(url) {
let local_relpath = util::repo_url_to_relpath(url).unwrap();
let relpath = Path::new(&local_relpath);
util::delete_repo_relpath(relpath);
} else {
println!("Repository '{}' has not been put in metadata yet.", url);
}
}
pub fn topics(&self) {
let mut topic_repo_counts: BTreeMap<&str, i32> = BTreeMap::new();
for (_, repo) in &self.metadata.repos {
let topics = &repo.topics;
for topic in topics {
let counter = topic_repo_counts.entry(&topic).or_insert(0);
*counter += 1;
}
}
let topics_count = topic_repo_counts.len();
println!("There are {} topics:", topics_count);
for (topic, counter) in &topic_repo_counts {
println!("* {}: {} repositories", &topic, counter);
}
}
pub fn topic(&self, _topic: &str) {
let mut urls = Vec::new();
for (url, repo) in &self.metadata.repos {
let topics = &repo.topics;
if topics.contains(&_topic.to_string()) {
urls.push(url);
}
}
println!("Topic '{}' has {} repositories:", _topic, urls.len());
urls.sort_unstable();
for url in urls {
println!("* {}", url)
}
}
pub fn stats(&self) {
let mut repositories_count = 0;
let mut bare_repo_count = 0;
let mut sync_allowed_repo_count = 0;
let mut synced_repo_count = 0;
let mut proxy_used_repo_count = 0;
let mut vcs_repo_counts: BTreeMap<&str, i32> = BTreeMap::new();
let mut topic_repo_counts: BTreeMap<&str, i32> = BTreeMap::new();
let mut server_repo_counts: BTreeMap<String, i32> = BTreeMap::new();
for (url, repo) in &self.metadata.repos {
repositories_count += 1;
if repo.bare {
bare_repo_count += 1;
}
if repo.allow_sync {
sync_allowed_repo_count += 1;
}
if repo.use_proxy {
proxy_used_repo_count += 1;
}
let local_relpath = util::repo_url_to_relpath(url).unwrap();
let relpath = Path::new(&local_relpath);
if relpath.is_dir() {
synced_repo_count += 1;
}
let vcs = &repo.vcs;
let vcs_counter = vcs_repo_counts.entry(&vcs).or_insert(0);
*vcs_counter += 1;
let topics = &repo.topics;
for topic in topics {
let counter = topic_repo_counts.entry(&topic).or_insert(0);
*counter += 1;
}
let server = util::repo_server_from_url(&url).unwrap();
let server_counter = server_repo_counts.entry(server).or_insert(0);
*server_counter += 1;
}
println!("There are {} repositories:", repositories_count);
println!("* Bare: {} repositories", bare_repo_count);
println!("* Sync allowed: {} repositories", sync_allowed_repo_count);
println!("* Proxy used: {} repositories", proxy_used_repo_count);
println!("* Synced: {} repositories", synced_repo_count);
let vcs_count = vcs_repo_counts.len();
println!("There are {} vcs:", vcs_count);
for (vcs, counter) in &vcs_repo_counts {
println!("* {}: {} repositories", &vcs, counter);
}
let topics_count = topic_repo_counts.len();
println!("There are {} topics:", topics_count);
for (topic, counter) in &topic_repo_counts {
println!("* {}: {} repositories", &topic, counter);
}
let servers_count = server_repo_counts.len();
println!("There are {} servers:", servers_count);
for (server, counter) in &server_repo_counts {
println!("* {}: {} repositories", server, counter);
}
}
pub fn cleanup(&self) {
let root_path = Path::new(self.root_dir);
match remove_empty_subdirs(root_path) {
Ok(()) => {}
Err(err) => println!("Unexpected error: {}", err.to_string()),
}
}
pub fn search(&self, keyword: &str) {
let mut urls_by_url = Vec::new();
let mut urls_by_topic: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
for (url, repo) in &self.metadata.repos {
if url.contains(&keyword) {
urls_by_url.push(url);
}
let topics = &repo.topics;
for topic in topics {
if topic.contains(&keyword) {
let mut _topics = urls_by_topic.entry(&url).or_insert(Vec::new());
_topics.push(topic);
}
}
}
urls_by_url.sort_unstable();
println!(
"There are {} repositories matched by url:",
urls_by_url.len()
);
for url in urls_by_url {
println!("* {}", &url);
}
println!(
"There are {} repositories matched by topic:",
urls_by_topic.len()
);
for (url, topics) in &urls_by_topic {
println!("* {} ({})", &url, topics.join(", "));
}
}
pub fn proxy(&self) {
let proxy = &self.metadata.proxy;
println!("Proxy configuration:");
println!("* scheme: {}", proxy.scheme);
println!("* host: {}", proxy.host);
println!("* port: {}", proxy.port);
}
}