From cbd9fa9ab61c372d3bd080cf5b5d592ac6c8aac9 Mon Sep 17 00:00:00 2001 From: innocentzero Date: Fri, 15 Nov 2024 22:07:13 +0530 Subject: [PATCH 1/4] add version subcommand Signed-off-by: innocentzero --- src/backends/all.rs | 21 +++++++++++++++++++++ src/backends/apt.rs | 8 ++++++++ src/backends/arch.rs | 11 +++++++++++ src/backends/brew.rs | 8 ++++++++ src/backends/cargo.rs | 10 +++++++++- src/backends/dnf.rs | 8 ++++++++ src/backends/flatpak.rs | 8 ++++++++ src/backends/mod.rs | 2 ++ src/backends/pipx.rs | 8 ++++++++ src/backends/rustup.rs | 8 ++++++++ src/backends/winget.rs | 8 ++++++++ src/backends/xbps.rs | 8 ++++++++ src/cli.rs | 6 ++++++ src/core.rs | 13 +++++++++++++ 14 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/backends/all.rs b/src/backends/all.rs index 11a18b8..50ddf8c 100644 --- a/src/backends/all.rs +++ b/src/backends/all.rs @@ -40,6 +40,11 @@ macro_rules! any { $($upper_backend,)* } impl AnyBackend { + pub fn version(&self, config: &Config) -> Result { + match self { + $( AnyBackend::$upper_backend => $upper_backend::version(config), )* + } + } pub fn remove_packages(&self, packages: &BTreeSet, no_confirm: bool, config: &Config) -> Result<()> { match self { $( AnyBackend::$upper_backend => $upper_backend::remove_packages(packages, no_confirm, config), )* @@ -48,8 +53,24 @@ macro_rules! any { } }; } + +macro_rules! versions { + ($(($upper_backend:ident, $lower_backend:ident)), *) => { + pub fn backend_versions(config: &Config) -> BTreeMap { + let mut results = BTreeMap::new(); + $( + results.insert(stringify!($upper_backend).to_string(), AnyBackend::version(&AnyBackend::$upper_backend, config).unwrap()); + )* + + results + } + } +} + apply_public_backends!(any); +apply_public_backends!(versions); + macro_rules! raw_package_ids { ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default)] diff --git a/src/backends/apt.rs b/src/backends/apt.rs index 75ce9fa..36423b0 100644 --- a/src/backends/apt.rs +++ b/src/backends/apt.rs @@ -76,4 +76,12 @@ impl Backend for Apt { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("apt") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["apt", "--version"], Perms::Same) + } + } } diff --git a/src/backends/arch.rs b/src/backends/arch.rs index 331e992..dc2a7c0 100644 --- a/src/backends/arch.rs +++ b/src/backends/arch.rs @@ -187,4 +187,15 @@ impl Backend for Arch { Ok(()) } + + fn version(config: &Config) -> Result { + if !command_found(config.arch_package_manager.as_command()) { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout( + [config.arch_package_manager.as_command(), "--version"], + Perms::Same, + ) + } + } } diff --git a/src/backends/brew.rs b/src/backends/brew.rs index 1e047a1..b5ba2e8 100644 --- a/src/backends/brew.rs +++ b/src/backends/brew.rs @@ -71,4 +71,12 @@ impl Backend for Brew { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("brew") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["brew", "--version"], Perms::Same) + } + } } diff --git a/src/backends/cargo.rs b/src/backends/cargo.rs index cd797ba..256b8fe 100644 --- a/src/backends/cargo.rs +++ b/src/backends/cargo.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use serde_inline_default::serde_inline_default; use serde_json::Value; -use crate::cmd::{command_found, run_command}; +use crate::cmd::{command_found, run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -113,6 +113,14 @@ impl Backend for Cargo { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("cargo") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["cargo", "--version"], Perms::Same) + } + } } fn extract_packages(contents: &str) -> Result> { diff --git a/src/backends/dnf.rs b/src/backends/dnf.rs index 0c9a571..ebc6e07 100644 --- a/src/backends/dnf.rs +++ b/src/backends/dnf.rs @@ -105,6 +105,14 @@ impl Backend for Dnf { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("dnf") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["dnf", "--version"], Perms::Same) + } + } } fn parse_package(package: &str) -> String { diff --git a/src/backends/flatpak.rs b/src/backends/flatpak.rs index d265396..6f7946e 100644 --- a/src/backends/flatpak.rs +++ b/src/backends/flatpak.rs @@ -180,4 +180,12 @@ impl Backend for Flatpak { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("flatpak") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["flatpak", "--version"], Perms::Same) + } + } } diff --git a/src/backends/mod.rs b/src/backends/mod.rs index b5820aa..11b5ebf 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -60,4 +60,6 @@ pub trait Backend { no_confirm: bool, config: &Config, ) -> Result<()>; + + fn version(config: &Config) -> Result; } diff --git a/src/backends/pipx.rs b/src/backends/pipx.rs index ae980b0..297dd97 100644 --- a/src/backends/pipx.rs +++ b/src/backends/pipx.rs @@ -77,6 +77,14 @@ impl Backend for Pipx { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("pipx") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["pipx", "--version"], Perms::Same) + } + } } fn extract_package_names(stdout: String) -> Result> { diff --git a/src/backends/rustup.rs b/src/backends/rustup.rs index b5466d1..a709e4f 100644 --- a/src/backends/rustup.rs +++ b/src/backends/rustup.rs @@ -120,4 +120,12 @@ impl Backend for Rustup { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("rustup") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["rustup", "--version"], Perms::Same) + } + } } diff --git a/src/backends/winget.rs b/src/backends/winget.rs index 51c6745..f35c282 100644 --- a/src/backends/winget.rs +++ b/src/backends/winget.rs @@ -93,4 +93,12 @@ impl Backend for WinGet { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("winget") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["winget", "--version"], Perms::Same) + } + } } diff --git a/src/backends/xbps.rs b/src/backends/xbps.rs index 20494ef..04b7a45 100644 --- a/src/backends/xbps.rs +++ b/src/backends/xbps.rs @@ -90,4 +90,12 @@ impl Backend for Xbps { Ok(()) } + + fn version(_: &Config) -> Result { + if !command_found("xbps-query") { + Ok(String::from("Not found\n")) + } else { + run_command_for_stdout(["xbps-query", "--version"], Perms::Same) + } + } } diff --git a/src/cli.rs b/src/cli.rs index 490d118..8daf5a7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -30,6 +30,7 @@ pub enum MainSubcommand { Review(ReviewCommand), Sync(SyncCommand), Unmanaged(UnmanagedCommand), + Backends(BackendsCommand), } #[derive(Args)] @@ -76,3 +77,8 @@ pub struct SyncCommand { #[command(visible_alias("u"))] /// show explicitly installed packages not managed by metapac pub struct UnmanagedCommand {} + +#[derive(Args)] +#[command(visible_alias("b"))] +/// show the backends found by metapac +pub struct BackendsCommand {} diff --git a/src/core.rs b/src/core.rs index 49dc887..3013180 100644 --- a/src/core.rs +++ b/src/core.rs @@ -6,6 +6,8 @@ use color_eyre::Result; use dialoguer::Confirm; use toml_edit::{Array, DocumentMut, Item, Value}; +use crate::backends::all::backend_versions; +use crate::cli::BackendsCommand; use crate::prelude::*; use crate::review::review; @@ -41,6 +43,7 @@ impl MainArguments { MainSubcommand::Review(review) => review.run(&managed, &config), MainSubcommand::Sync(sync) => sync.run(&managed, &config), MainSubcommand::Unmanaged(unmanaged) => unmanaged.run(&managed, &config), + MainSubcommand::Backends(found_backends) => found_backends.run(&config), } } } @@ -170,6 +173,16 @@ impl UnmanagedCommand { } } +impl BackendsCommand { + fn run(self, config: &Config) -> Result<()> { + for (backend, version) in backend_versions(config) { + println!("{backend}:"); + println!("{version}"); + } + Ok(()) + } +} + fn unmanaged(managed: &InstallOptions, config: &Config) -> Result { QueryInfos::query_installed_packages(config) .map(|x| x.to_package_ids().difference(&managed.to_package_ids())) From 2a6feee65e8b430c03b2f3c29fb9b1bf6eaad7f6 Mon Sep 17 00:00:00 2001 From: ripytide Date: Sat, 16 Nov 2024 23:40:45 +0000 Subject: [PATCH 2/4] refactor: re-organize things --- Cargo.lock | 76 +++++++++++++++++++++++------------------ Cargo.toml | 10 +++--- src/backends/all.rs | 17 +-------- src/backends/apt.rs | 14 +++----- src/backends/arch.rs | 23 +++++++------ src/backends/brew.rs | 13 +++---- src/backends/cargo.rs | 12 +++---- src/backends/dnf.rs | 14 ++++---- src/backends/flatpak.rs | 20 +++++------ src/backends/pipx.rs | 12 +++---- src/backends/rustup.rs | 14 +++----- src/backends/winget.rs | 13 +++---- src/backends/xbps.rs | 14 +++----- src/cmd.rs | 18 +++++----- src/core.rs | 15 +++++--- 15 files changed, 130 insertions(+), 155 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c444c0d..b88436b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -140,9 +140,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.20" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" dependencies = [ "clap_builder", "clap_derive", @@ -150,9 +150,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.20" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" dependencies = [ "anstream", "anstyle", @@ -537,11 +537,11 @@ dependencies = [ "serde", "serde-inline-default", "serde_json", + "strum", "tempfile", "toml", "toml_edit", "walkdir", - "which", ] [[package]] @@ -654,9 +654,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", @@ -700,6 +700,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustversion" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" + [[package]] name = "ryu" version = "1.0.18" @@ -717,9 +723,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.210" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" dependencies = [ "serde_derive", ] @@ -737,9 +743,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.210" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", @@ -748,9 +754,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.131" +version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67d42a0bd4ac281beff598909bb56a86acaf979b84483e1c79c10dcaf98f8cf3" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" dependencies = [ "itoa", "memchr", @@ -794,11 +800,33 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "syn" -version = "2.0.79" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -1005,18 +1033,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "which" -version = "7.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9cad3279ade7346b96e38731a641d7343dd6a53d55083dd54eadfa5a1b38c6b" -dependencies = [ - "either", - "home", - "rustix", - "winsafe", -] - [[package]] name = "winapi-util" version = "0.1.9" @@ -1202,12 +1218,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "winsafe" -version = "0.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" - [[package]] name = "zeroize" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index 22556f1..920e644 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,12 +32,12 @@ eula = false [dependencies] color-eyre = "0.6.3" -clap = { version = "4.5.20", features = ["derive"] } -regex = { version = "1.11.0", default-features = false, features = ["std"] } +clap = { version = "4.5.21", features = ["derive"] } +regex = { version = "1.11.1", default-features = false, features = ["std"] } libc = "0.2.161" log = { version = "0.4.22", features = ["std"] } -serde = { version = "1.0.210", features = ["derive"] } -serde_json = "1.0.131" +serde = { version = "1.0.215", features = ["derive"] } +serde_json = "1.0.132" toml = "0.8.19" derive_more = { version = "1.0.0", features = ["full"] } itertools = "0.13.0" @@ -49,8 +49,8 @@ serde-inline-default = "0.2.2" hostname = "0.4.0" walkdir = "2.5.0" toml_edit = "0.22.22" -which = '7.0.0' tempfile = "3.14.0" +strum = {version = "0.26.3", features = ["derive"]} [dev-dependencies] assert_cmd = "2.0.16" diff --git a/src/backends/all.rs b/src/backends/all.rs index 50ddf8c..624536b 100644 --- a/src/backends/all.rs +++ b/src/backends/all.rs @@ -35,7 +35,7 @@ macro_rules! to_package_ids { macro_rules! any { ($(($upper_backend:ident, $lower_backend:ident)),*) => { - #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::FromStr, derive_more::Display)] + #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::FromStr, derive_more::Display, strum::EnumIter)] pub enum AnyBackend { $($upper_backend,)* } @@ -54,23 +54,8 @@ macro_rules! any { }; } -macro_rules! versions { - ($(($upper_backend:ident, $lower_backend:ident)), *) => { - pub fn backend_versions(config: &Config) -> BTreeMap { - let mut results = BTreeMap::new(); - $( - results.insert(stringify!($upper_backend).to_string(), AnyBackend::version(&AnyBackend::$upper_backend, config).unwrap()); - )* - - results - } - } -} - apply_public_backends!(any); -apply_public_backends!(versions); - macro_rules! raw_package_ids { ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default)] diff --git a/src/backends/apt.rs b/src/backends/apt.rs index 36423b0..5969f81 100644 --- a/src/backends/apt.rs +++ b/src/backends/apt.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet}; use color_eyre::Result; use serde::{Deserialize, Serialize}; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -26,8 +26,8 @@ impl Backend for Apt { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("apt-mark") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -37,7 +37,7 @@ impl Backend for Apt { // designed with this use-case in mind so there are lots and // lots of different methods all of which seem to have // caveats. - let explicit = run_command_for_stdout(["apt-mark", "showmanual"], Perms::Same)?; + let explicit = run_command_for_stdout(["apt-mark", "showmanual"], Perms::Same, false)?; Ok(explicit .lines() @@ -78,10 +78,6 @@ impl Backend for Apt { } fn version(_: &Config) -> Result { - if !command_found("apt") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["apt", "--version"], Perms::Same) - } + run_command_for_stdout(["apt", "--version"], Perms::Same, false) } } diff --git a/src/backends/arch.rs b/src/backends/arch.rs index dc2a7c0..7032711 100644 --- a/src/backends/arch.rs +++ b/src/backends/arch.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use serde_inline_default::serde_inline_default; use std::collections::{BTreeMap, BTreeSet}; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -27,7 +27,7 @@ impl Backend for Arch { mut packages: BTreeMap, config: &Config, ) -> Result> { - if !command_found(config.arch_package_manager.as_command()) { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -39,6 +39,7 @@ impl Backend for Arch { "--quiet", ], Perms::Same, + false, )?; for group in groups.lines() { @@ -52,6 +53,7 @@ impl Backend for Arch { group, ], Perms::Same, + false, )?; for group_package in group_packages.lines() { @@ -92,7 +94,7 @@ impl Backend for Arch { } fn query_installed_packages(config: &Config) -> Result> { - if !command_found(config.arch_package_manager.as_command()) { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -104,6 +106,7 @@ impl Backend for Arch { "--quiet", ], Perms::Same, + false, )?; let mut result = BTreeMap::new(); @@ -168,6 +171,7 @@ impl Backend for Arch { "--quiet", ], Perms::Same, + false, )?; let orphans = orphans_output.lines(); @@ -189,13 +193,10 @@ impl Backend for Arch { } fn version(config: &Config) -> Result { - if !command_found(config.arch_package_manager.as_command()) { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout( - [config.arch_package_manager.as_command(), "--version"], - Perms::Same, - ) - } + run_command_for_stdout( + [config.arch_package_manager.as_command(), "--version"], + Perms::Same, + false, + ) } } diff --git a/src/backends/brew.rs b/src/backends/brew.rs index b5ba2e8..d95a659 100644 --- a/src/backends/brew.rs +++ b/src/backends/brew.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet}; use color_eyre::Result; use serde::{Deserialize, Serialize}; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -26,14 +26,15 @@ impl Backend for Brew { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("brew") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } let explicit = run_command_for_stdout( ["brew", "list", "-1", "--quiet", "--installed-on-request"], Perms::Same, + false, )?; Ok(explicit @@ -73,10 +74,6 @@ impl Backend for Brew { } fn version(_: &Config) -> Result { - if !command_found("brew") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["brew", "--version"], Perms::Same) - } + run_command_for_stdout(["brew", "--version"], Perms::Same, false) } } diff --git a/src/backends/cargo.rs b/src/backends/cargo.rs index 256b8fe..c5954f0 100644 --- a/src/backends/cargo.rs +++ b/src/backends/cargo.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use serde_inline_default::serde_inline_default; use serde_json::Value; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -45,8 +45,8 @@ impl Backend for Cargo { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("cargo") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -115,11 +115,7 @@ impl Backend for Cargo { } fn version(_: &Config) -> Result { - if !command_found("cargo") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["cargo", "--version"], Perms::Same) - } + run_command_for_stdout(["cargo", "--version"], Perms::Same, false) } } diff --git a/src/backends/dnf.rs b/src/backends/dnf.rs index ebc6e07..ca01df4 100644 --- a/src/backends/dnf.rs +++ b/src/backends/dnf.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet}; use color_eyre::Result; use serde::{Deserialize, Serialize}; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -30,8 +30,8 @@ impl Backend for Dnf { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("dnf") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -44,6 +44,7 @@ impl Backend for Dnf { "%{from_repo}/%{name}", ], Perms::Same, + false, )?; let system_packages = system_packages.lines().map(parse_package); @@ -56,6 +57,7 @@ impl Backend for Dnf { "%{from_repo}/%{name}", ], Perms::Same, + false, )?; let user_packages = user_packages.lines().map(parse_package); @@ -107,11 +109,7 @@ impl Backend for Dnf { } fn version(_: &Config) -> Result { - if !command_found("dnf") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["dnf", "--version"], Perms::Same) - } + run_command_for_stdout(["dnf", "--version"], Perms::Same, false) } } diff --git a/src/backends/flatpak.rs b/src/backends/flatpak.rs index 6f7946e..468ebf6 100644 --- a/src/backends/flatpak.rs +++ b/src/backends/flatpak.rs @@ -4,7 +4,7 @@ use color_eyre::Result; use itertools::Itertools; use serde::{Deserialize, Serialize}; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -29,8 +29,8 @@ impl Backend for Flatpak { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("flatpak") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -43,6 +43,7 @@ impl Backend for Flatpak { "--columns=application", ], Perms::Same, + false, )?; let sys_explicit = sys_explicit_out .lines() @@ -57,6 +58,7 @@ impl Backend for Flatpak { "--columns=application", ], Perms::Same, + false, )?; let user_explicit = user_explicit_out .lines() @@ -71,9 +73,10 @@ impl Backend for Flatpak { "--columns=application", ], Perms::Same, + false, )?; let sys_explicit_runtimes_out = - run_command_for_stdout(["flatpak", "pin", "--system"], Perms::Same)?; + run_command_for_stdout(["flatpak", "pin", "--system"], Perms::Same, false)?; let sys_explicit_runtimes = sys_explicit_runtimes_out .lines() .skip(1) @@ -100,9 +103,10 @@ impl Backend for Flatpak { "--columns=application", ], Perms::Same, + false, )?; let user_explicit_runtimes_out = - run_command_for_stdout(["flatpak", "pin", "--user"], Perms::Same)?; + run_command_for_stdout(["flatpak", "pin", "--user"], Perms::Same, false)?; let user_explicit_runtimes = user_explicit_runtimes_out .lines() .skip(1) @@ -182,10 +186,6 @@ impl Backend for Flatpak { } fn version(_: &Config) -> Result { - if !command_found("flatpak") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["flatpak", "--version"], Perms::Same) - } + run_command_for_stdout(["flatpak", "--version"], Perms::Same, false) } } diff --git a/src/backends/pipx.rs b/src/backends/pipx.rs index 297dd97..c42ddb3 100644 --- a/src/backends/pipx.rs +++ b/src/backends/pipx.rs @@ -7,7 +7,6 @@ use serde::Deserialize; use serde::Serialize; use serde_json::Value; -use crate::cmd::command_found; use crate::cmd::run_command; use crate::cmd::run_command_for_stdout; use crate::prelude::*; @@ -32,14 +31,15 @@ impl Backend for Pipx { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("pipx") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } let names = extract_package_names(run_command_for_stdout( ["pipx", "list", "--json"], Perms::Same, + false, )?)?; Ok(names @@ -79,11 +79,7 @@ impl Backend for Pipx { } fn version(_: &Config) -> Result { - if !command_found("pipx") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["pipx", "--version"], Perms::Same) - } + run_command_for_stdout(["pipx", "--version"], Perms::Same, false) } } diff --git a/src/backends/rustup.rs b/src/backends/rustup.rs index a709e4f..16bfa22 100644 --- a/src/backends/rustup.rs +++ b/src/backends/rustup.rs @@ -1,4 +1,3 @@ -use crate::cmd::command_found; use crate::cmd::run_command; use crate::cmd::run_command_for_stdout; use crate::prelude::*; @@ -35,15 +34,15 @@ impl Backend for Rustup { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("rustup") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } let mut packages = BTreeMap::new(); let toolchains_stdout = - run_command_for_stdout(["rustup", "toolchain", "list"], Perms::Same)?; + run_command_for_stdout(["rustup", "toolchain", "list"], Perms::Same, false)?; let toolchains = toolchains_stdout.lines().map(|x| { x.split(' ') .next() @@ -65,6 +64,7 @@ impl Backend for Rustup { toolchain.as_str(), ], Perms::Same, + false, ) { packages.insert( toolchain, @@ -122,10 +122,6 @@ impl Backend for Rustup { } fn version(_: &Config) -> Result { - if !command_found("rustup") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["rustup", "--version"], Perms::Same) - } + run_command_for_stdout(["rustup", "--version"], Perms::Same, true) } } diff --git a/src/backends/winget.rs b/src/backends/winget.rs index f35c282..bfdd065 100644 --- a/src/backends/winget.rs +++ b/src/backends/winget.rs @@ -5,7 +5,7 @@ use color_eyre::Result; use serde::{Deserialize, Serialize}; use serde_json::Value; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -28,8 +28,8 @@ impl Backend for WinGet { Ok(packages) } - fn query_installed_packages(_: &Config) -> Result> { - if !command_found("winget") { + fn query_installed_packages(config: &Config) -> Result> { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } @@ -43,6 +43,7 @@ impl Backend for WinGet { tempfile.path().to_str().unwrap(), ], Perms::Same, + false, )?; let mut export = String::new(); @@ -95,10 +96,6 @@ impl Backend for WinGet { } fn version(_: &Config) -> Result { - if !command_found("winget") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["winget", "--version"], Perms::Same) - } + run_command_for_stdout(["winget", "--version"], Perms::Same, false) } } diff --git a/src/backends/xbps.rs b/src/backends/xbps.rs index 04b7a45..ee3d014 100644 --- a/src/backends/xbps.rs +++ b/src/backends/xbps.rs @@ -5,7 +5,7 @@ use color_eyre::Result; use regex::Regex; use serde::{Deserialize, Serialize}; -use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::cmd::{run_command, run_command_for_stdout}; use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] @@ -29,15 +29,15 @@ impl Backend for Xbps { } fn query_installed_packages( - _: &Config, + config: &Config, ) -> Result> { - if !command_found("xbps-query") { + if Self::version(config).is_ok() { return Ok(BTreeMap::new()); } let mut cmd = Command::new("xbps-query"); cmd.args(["-l"]); - let stdout = run_command_for_stdout(["xbps-query", "-l"], Perms::Same)?; + let stdout = run_command_for_stdout(["xbps-query", "-l"], Perms::Same, false)?; // Removes the package status and description from output let re1 = Regex::new(r"^ii |^uu |^hr |^\?\? | .*")?; @@ -92,10 +92,6 @@ impl Backend for Xbps { } fn version(_: &Config) -> Result { - if !command_found("xbps-query") { - Ok(String::from("Not found\n")) - } else { - run_command_for_stdout(["xbps-query", "--version"], Perms::Same) - } + run_command_for_stdout(["xbps-query", "--version"], Perms::Same, false) } } diff --git a/src/cmd.rs b/src/cmd.rs index 7f416bf..7aebd4b 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -3,17 +3,13 @@ use std::process::{Command, Stdio}; use color_eyre::{eyre::eyre, Result}; use itertools::Itertools; -pub fn command_found(command: &str) -> bool { - which::which(command).is_ok() -} - #[derive(Debug, Clone, Copy)] pub enum Perms { Sudo, Same, } -pub fn run_command_for_stdout(args: I, perms: Perms) -> Result +pub fn run_command_for_stdout(args: I, perms: Perms, hide_stderr: bool) -> Result where S: Into, I: IntoIterator, @@ -34,15 +30,19 @@ where let (first_arg, remaining_args) = args.split_first().unwrap(); let mut command = Command::new(first_arg); - let status = command + let output = command .args(remaining_args) .stdin(Stdio::inherit()) .stdout(Stdio::piped()) - .stderr(Stdio::inherit()) + .stderr(if !hide_stderr { + Stdio::inherit() + } else { + Stdio::null() + }) .output()?; - if status.status.success() { - Ok(String::from_utf8(status.stdout)?) + if output.status.success() { + Ok(String::from_utf8(output.stdout)?) } else { Err(eyre!("command failed: {:?}", args.into_iter().join(" "))) } diff --git a/src/core.rs b/src/core.rs index 3013180..a1b97a7 100644 --- a/src/core.rs +++ b/src/core.rs @@ -4,9 +4,9 @@ use std::path::Path; use color_eyre::eyre::{eyre, Context, ContextCompat}; use color_eyre::Result; use dialoguer::Confirm; +use strum::IntoEnumIterator; use toml_edit::{Array, DocumentMut, Item, Value}; -use crate::backends::all::backend_versions; use crate::cli::BackendsCommand; use crate::prelude::*; use crate::review::review; @@ -175,10 +175,17 @@ impl UnmanagedCommand { impl BackendsCommand { fn run(self, config: &Config) -> Result<()> { - for (backend, version) in backend_versions(config) { - println!("{backend}:"); - println!("{version}"); + for backend in AnyBackend::iter() { + println!( + "{backend}: {}", + backend + .version(config) + .as_deref() + .unwrap_or("Not Found") + .trim() + ); } + Ok(()) } } From 6a0421b0750e55d4d79d3f502d71c88a87bfa27d Mon Sep 17 00:00:00 2001 From: ripytide Date: Sun, 17 Nov 2024 14:03:32 +0000 Subject: [PATCH 3/4] fix: flip is_ok with is_err for checking early returns --- src/backends/apt.rs | 2 +- src/backends/arch.rs | 4 ++-- src/backends/brew.rs | 2 +- src/backends/cargo.rs | 2 +- src/backends/dnf.rs | 2 +- src/backends/flatpak.rs | 2 +- src/backends/pipx.rs | 2 +- src/backends/rustup.rs | 2 +- src/backends/winget.rs | 2 +- src/backends/xbps.rs | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/backends/apt.rs b/src/backends/apt.rs index 5969f81..ba5fa63 100644 --- a/src/backends/apt.rs +++ b/src/backends/apt.rs @@ -27,7 +27,7 @@ impl Backend for Apt { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/arch.rs b/src/backends/arch.rs index 7032711..042b4d4 100644 --- a/src/backends/arch.rs +++ b/src/backends/arch.rs @@ -27,7 +27,7 @@ impl Backend for Arch { mut packages: BTreeMap, config: &Config, ) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } @@ -94,7 +94,7 @@ impl Backend for Arch { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/brew.rs b/src/backends/brew.rs index d95a659..f9d22fa 100644 --- a/src/backends/brew.rs +++ b/src/backends/brew.rs @@ -27,7 +27,7 @@ impl Backend for Brew { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/cargo.rs b/src/backends/cargo.rs index c5954f0..5ca7f21 100644 --- a/src/backends/cargo.rs +++ b/src/backends/cargo.rs @@ -46,7 +46,7 @@ impl Backend for Cargo { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/dnf.rs b/src/backends/dnf.rs index ca01df4..c2ebb85 100644 --- a/src/backends/dnf.rs +++ b/src/backends/dnf.rs @@ -31,7 +31,7 @@ impl Backend for Dnf { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/flatpak.rs b/src/backends/flatpak.rs index 468ebf6..64ea542 100644 --- a/src/backends/flatpak.rs +++ b/src/backends/flatpak.rs @@ -30,7 +30,7 @@ impl Backend for Flatpak { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/pipx.rs b/src/backends/pipx.rs index c42ddb3..c503fe1 100644 --- a/src/backends/pipx.rs +++ b/src/backends/pipx.rs @@ -32,7 +32,7 @@ impl Backend for Pipx { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/rustup.rs b/src/backends/rustup.rs index 16bfa22..6b98f54 100644 --- a/src/backends/rustup.rs +++ b/src/backends/rustup.rs @@ -35,7 +35,7 @@ impl Backend for Rustup { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/winget.rs b/src/backends/winget.rs index bfdd065..80ffd39 100644 --- a/src/backends/winget.rs +++ b/src/backends/winget.rs @@ -29,7 +29,7 @@ impl Backend for WinGet { } fn query_installed_packages(config: &Config) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } diff --git a/src/backends/xbps.rs b/src/backends/xbps.rs index ee3d014..14f1334 100644 --- a/src/backends/xbps.rs +++ b/src/backends/xbps.rs @@ -31,7 +31,7 @@ impl Backend for Xbps { fn query_installed_packages( config: &Config, ) -> Result> { - if Self::version(config).is_ok() { + if Self::version(config).is_err() { return Ok(BTreeMap::new()); } From e81ea24e889608aaed918363a8480d391085c548 Mon Sep 17 00:00:00 2001 From: ripytide Date: Sun, 17 Nov 2024 16:40:12 +0000 Subject: [PATCH 4/4] fix: prevent pipx noisy output --- src/backends/pipx.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backends/pipx.rs b/src/backends/pipx.rs index c503fe1..f7550d9 100644 --- a/src/backends/pipx.rs +++ b/src/backends/pipx.rs @@ -39,7 +39,7 @@ impl Backend for Pipx { let names = extract_package_names(run_command_for_stdout( ["pipx", "list", "--json"], Perms::Same, - false, + true, )?)?; Ok(names