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
use eyre::{eyre, Result};
use strum::{EnumIter, IntoEnumIterator};
use std::{ffi::OsString, fmt::Display, process::Command};


#[derive(Debug, Clone, Copy, EnumIter, PartialEq)]
pub enum Vendor {
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Apt,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Yay,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Yum,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Pacman,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Apk,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Emerge,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Guix,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    NixEnv,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Pkg,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Slackpkg,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Cards,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Dnf,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Eopkg,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Opkg,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Urpm,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Xbps,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Zypper,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Flatpak,
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    Snap,
    #[cfg(target_os = "haiku")]
    Pkgman,
    #[cfg(target_os = "macos")]
    Brew,
    #[cfg(target_os = "macos")]
    Ports,
    #[cfg(target_os = "windows")]
    Scoop,
    #[cfg(target_os = "windows")]
    Choco,
    #[cfg(target_os = "windows")]
    Winget,
    #[cfg(target_os = "android")]
    Termux,
}

#[derive(Debug, Clone, Copy)]
pub enum PlsCommand {
    Install,
    Remove,
    Upgrade,
    Search,
    Info,
    Update,
    UpgradeAll,
    List,
}

impl Vendor {
    pub fn new() -> Result<Self> {
        for vendor in Vendor::iter() {
            if vendor.is_available() {
                return Ok(vendor)
            }
        }
        Err(eyre!(
            "no vendor installed, candidates are: {}",
            Vendor::iter().map(|vendor| vendor.to_string()).collect::<Vec<String>>().join(", "),
        ))
    }

    pub fn is_available(&self) -> bool {
            let vendor_data: VendorData = (*self).into();
            which::which(vendor_data.1[0]).is_ok()
    }

    pub fn execute(self, command: PlsCommand, args: &str, yes: bool, su: bool, dry_run: bool, pager: Option<String>) -> Result<i32> {
        let vendor_data: VendorData = self.into();
        let command = command.format(vendor_data, args, yes, pager);

        if command.is_empty() {
            eprintln!("command not supported by the current vendor");
            return Ok(1)
        }

        if dry_run {
            eprintln!("{}", command);
            return Ok(0);
        }

        #[cfg(target_os = "windows")]
        let status = Command::new("cmd").args(["/C", &command]).status()?;
        #[cfg(not(target_os = "windows"))]
        let status = if su {
            Command::new("sudo").args(command.split(" ")).status()?
        } else {
            Command::new("sh").args(["-c", &command]).status()?
        };

        Ok(status.code().unwrap_or_default())
    }
}

impl PlsCommand {
    fn format(self, vendor: VendorData, args: &str, yes: bool, pager: Option<String>) -> String {
        match self {
            PlsCommand::Install => vendor.1[2].to_owned(),
            PlsCommand::Remove => vendor.1[3].to_owned(),
            PlsCommand::Upgrade => vendor.1[4].to_owned(),
            PlsCommand::Info => vendor.1[6].to_owned(),
            PlsCommand::Update => vendor.1[7].to_owned(),
            PlsCommand::UpgradeAll => vendor.1[8].to_owned(),
            PlsCommand::Search => {
                if let Some(pager) = pager {
                    format!("{} | {}", vendor.1[5], pager)
                } else {
                    vendor.1[5].to_owned()
                }
            }
            PlsCommand::List => {
                if let Some(pager) = pager {
                    format!("{} | {}", vendor.1[9], pager)
                } else {
                    vendor.1[9].to_owned()
                }
            }
        }
            .replace("$yes", if yes {vendor.1[1]} else {""})
            .replace("$args", args)
    }
}

impl From<OsString> for Vendor {
    fn from(value: OsString) -> Self {
        let value = value.to_string_lossy().to_lowercase();
        for vendor in Vendor::iter() {
            if vendor.to_string().to_lowercase() == value {
                return vendor;
            }
        }
        panic!("invalid vendor name {}", value);
    }
}

//----------------------------------------------------------------------------//
use Vendor::*;

