forked from Morganamilo/paru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.rs
151 lines (129 loc) · 4.35 KB
/
sync.rs
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
use crate::config::Config;
use crate::pkgbuild::PkgbuildRepos;
use crate::{exec, print_error};
use std::io::Write;
use anyhow::{anyhow, ensure, Context, Result};
use raur::Raur;
use tr::tr;
pub async fn filter(config: &Config) -> Result<i32> {
let mut cache = raur::Cache::new();
config.raur.cache_info(&mut cache, &config.targets).await?;
for targ in config.targets.iter().filter(|t| cache.contains(t.as_str())) {
println!("{}", targ);
}
if cache.len() == config.targets.len() {
Ok(0)
} else {
Ok(127)
}
}
pub async fn list(config: &Config) -> Result<i32> {
let c = config.color;
let args = config.pacman_args();
let mut ret = 0;
if args.targets.is_empty() {
if config.mode.repo() {
if let Err(e) = exec::pacman(config, &args) {
print_error(c.error, e);
ret = 1
}
}
if config.mode.pkgbuild() {
for repo in &config.pkgbuild_repos.repos {
list_pkgbuilds(config, &config.pkgbuild_repos, &repo.name);
}
}
if config.mode.aur() {
if let Err(e) = list_aur(config).await {
print_error(c.error, e);
ret = 1
}
}
} else {
for &target in &args.targets {
if config.alpm.syncdbs().iter().any(|r| r.name() == target) && config.mode.repo() {
let mut args = args.clone();
args.targets.clear();
args.target(target);
if let Err(e) = exec::pacman(config, &args) {
print_error(c.error, e);
ret = 1;
}
} else if config.pkgbuild_repos.repo(target).is_some() && config.mode.pkgbuild() {
list_pkgbuilds(config, &config.pkgbuild_repos, target);
} else if target == config.aur_namespace() && config.mode.aur() {
if let Err(e) = list_aur(config).await {
print_error(c.error, e);
ret = 1;
}
} else {
print_error(c.error, anyhow!("repository \"{}\" was not found", target));
ret = 1;
}
}
}
Ok(ret)
}
pub fn list_pkgbuilds(config: &Config, repos: &PkgbuildRepos, repo: &str) {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
if let Some(repo) = repos.repo(repo) {
for pkg in repo.pkgs(config) {
for name in pkg.srcinfo.names() {
print_pkg(
config,
&mut stdout,
name.as_bytes(),
&repo.name,
&pkg.srcinfo.version(),
)
}
}
}
}
pub async fn list_aur(config: &Config) -> Result<()> {
let url = config.aur_url.join("packages.gz")?;
let client = config.raur.client();
let resp = client
.get(url.clone())
.send()
.await
.with_context(|| format!("get {}", url))?;
let success = resp.status().is_success();
ensure!(success, "get {}: {}", url, resp.status());
let data = resp.bytes().await?;
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
let mut lines = data
.split(|&c| c == b'\n')
.skip(1)
.filter(|l| !l.is_empty())
.collect::<Vec<_>>();
lines.sort_unstable();
for line in lines {
print_pkg(config, &mut stdout, line, "aur", "unknown-version");
}
Ok(())
}
fn print_pkg(config: &Config, mut stdout: impl Write, line: &[u8], repo: &str, version: &str) {
let cpkg = config.color.sl_pkg;
let crepo = config.color.sl_repo;
let cversion = config.color.sl_version;
let cinstalled = config.color.sl_installed;
if config.args.has_arg("q", "quiet") {
let _ = stdout.write_all(line);
let _ = stdout.write_all(b"\n");
return;
}
let _ = crepo.paint(repo.as_bytes()).write_to(&mut stdout);
let _ = stdout.write_all(b" ");
let _ = cpkg.paint(line).write_to(&mut stdout);
let _ = stdout.write_all(b" ");
let _ = cversion.paint(version.as_bytes()).write_to(&mut stdout);
if config.alpm.localdb().pkg(line).is_ok() {
let _ = cinstalled
.paint(tr!(" [installed]").as_bytes())
.write_to(&mut stdout);
}
let _ = stdout.write_all(b"\n");
}