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
use crate::{
database::{self, db},
get_slug,
models::{Icon, Repo},
modify_gitignore, set, write, CACHE_DIR,
};
use diesel::RunQueryDsl;
use futures::future;
use parking_lot::RwLock;
use repo_icons::RepoIcons;
use reqwest::Client;
use site_icons::IconInfo;
use std::{
collections::hash_map::DefaultHasher,
error::Error,
hash::{Hash, Hasher},
io::{BufRead, BufReader, Cursor},
process::{Command, Stdio},
};
use std::{process::exit, sync::Arc};
use tokio::{fs::File, io::copy, task::JoinHandle};
const LIMIT: i32 = 5;
pub async fn sync_all(token: Option<&str>, debug: bool, limit: bool) -> Result<(), Box<dyn Error>> {
let home_dir = home::home_dir().unwrap();
let home_dir = home_dir.to_str().unwrap();
let mut cmd = Command::new("find")
.args([
home_dir,
"-path",
&format!("{}/.Trash", home_dir),
"-prune",
"-o",
"-path",
&format!("{}/Library", home_dir),
"-prune",
"-o",
"-type",
"d",
"-name",
".git",
"-exec",
"echo",
"{}",
";",
])
.stderr(Stdio::inherit())
.stdout(Stdio::piped())
.spawn()?;
let stdout = cmd.stdout.as_mut().unwrap();
let stdout_reader = BufReader::new(stdout);
let stdout_lines = stdout_reader.lines();
let repo_paths = stdout_lines.filter_map(|repo_path| {
let repo_path = repo_path.unwrap();
let repo_path = match repo_path.strip_suffix("/.git") {
Some(repo_path) => repo_path.to_string(),
None => repo_path,
};
if repo_path
.split("/")
.find(|path| path.starts_with("."))
.is_some()
{
None
} else {
Some(repo_path)
}
});
let tasks: Arc<RwLock<Vec<JoinHandle<()>>>> = Arc::new(RwLock::new(Vec::new()));
let amount = Arc::new(RwLock::new(0));
for repo_path in repo_paths {
let token = token.map(|token| token.to_string());
let amount = amount.clone();
let tasks2 = tasks.clone();
let task = tokio::spawn(async move {
if limit && amount.read().clone() == LIMIT {
return;
}
let mut cmd = tokio::process::Command::new(std::env::current_exe().unwrap());
cmd.args(["sync", &repo_path]);
if debug {
cmd.arg("--debug");
}
if let Some(token) = token {
cmd.args(["--token", &token]);
}
let output = cmd.output().await.unwrap();
let error = String::from_utf8(output.stderr).unwrap();
println!("{}", &repo_path);
match &error[..] {
"" => {
*amount.write() += 1;
let amount = amount.read().clone();
if limit && amount == LIMIT {
println!("Limit of {amount} reached! Get the app https://samddenty.gumroad.com/l/github-icons for unlimited sync and custom icon picker!");
for task in tasks2.read().iter() {
task.abort();
}
}
}
error => eprint!("{}", error),
}
if !output.status.success() {
exit(1);
}
});
tasks.write().push(task);
}
future::join_all(tasks.write().iter_mut()).await;
Ok(())
}
pub async fn sync(slug_or_path: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
modify_gitignore::modify()?;
let (user, repo_name, repo_path) = get_slug(slug_or_path)?;
let icons = RepoIcons::fetch("https://github-icons.com", &user, &repo_name).await;
if let Ok(icons) = icons {
let mut tasks: Vec<_> = icons
.into_iter()
.enumerate()
.map(|(i, icon)| -> JoinHandle<Option<()>> {
let slug_or_path = slug_or_path.to_string();
let (user, repo_name) = (user.clone(), repo_name.clone());
tokio::spawn(async move {
let cache_name = format!("{}{}", icon.url.host_str().unwrap_or(""), icon.url.path())
.replace("/", "-")
.replace(":", "-");
let mut hasher = DefaultHasher::new();
cache_name.hash(&mut hasher);
let icon_name = format!(
"{}.{}",
hasher.finish().to_string(),
match icon.info {
IconInfo::PNG { .. } => "png",
IconInfo::JPEG { .. } => "jpg",
IconInfo::ICO { .. } => "ico",
IconInfo::SVG => "svg",
}
);
let icon_path = CACHE_DIR.join(icon_name.clone());
if !icon_path.exists() {
let mut icon_file = File::create(&icon_path).await.ok()?;
match icon.url.scheme() {
"data" => {
let data_uri_path = icon.url.path();
let data_index = data_uri_path.find(",").unwrap_or(0);
let type_index = data_uri_path[..data_index].find(";");
let data = data_uri_path[(data_index + 1)..].to_string();
let mut written = false;
if let Some(type_index) = type_index {
let data_type = data_uri_path[(type_index + 1)..data_index].to_string();
if data_type == "base64" {
let mut content = Cursor::new(base64::decode(&data).unwrap_or(Vec::new()));
copy(&mut content, &mut icon_file).await.ok()?;
written = true;
}
}
if !written {
let mut content = Cursor::new(urlencoding::decode_binary(&data.as_bytes()));
copy(&mut content, &mut icon_file).await.ok()?;
}
}
_ => {
let response = Client::new()
.get(icon.url)
.headers((&icon.headers).try_into().unwrap())
.send()
.await
.ok()?;
let mut content = Cursor::new(response.bytes().await.ok()?);
copy(&mut content, &mut icon_file).await.ok()?;
}
}
}
if i == 0 {
set(&slug_or_path, &icon_name, false).await.ok()?;
}
let icon = Icon {
owner: user,
repo: repo_name,
path: icon_name,
};
{
use database::schema::icons::dsl::*;
diesel::insert_or_ignore_into(icons)
.values(&icon)
.execute(db())
.ok()?;
}
Some(())
})
})
.collect();
while !tasks.is_empty() {
match future::select_all(tasks).await {
(Ok(_), _index, remaining) => {
tasks = remaining;
}
(Err(error), _index, remaining) => {
eprintln!("{:?}", error);
tasks = remaining;
}
}
}
} else {
let error = format!("{:?}", icons);
if error.contains("403") {
eprintln!("Error: Rate limited, please provide a token");
exit(1);
} else {
eprintln!("{}", error);
}
if let Some(repo_path) = repo_path {
let new_repo = Repo {
owner: user,
repo: repo_name,
path: repo_path,
icon_path: None,
};
{
use database::schema::repos::dsl::*;
diesel::insert_or_ignore_into(repos)
.values(&new_repo)
.execute(db())?;
}
}
}
write(&slug_or_path).await?;
Ok(())
}