forked from Morganamilo/paru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
1237 lines (1075 loc) · 36.3 KB
/
config.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::args::Args;
use crate::devel::save_devel_info;
use crate::exec::{self, Status};
use crate::fmt::color_repo;
use crate::info::get_terminal_width;
use crate::pkgbuild::PkgbuildRepos;
use crate::util::{get_provider, reopen_stdin};
use crate::{alpm_debug_enabled, help, printtr, repo};
use std::env::consts::ARCH;
use std::env::{remove_var, set_var, var};
use std::fmt;
use std::fs::{remove_file, OpenOptions};
use std::io::{stderr, stdin, stdout, BufRead, IsTerminal};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use alpm::{
AnyDownloadEvent, AnyQuestion, Depend, DownloadEvent, DownloadResult, LogLevel, Question,
};
use ansiterm::Color::{Blue, Cyan, Green, Purple, Red, Yellow};
use ansiterm::Style;
use anyhow::{anyhow, bail, ensure, Context, Error, Result};
use bitflags::bitflags;
use cini::{Callback, CallbackKind, Ini};
use globset::{Glob, GlobSet, GlobSetBuilder};
use tr::tr;
use url::Url;
#[derive(Debug, Default)]
pub struct Alpm {
alpm: Option<alpm::Alpm>,
}
impl std::ops::Deref for Alpm {
type Target = alpm::Alpm;
fn deref(&self) -> &Self::Target {
self.alpm.as_ref().unwrap()
}
}
impl std::ops::DerefMut for Alpm {
fn deref_mut(&mut self) -> &mut Self::Target {
self.alpm.as_mut().unwrap()
}
}
impl Alpm {
fn new(alpm: alpm::Alpm) -> Self {
Self { alpm: Some(alpm) }
}
}
#[derive(Debug, SmartDefault, Clone, PartialEq, Eq)]
pub enum LocalRepos {
#[default]
None,
Default,
Repo(Vec<String>),
}
impl LocalRepos {
pub fn new(repo: Option<&str>) -> Self {
match repo {
Some(s) => LocalRepos::Repo(s.split_whitespace().map(|s| s.to_string()).collect()),
None => LocalRepos::Default,
}
}
}
#[derive(Debug, Default, Copy, Clone)]
pub struct Colors {
pub enabled: bool,
pub field: Style,
pub error: Style,
pub warning: Style,
pub bold: Style,
pub upgrade: Style,
//pub base: Style,
pub action: Style,
pub sl_repo: Style,
pub sl_pkg: Style,
pub sl_version: Style,
pub sl_installed: Style,
//pub ss_repo: Style,
pub ss_name: Style,
pub ss_ver: Style,
pub ss_stats: Style,
pub ss_orphaned: Style,
pub ss_installed: Style,
pub ss_ood: Style,
pub code: Style,
pub news_date: Style,
pub old_version: Style,
pub new_version: Style,
pub install_version: Style,
pub number_menu: Style,
pub group: Style,
pub stats_line_separator: Style,
pub stats_value: Style,
}
impl From<&str> for Colors {
fn from(s: &str) -> Self {
match s {
"auto" if stdout().is_terminal() && stderr().is_terminal() => Colors::new(),
"always" => Colors::new(),
_ => Colors::default(),
}
}
}
impl Colors {
pub fn new() -> Colors {
Colors {
enabled: true,
field: Style::new().bold(),
error: Style::new().fg(Red),
warning: Style::new().fg(Yellow),
bold: Style::new().bold(),
upgrade: Style::new().fg(Green).bold(),
//base: Style::new().fg(Blue),
action: Style::new().fg(Blue).bold(),
sl_repo: Style::new().fg(Purple).bold(),
sl_pkg: Style::new().bold(),
sl_version: Style::new().fg(Green).bold(),
sl_installed: Style::new().fg(Cyan).bold(),
//ss_repo: Style::new().fg(Blue).bold(),
ss_name: Style::new().bold(),
ss_ver: Style::new().fg(Green).bold(),
ss_stats: Style::new().bold(),
ss_orphaned: Style::new().fg(Red).bold(),
ss_installed: Style::new().fg(Cyan).bold(),
ss_ood: Style::new().fg(Red).bold(),
code: Style::new().fg(Cyan),
news_date: Style::new().fg(Cyan).bold(),
old_version: Style::new().fg(Red),
install_version: Style::new().fg(ansiterm::Color::Fixed(243)),
new_version: Style::new().fg(Green),
number_menu: Style::new().fg(Purple),
group: Style::new().fg(Blue).bold(),
stats_line_separator: Style::new().fg(Blue).bold(),
stats_value: Style::new().fg(Cyan),
}
}
}
pub trait ConfigEnum: Sized + PartialEq + Copy + Clone + fmt::Debug + 'static {
const VALUE_LOOKUP: &'static [(&'static str, Self)];
fn as_str(&self) -> &'static str {
Self::VALUE_LOOKUP
.iter()
.find(|(_, v)| self == v)
.map(|(k, _)| k)
.unwrap()
}
fn default_or(self, key: &str, value: Option<&str>) -> Result<Self> {
value.map_or(Ok(self), |value| ConfigEnum::from_str(key, value))
}
fn from_str(key: &str, value: &str) -> Result<Self> {
let val = Self::VALUE_LOOKUP
.iter()
.find(|(name, _)| name == &value)
.map(|(_, res)| *res);
if let Some(val) = val {
Ok(val)
} else {
let okvalues = Self::VALUE_LOOKUP
.iter()
.map(|v| v.0)
.collect::<Vec<&str>>()
.join("|");
bail!(tr!(
"invalid value '{val}' for key '{key}', expected: {exp}",
val = value,
key = key,
exp = okvalues
))
}
}
}
type ConfigEnumValues<T> = &'static [(&'static str, T)];
#[derive(Debug, SmartDefault, PartialEq, Eq)]
pub enum Sign {
#[default]
No,
Yes,
Key(String),
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Op {
#[default]
Default,
ChrootCtl,
Database,
DepTest,
Files,
GetPkgBuild,
Query,
Remove,
RepoCtl,
Show,
Sync,
Upgrade,
Build,
}
impl ConfigEnum for Op {
const VALUE_LOOKUP: ConfigEnumValues<Self> = &[
("chrootctl", Self::ChrootCtl),
("database", Self::Database),
("deptest", Self::DepTest),
("files", Self::Files),
("getpkgbuild", Self::GetPkgBuild),
("query", Self::Query),
("remove", Self::Remove),
("repoctl", Self::RepoCtl),
("show", Self::Show),
("sync", Self::Sync),
("upgrade", Self::Upgrade),
("build", Self::Build),
("default", Self::Default),
];
}
impl fmt::Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortBy {
Base,
BaseId,
Id,
Modified,
Name,
Popularity,
Submitted,
Votes,
}
impl ConfigEnum for SortBy {
const VALUE_LOOKUP: ConfigEnumValues<Self> = &[
("base", Self::Base),
("baseid", Self::BaseId),
("id", Self::Id),
("modified", Self::Modified),
("name", Self::Name),
("popularity", Self::Popularity),
("submitted", Self::Submitted),
("votes", Self::Votes),
];
}
impl ConfigEnum for raur::SearchBy {
const VALUE_LOOKUP: ConfigEnumValues<Self> = &[
("checkdepends", Self::CheckDepends),
("depends", Self::Depends),
("maintainer", Self::Maintainer),
("makedepends", Self::MakeDepends),
("name-desc", Self::NameDesc),
("name", Self::Name),
("submitter", Self::Submitter),
("provides", Self::Provides),
("replaces", Self::Replaces),
("groups", Self::Groups),
("keywords", Self::Keywords),
("comaintainers", Self::CoMaintainers),
];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortMode {
BottomUp,
TopDown,
}
impl ConfigEnum for SortMode {
const VALUE_LOOKUP: ConfigEnumValues<Self> =
&[("bottomup", Self::BottomUp), ("topdown", Self::TopDown)];
}
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Mode: u32 {
const AUR = 1 << 0;
const REPO = 1 << 1;
const PKGBUILD = 1 << 2;
}
}
impl Mode {
pub fn aur(self) -> bool {
self.contains(Self::AUR)
}
pub fn repo(self) -> bool {
self.contains(Self::REPO)
}
pub fn pkgbuild(self) -> bool {
self.contains(Self::PKGBUILD)
}
}
impl FromStr for Mode {
type Err = Error;
fn from_str(input: &str) -> Result<Self> {
let mode = match input {
"all" => Mode::all(),
"aur" => Mode::AUR,
"repo" => Mode::REPO,
"pkgbuilds" => Mode::PKGBUILD,
_ => {
let mut mode = Mode::empty();
for c in input.chars() {
match c {
'a' => mode |= Mode::AUR,
'r' => mode |= Mode::REPO,
'p' => mode |= Mode::PKGBUILD,
_ => bail!(tr!("unknown mode {}", input)),
}
}
mode
}
};
Ok(mode)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum YesNoAll {
Yes,
No,
All,
}
impl ConfigEnum for YesNoAll {
const VALUE_LOOKUP: ConfigEnumValues<Self> =
&[("yes", Self::Yes), ("no", Self::No), ("all", Self::All)];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum YesNoAsk {
Yes,
No,
Ask,
}
impl ConfigEnum for YesNoAsk {
const VALUE_LOOKUP: ConfigEnumValues<Self> =
&[("yes", Self::Yes), ("no", Self::No), ("ask", Self::Ask)];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum YesNoAllTree {
Yes,
No,
All,
Tree,
}
impl ConfigEnum for YesNoAllTree {
const VALUE_LOOKUP: ConfigEnumValues<Self> = &[
("yes", Self::Yes),
("no", Self::No),
("all", Self::All),
("tree", Self::Tree),
];
}
#[derive(SmartDefault, Debug)]
pub struct Config {
section: Option<String>,
pub args: Args<String>,
pub globals: Args<String>,
pub cols: Option<usize>,
pub op: Op,
#[cfg(not(feature = "mock"))]
pub raur: raur::Handle,
#[cfg(feature = "mock")]
pub raur: crate::mock::Mock,
#[default(aur_fetch::Fetch::with_cache_dir(""))]
pub fetch: aur_fetch::Fetch,
pub cache: raur::Cache,
pub need_root: bool,
pub pacman: pacmanconf::Config,
pub alpm: Alpm,
pub color: Colors,
pub targets: Vec<String>,
#[default(Url::parse("https://aur.archlinux.org").unwrap())]
pub aur_url: Url,
pub aur_rpc_url: Option<Url>,
#[default(Url::parse("https://archlinux.org").unwrap())]
pub arch_url: Url,
pub build_dir: PathBuf,
pub cache_dir: PathBuf,
pub state_dir: PathBuf,
pub devel_path: PathBuf,
pub config_path: Option<PathBuf>,
pub news: u32,
pub stats: bool,
pub order: bool,
pub gendb: bool,
#[default(YesNoAll::No)]
pub redownload: YesNoAll,
#[default(YesNoAllTree::No)]
pub rebuild: YesNoAllTree,
#[default(YesNoAsk::No)]
pub remove_make: YesNoAsk,
#[default(SortBy::Votes)]
pub sort_by: SortBy,
#[default(raur::SearchBy::NameDesc)]
pub search_by: raur::SearchBy,
pub limit: usize,
#[default(SortMode::TopDown)]
pub sort_mode: SortMode,
#[default(Mode::empty())]
pub mode: Mode,
pub aur_filter: bool,
pub interactive: bool,
#[default = 7]
pub completion_interval: u64,
pub help: bool,
pub version: bool,
pub skip_review: bool,
pub no_check: bool,
pub no_confirm: bool,
pub devel: bool,
pub clean_after: bool,
#[default(YesNoAll::No)]
pub provides: YesNoAll,
pub pgp_fetch: bool,
pub combined_upgrade: bool,
pub batch_install: bool,
pub use_ask: bool,
pub save_changes: bool,
pub clean: usize,
pub optional: bool,
pub complete: bool,
pub print: bool,
pub news_on_upgrade: bool,
pub comments: usize,
pub ssh: bool,
pub keep_repo_cache: bool,
pub fail_fast: bool,
pub keep_src: bool,
pub sign: Sign,
pub sign_db: Sign,
pub pre_build_command: Option<String>,
#[default = "makepkg"]
pub makepkg_bin: String,
#[default = "pacman"]
pub pacman_bin: String,
pub pacman_conf_bin: Option<String>,
#[default = "git"]
pub git_bin: String,
#[default = "gpg"]
pub gpg_bin: String,
#[default = "sudo"]
pub sudo_bin: String,
#[default = "pkgctl"]
pub pkgctl_bin: String,
#[default = "bat"]
pub bat_bin: String,
pub fm: Option<String>,
pub sudo_loop: Vec<String>,
pub mflags: Vec<String>,
pub git_flags: Vec<String>,
pub gpg_flags: Vec<String>,
pub sudo_flags: Vec<String>,
pub bat_flags: Vec<String>,
pub fm_flags: Vec<String>,
pub chroot_flags: Vec<String>,
pub pager_cmd: Option<String>,
pub devel_suffixes: Vec<String>,
#[default(GlobSet::empty())]
pub no_warn: GlobSet,
#[default(GlobSetBuilder::new())]
pub no_warn_builder: GlobSetBuilder,
pub install_debug: bool,
pub upgrade_menu: bool,
pub makepkg_conf: Option<String>,
pub pacman_conf: Option<String>,
pub repos: LocalRepos,
#[default(Path::new("/var/lib/aurbuild/").join(ARCH))]
pub chroot_dir: PathBuf,
pub chroot: bool,
pub chroot_pkgs: Vec<String>,
pub install: bool,
pub uninstall: bool,
pub sysupgrade: bool,
pub refresh: bool,
pub quiet: bool,
pub list: bool,
pub delete: u32,
pub no_install: bool,
pub env: Vec<(String, String)>,
//pacman
pub db_path: Option<String>,
pub root: Option<String>,
pub verbose: bool,
pub ask: usize,
pub arch: Option<String>,
pub ignore: Vec<String>,
pub ignore_group: Vec<String>,
pub ignore_devel_source: Vec<String>,
#[default(GlobSet::empty())]
pub ignore_devel: GlobSet,
#[default(GlobSetBuilder::new())]
pub ignore_devel_builder: GlobSetBuilder,
pub assume_installed: Vec<String>,
#[default(PkgbuildRepos::new(aur_fetch::Fetch::with_cache_dir("repo")))]
pub pkgbuild_repos: PkgbuildRepos,
}
impl Ini for Config {
type Err = Error;
fn callback(&mut self, cb: Callback) -> Result<(), Self::Err> {
let err = match cb.kind {
CallbackKind::Section(section) => {
self.section = Some(section.to_string());
if !matches!(section, "options" | "bin" | "env")
&& self.pkgbuild_repos.repo(section).is_none()
{
if matches!(section, "local" | "aur" | "pkg" | "base") || section.contains('.')
{
bail!(tr!("section can not be called {}", section));
}
self.pkgbuild_repos.add_repo(section.to_string());
}
Ok(())
}
CallbackKind::Directive(_, key, value) => self.parse_directive(key, value),
};
let filename = cb.filename.unwrap_or("paru.conf");
err.map_err(|e| anyhow!("{}:{}: {}", filename, cb.line_number, e))
}
}
impl Config {
pub fn new() -> Result<Self> {
let cache =
dirs::cache_dir().ok_or_else(|| anyhow!(tr!("failed to find cache directory")))?;
let cache = cache.join("paru");
let config =
dirs::config_dir().ok_or_else(|| anyhow!(tr!("failed to find config directory")))?;
let config = config.join("paru");
let state = dirs::state_dir()
.or_else(dirs::cache_dir)
.ok_or_else(|| anyhow!(tr!("failed to find state directory")))?;
let state = state.join("paru");
let build_dir = cache.join("clone");
let old_old_devel_path = cache.join("devel.json");
let old_devel_path = state.join("devel.json");
let devel_path = state.join("devel.toml");
let config_path = config.join("paru.conf");
let old = if old_devel_path.exists() {
Some(&old_devel_path)
} else if old_old_devel_path.exists() {
Some(&old_old_devel_path)
} else {
None
};
let cache_dir = cache;
let state_dir = state;
let color = Colors::from("never");
let cols = get_terminal_width();
let mut config = Self {
cols,
color,
build_dir,
cache_dir,
state_dir,
devel_path,
..Self::default()
};
if let Some(old) = old {
if let Ok(devel) = OpenOptions::new().read(true).open(old) {
if let Ok(devel) = serde_json::from_reader(devel) {
save_devel_info(&config, &devel)?;
let _ = remove_file(&old_devel_path);
let _ = remove_file(&old_old_devel_path);
}
}
}
if let Ok(conf) = var("PARU_CONF") {
let path = PathBuf::from(conf);
ensure!(
path.exists(),
tr!("config file '{}' does not exist", path.display())
);
config.config_path = Some(path);
} else if config_path.exists() {
config.config_path = Some(config_path);
} else {
let config_path = PathBuf::from("/etc/paru.conf");
if config_path.exists() {
config.config_path = Some(config_path);
}
}
Ok(config)
}
pub fn set_op_args_globals(&mut self, op: Op) {
self.op = op;
self.args.op = op.as_str().to_string();
self.globals.op = op.as_str().to_string();
}
pub fn pacman_args(&self) -> Args<&str> {
self.args.as_str()
}
pub fn pacman_globals(&self) -> Args<&str> {
self.globals.as_str()
}
pub fn parse_args<S: AsRef<str>, I: IntoIterator<Item = S>>(&mut self, iter: I) -> Result<()> {
let iter = iter.into_iter();
let mut iter = iter.peekable();
let mut op_count = 0;
let mut end_of_ops = false;
if let Ok(aurdest) = var("AURDEST") {
self.build_dir = aurdest.into();
}
while let Some(arg) = iter.next() {
let value = iter.peek().map(|s| s.as_ref());
let arg = arg.as_ref();
if self.parse_arg(arg, value, &mut op_count, &mut end_of_ops)? {
iter.next();
}
ensure!(
op_count <= 1,
tr!("only one operation may be used at a time")
);
}
if let Some((i, _)) = self.targets.iter().enumerate().find(|t| t.1 == "-") {
self.targets.remove(i);
self.parse_stdin()?;
reopen_stdin()?;
}
self.args.op = self.op.as_str().to_string();
self.args.targets = self.targets.clone();
self.args.bin = self.pacman_bin.clone();
self.globals.op = self.op.as_str().to_string();
self.globals.bin = self.pacman_bin.clone();
if self.help {
match self.op {
Op::GetPkgBuild | Op::Show | Op::Default => {
help::help();
std::process::exit(0);
}
_ => {
let status = exec::pacman(self, &self.args).unwrap_or(Status(1));
std::process::exit(status.code());
}
}
}
if self.version {
version();
std::process::exit(0);
}
self.init_pacmanconf()?;
self.init_alpm()?;
if self.pacman.color && !self.globals.has_arg("color", "color") {
self.color = Colors::from("auto");
}
#[cfg(not(feature = "mock"))]
{
use std::time::Duration;
let ver = option_env!("PARU_VERSION").unwrap_or(env!("CARGO_PKG_VERSION"));
let client = reqwest::Client::builder()
.tcp_keepalive(Duration::new(15, 0))
.user_agent(format!("paru/{}", ver))
.build()?;
let rpc_url = match &self.aur_rpc_url {
Some(rpc) => rpc.to_string(),
None => self.aur_url.join("rpc")?.to_string(),
};
self.raur = raur::Handle::new_with_settings(client, rpc_url);
}
#[cfg(feature = "mock")]
{
self.raur = crate::mock::Mock::new()?;
}
let aur_url = if self.ssh {
self.aur_url
.to_string()
.replacen("https://", "ssh://aur@", 1)
.parse()
.expect("change AUR URL schema from HTTPS to SSH")
} else {
self.aur_url.clone()
};
self.fetch = aur_fetch::Fetch {
git: self.git_bin.clone().into(),
git_flags: self.git_flags.clone(),
clone_dir: self.build_dir.clone(),
diff_dir: self.cache_dir.join("diff"),
aur_url: aur_url.clone(),
};
self.pkgbuild_repos.fetch = aur_fetch::Fetch {
git: self.git_bin.clone().into(),
git_flags: self.git_flags.clone(),
clone_dir: self.build_dir.join("repo"),
diff_dir: self.cache_dir.join("repo/diff"),
aur_url,
};
for repo in &mut self.pkgbuild_repos.repos {
if repo.source.url().is_some() {
repo.path = self.pkgbuild_repos.fetch.clone_dir.join(&repo.path);
}
}
if self.mode == Mode::empty() {
self.mode = Mode::all();
}
if !self.mode.pkgbuild() {
self.pkgbuild_repos.repos.clear();
}
self.need_root = self.need_root();
if let LocalRepos::Repo(repos) = &self.repos {
let (_, db) = repo::repo_aur_dbs(self);
for repo in repos {
if !db.iter().any(|db| db.name() == repo) {
bail!("{}", tr!("no local repo named {}", repo))
}
}
}
if self.repos != LocalRepos::None {
let (_, repos) = repo::repo_aur_dbs(self);
if repos.is_empty() {
bail!(
"no local repos configured, add one to your pacman.conf:
[options]
CacheDir = /var/lib/repo/aur
[aur]
SigLevel = PackageOptional DatabaseOptional
Server = file:///var/lib/repo/aur
then initialise it with:
paru -Ly"
);
}
for repo in repos {
if !self.pacman.repos.iter().any(|r| r.name == repo.name()) {
bail!(tr!(
"can not find local repo '{}' in pacman.conf",
repo.name()
));
}
}
}
self.no_warn = self.no_warn_builder.build()?;
self.ignore_devel = self.ignore_devel_builder.build()?;
if !self.assume_installed.is_empty() && !self.chroot {
self.mflags.push("-d".to_string());
}
if self.no_check {
self.mflags.push("--nocheck".to_string());
}
if self.chroot {
remove_var("PKGEXT");
}
Ok(())
}
fn init_pacmanconf(&mut self) -> Result<()> {
self.pacman = pacmanconf::Config::with_opts(
self.pacman_conf_bin.as_deref(),
self.pacman_conf.as_deref(),
self.root.as_deref(),
)?;
if let Some(ref dbpath) = self.db_path {
self.pacman.db_path = dbpath.clone();
}
self.ignore.extend(self.pacman.ignore_pkg.clone());
self.ignore_group.extend(self.pacman.ignore_group.clone());
Ok(())
}
pub fn new_alpm(&self) -> Result<alpm::Alpm> {
let mut alpm = alpm::Alpm::new(self.pacman.root_dir.as_str(), self.pacman.db_path.as_str())
.with_context(|| {
tr!(
"failed to initialize alpm: root={} dbpath={}",
self.pacman.root_dir,
self.pacman.db_path
)
})?;
alpm.set_question_cb((self.no_confirm, self.color), question);
alpm.set_dl_cb((), download);
alpm.set_log_cb(self.color, log);
alpm_utils::configure_alpm(&mut alpm, &self.pacman)?;
if !self.chroot {
for dep in &self.assume_installed {
alpm.add_assume_installed(&Depend::new(dep.as_str()))?;
}
}
for pkg in &self.ignore {
alpm.add_ignorepkg(pkg.as_str())?;
}
for group in &self.ignore_group {
alpm.add_ignoregroup(group.as_str())?;
}
Ok(alpm)
}
pub fn init_alpm(&mut self) -> Result<()> {
self.alpm = Alpm::new(self.new_alpm()?);
Ok(())
}
fn parse_stdin(&mut self) -> Result<()> {
for line in stdin().lock().lines() {
self.targets.push(line?);
}
Ok(())
}
fn need_root(&self) -> bool {
let args = &self.args;
if self.op == Op::Database {
return !args.has_arg("k", "check");
} else if self.op == Op::Files {
return args.has_arg("y", "refresh");
} else if self.op == Op::Query {
return args.has_arg("k", "check");
} else if self.op == Op::Remove {
return !(args.has_arg("p", "print") || args.has_arg("p", "print-format"));
} else if self.op == Op::Sync {
if args.has_arg("y", "refresh") {
return true;
}
return !(args.has_arg("p", "print")
|| args.has_arg("p", "print-format")
|| args.has_arg("s", "search")
|| args.has_arg("l", "list")
|| args.has_arg("g", "groups")
|| args.has_arg("i", "info")
|| (args.has_arg("c", "clean") && !self.mode.repo()));
} else if self.op == Op::Upgrade || self.op == Op::Build {
return true;
}
false
}
fn parse_directive(&mut self, key: &str, value: Option<&str>) -> Result<()> {
if key == "Include" {
let value = match value {
Some(value) => value,
None => bail!(tr!("value can not be empty for key '{}'", key)),
};
let ini = std::fs::read_to_string(value)?;
let section = self.section.clone();
let section = section.as_deref();
let section = self
.parse_with_section(section, Some(value), &ini)?
.map(|s| s.to_string());
self.section = section;
return Ok(());
}
let section = match &self.section {
Some(section) => section.as_str(),
None => bail!(tr!("key '{}' does not belong to a section", key)),
};
let section = section.to_string();
match section.as_str() {
"options" => self.parse_option(key, value),
"bin" => self.parse_bin(key, value),
"env" => self.parse_env(key, value),
repo => self.parse_repo(repo, key, value),
}
}
fn parse_repo(&mut self, repo: &str, key: &str, value: Option<&str>) -> Result<()> {
let value = value.context(tr!("key can not be empty"));
let repo = self.pkgbuild_repos.repo_mut(repo).unwrap();
match key {
"Url" => repo.source.set_url(Url::parse(value?)?),
"Path" => repo.source.set_path(value?.to_string()),
"Depth" => repo.depth = value?.parse()?,
"SkipReview" => repo.skip_review = true,
"GenerateSrcinfo" => repo.force_srcinfo = true,
_ => eprintln!("{}", tr!("error: unknown option '{}' in repo", key)),
}
Ok(())
}
fn parse_env(&mut self, key: &str, value: Option<&str>) -> Result<()> {
let value = value.context(tr!("key can not be empty"))?;
ensure!(!key.is_empty(), tr!("key can not be empty"));
ensure!(!key.contains('\0'), tr!("key can not contain null bytes"));
ensure!(
!value.contains('\0'),
tr!("value can not contain null bytes")
);
self.env.push((key.to_owned(), value.to_string()));
set_var(key, value);
Ok(())
}
fn parse_bin(&mut self, key: &str, value: Option<&str>) -> Result<()> {