Skip to content

Commit

Permalink
refactor: re-organize things
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Nov 16, 2024
1 parent cbd9fa9 commit c3a9a71
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 155 deletions.
76 changes: 43 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
17 changes: 1 addition & 16 deletions src/backends/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,)*
}
Expand All @@ -54,23 +54,8 @@ macro_rules! any {
};
}

macro_rules! versions {
($(($upper_backend:ident, $lower_backend:ident)), *) => {
pub fn backend_versions(config: &Config) -> BTreeMap<String, String> {
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)]
Expand Down
14 changes: 5 additions & 9 deletions src/backends/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -26,8 +26,8 @@ impl Backend for Apt {
Ok(packages)
}

fn query_installed_packages(_: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
if !command_found("apt-mark") {
fn query_installed_packages(config: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
if Self::version(config).is_ok() {
return Ok(BTreeMap::new());
}

Expand All @@ -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()
Expand Down Expand Up @@ -78,10 +78,6 @@ impl Backend for Apt {
}

fn version(_: &Config) -> Result<String> {
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)
}
}
23 changes: 12 additions & 11 deletions src/backends/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -27,7 +27,7 @@ impl Backend for Arch {
mut packages: BTreeMap<String, Self::InstallOptions>,
config: &Config,
) -> Result<BTreeMap<String, Self::InstallOptions>> {
if !command_found(config.arch_package_manager.as_command()) {
if Self::version(config).is_ok() {
return Ok(BTreeMap::new());
}

Expand All @@ -39,6 +39,7 @@ impl Backend for Arch {
"--quiet",
],
Perms::Same,
false,
)?;

for group in groups.lines() {
Expand All @@ -52,6 +53,7 @@ impl Backend for Arch {
group,
],
Perms::Same,
false,
)?;

for group_package in group_packages.lines() {
Expand Down Expand Up @@ -92,7 +94,7 @@ impl Backend for Arch {
}

fn query_installed_packages(config: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
if !command_found(config.arch_package_manager.as_command()) {
if Self::version(config).is_ok() {
return Ok(BTreeMap::new());
}

Expand All @@ -104,6 +106,7 @@ impl Backend for Arch {
"--quiet",
],
Perms::Same,
false,
)?;

let mut result = BTreeMap::new();
Expand Down Expand Up @@ -168,6 +171,7 @@ impl Backend for Arch {
"--quiet",
],
Perms::Same,
false,
)?;
let orphans = orphans_output.lines();

Expand All @@ -189,13 +193,10 @@ impl Backend for Arch {
}

fn version(config: &Config) -> Result<String> {
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,
)
}
}
13 changes: 5 additions & 8 deletions src/backends/brew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -26,14 +26,15 @@ impl Backend for Brew {
Ok(packages)
}

fn query_installed_packages(_: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
if !command_found("brew") {
fn query_installed_packages(config: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
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
Expand Down Expand Up @@ -73,10 +74,6 @@ impl Backend for Brew {
}

fn version(_: &Config) -> Result<String> {
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)
}
}
Loading

0 comments on commit c3a9a71

Please sign in to comment.