#[derive(Debug, Clone, Copy, PartialEq)]
struct VendorData(Vendor, [&'static str; 10]);

static VENDORS: &[VendorData] = &[
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Apk, [
        "apk",
        "",
        "apk add $args",
        "apk del $args",
        "apk upgrade $args",
        "apk search $args",
        "apk info $args",
        "apk update",
        "apk upgrade",
        "apk list --installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Apt, [
        "apt",
        "--yes",
        "apt install $yes $args",
        "apt remove $yes $args",
        "apt install --only-upgrade $yes $args",
        "apt search $args",
        "apt show $args",
        "apt update $yes",
        "apt upgrade $yes",
        "apt list --installed",
    ]),
    #[cfg(target_os = "macos")]
    VendorData(Brew, [
        "brew",
        "",
        "brew install $args",
        "brew uninstall $args",
        "brew upgrade $args",
        "brew search $args",
        "brew info $args",
        "brew update",
        "brew upgrade",
        "brew list",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Cards, [
        "cards",
        "",
        "cards install $args",
        "cards remove $args",
        "cards install --upgrade $args",
        "cards search $args",
        "cards info $args",
        "cards sync",
        "cards upgrade",
        "cards list",
    ]),
    #[cfg(target_os = "windows")]
    VendorData(Choco, [
        "choco",
        "--yes",
        "choco install $yes $args",
        "choco uninstall $yes $args",
        "choco upgrade $yes $args",
        "choco search $args",
        "choco info $args",
        "",
        "choco upgrade all $yes",
        "choco list",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Dnf, [
        "dnf",
        "--assumeyes",
        "dnf install $yes $args",
        "dnf remove $yes $args",
        "dnf upgrade $yes $args",
        "dnf search $args",
        "dnf info $args",
        "dnf check-update $yes",
        "dnf update $yes",
        "dnf list --installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Emerge, [
        "emerge",
        "",
        "emerge $args",
        "emerge --depclean $args",
        "emerge --update $args",
        "emerge --search $args",
        "emerge --info $args",
        "emerge --sync",
        "emerge -vuDN @world",
        "qlist -Iv",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Eopkg, [
        "eopkg",
        "--yes-all",
        "eopkg install $yes $args",
        "eopkg remove $yes $args",
        "eopkg upgrade $yes $args",
        "eopkg search $args",
        "eopkg info $args",
        "eopkg update-repo $yes",
        "eopkg upgrade $yes",
        "eopkg list-installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Flatpak, [
        "flatpak",
        "--assumeyes",
        "flatpak install $yes $args",
        "flatpak uninstall $yes $args",
        "flatpak update $yes $args",
        "flatpak search $args",
        "flatpak info $args",
        "",
        "flatpak update $yes",
        "flatpak list",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Guix, [
        "guix",
        "",
        "guix install $yes $args",
        "guix remove $yes $args",
        "guix upgrade $yes $args",
        "guix search $args",
        "guix show $args",
        "guix refresh $yes",
        "guix upgrade $yes",
        "guix package --list-installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(NixEnv, [
        "nix-env",
        "",
        "nix-env --install $args",
        "nix-env --uninstall $args",
        "nix-env --upgrade $args",
        "nix-env -qaP $args",
        "nix-env -qa --description $args",
        "nix-channel --update",
        "nix-env --upgrade",
        "nix-env --query --installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Opkg, [
        "opkg",
        "",
        "opkg install $args",
        "opkg remove $args",
        "opkg upgrade $args",
        "opkg find $args",
        "opkg info $args",
        "opkg update",
        "opkg upgrade",
        "opkg list-installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Pacman, [
        "pacman",
        "--noconfirm",
        "pacman -S $yes $args",
        "pacman -Rs $yes $args",
        "pacman -S $yes $args",
        "pacman -Ss $args",
        "pacman -Si $args",
        "pacman -Sy $yes",
        "pacman -Syu $yes",
        "pacman -Q",
    ]),
    #[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "dragonfly", target_os = "netbsd"))]
    VendorData(Pkg, [
        "pkg",
        "--yes",
        "pkg install $yes $args",
        "pkg remove $yes $args",
        "pkg install $yes $args",
        "pkg search $args",
        "pkg info $args",
        "pkg update $yes",
        "pkg upgrade $yes",
        "pkg info --all",
    ]),
    #[cfg(target_os = "haiku")]
    VendorData(Pkgman, [
        "pkgman",
        "-y",
        "pkgman install $yes $args",
        "pkgman uninstall $yes $args",
        "pkgman update $yes $args",
        "pkgman search $args",
        "",
        "pkgman refresh $yes",
        "pkgman update $yes",
        "pkgman search --installed-only --all",
    ]),
    #[cfg(target_os = "macos")]
    VendorData(Ports, [
        "prt-get",
        "",
        "prt-get install $args",
        "prt-get remove $args",
        "prt-get update $args",
        "prt-get search $args",
        "prt-get info $args",
        "ports -u",
        "prt-get sysup",
        "prt-get listinst",
    ]),
    #[cfg(target_os = "windows")]
    VendorData(Scoop, [
        "scoop",
        "",
        "scoop install $args",
        "scoop uninstall $args",
        "scoop update $args",
        "scoop search $args",
        "scoop info $args",
        "scoop update",
        "scoop update *",
        "scoop list",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Slackpkg, [
        "slackpkg",
        "",
        "slackpkg install $args",
        "slackpkg remove $args",
        "slackpkg upgrade $args",
        "slackpkg search $args",
        "slackpkg info $args",
        "slackpkg update",
        "slackpkg upgrade-all",
        "ls -1 /var/log/packages",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Snap, [
        "snap",
        "",
        "snap install --classic $args",
        "snap remove $args",
        "snap refresh $args",
        "snap find $args",
        "snap info $args",
        "",
        "snap refresh",
        "snap list",
    ]),
    #[cfg(target_os = "android")]
    VendorData(Termux, [
        "termux",
        "--yes",
        "pkg install $yes $args",
        "pkg uninstall $yes $args",
        "pkg install $yes $args",
        "pkg search $args",
        "pkg show $args",
        "pkg update $yes",
        "pkg upgrade $yes",
        "pkg list-installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Urpm, [
        "urpm",
        "",
        "urpmi $args",
        "urpme $args",
        "urpmi $args",
        "urpmq --fuzzy $args",
        "urpmq -i $args",
        "urpmi.update -a",
        "urpmi --auto-update",
        "rpm --query --all",
    ]),
    #[cfg(target_os = "windows")]
    VendorData(Winget, [
        "winget",
        "",
        "winget install $args",
        "winget uninstall $args",
        "winget upgrade $args",
        "winget search $args",
        "winget show $args",
        "",
        "winget upgrade --all",
        "winget list",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Xbps, [
        "xbps",
        "--yes",
        "xbps-install $yes $args",
        "xbps-remove $yes $args",
        "xbps-install --update $yes $args",
        "xbps-query -Rs $args",
        "xbps-query -RS $args",
        "xbps-install --sync $yes",
        "xbps-install --update $yes",
        "xbps-query --list-pkgs",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Yay, [
        "yay",
        "--noconfirm",
        "yay --topdown --cleanafter -S $yes $args",
        "pacman -Rs $yes $",
        "yay --topdown --cleanafter -S $yes $args",
        "yay --topdown -Ss $args",
        "yay --topdown -Si $args",
        "yay --topdown -Sy $yes",
        "yay --topdown -Syu $yes",
        "pacman -Q",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Yum, [
        "yum",
        "--assumeyes",
        "yum install $yes $args",
        "yum remove $yes $args",
        "yum update $yes $args",
        "yum search $args",
        "yum info $args",
        "yum check-update $yes",
        "yum update $yes",
        "yum list --installed",
    ]),
    #[cfg(target_os = "linux")] /* TODO: filter by distro */
    VendorData(Zypper, [
        "zypper",
        "--no-confirm",
        "zypper install $yes $args",
        "zypper remove $yes $args",
        "zypper update $yes $args",
        "zypper search $args",
        "zypper info $args",
        "zypper refresh $yes",
        "zypper update $yes",
        "zypper search --installed-only",
    ]),
];

impl Display for Vendor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl From<Vendor> for VendorData {
    fn from(value: Vendor) -> Self {
        for vendor in VENDORS.iter() {
            if vendor.0 == value {
                return *vendor;
            }
        }
        panic!("unreachable code reached for vendor {:?}", value);
    }
}

impl TryFrom<&str> for Vendor {
    type Error = String;

    fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
        let value = value.to_lowercase();
        for vendor in Vendor::iter() {
            if vendor.to_string().to_lowercase() == value {
                return Ok(vendor);
            }
        }
        Err(format!("invalid vendor name {}", value))
    }
}