Skip to content

Commit

Permalink
Merge pull request #50 from InnocentZero/backends_command
Browse files Browse the repository at this point in the history
add version subcommand
  • Loading branch information
InnocentZero authored Nov 19, 2024
2 parents 3914d03 + e81ea24 commit 6c1b844
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 83 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
8 changes: 7 additions & 1 deletion src/backends/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ 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,)*
}
impl AnyBackend {
pub fn version(&self, config: &Config) -> Result<String> {
match self {
$( AnyBackend::$upper_backend => $upper_backend::version(config), )*
}
}
pub fn remove_packages(&self, packages: &BTreeSet<String>, no_confirm: bool, config: &Config) -> Result<()> {
match self {
$( AnyBackend::$upper_backend => $upper_backend::remove_packages(packages, no_confirm, config), )*
Expand All @@ -48,6 +53,7 @@ macro_rules! any {
}
};
}

apply_public_backends!(any);

macro_rules! raw_package_ids {
Expand Down
12 changes: 8 additions & 4 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_err() {
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 @@ -76,4 +76,8 @@ impl Backend for Apt {

Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["apt", "--version"], Perms::Same, false)
}
}
18 changes: 15 additions & 3 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_err() {
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_err() {
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 @@ -187,4 +191,12 @@ impl Backend for Arch {

Ok(())
}

fn version(config: &Config) -> Result<String> {
run_command_for_stdout(
[config.arch_package_manager.as_command(), "--version"],
Perms::Same,
false,
)
}
}
11 changes: 8 additions & 3 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_err() {
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 @@ -71,4 +72,8 @@ impl Backend for Brew {

Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["brew", "--version"], Perms::Same, false)
}
}
10 changes: 7 additions & 3 deletions src/backends/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{run_command, run_command_for_stdout};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
Expand Down Expand Up @@ -45,8 +45,8 @@ impl Backend for Cargo {
Ok(packages)
}

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

Expand Down Expand Up @@ -113,6 +113,10 @@ impl Backend for Cargo {

Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["cargo", "--version"], Perms::Same, false)
}
}

fn extract_packages(contents: &str) -> Result<BTreeMap<String, CargoQueryInfo>> {
Expand Down
Loading

0 comments on commit 6c1b844

Please sign in to comment.