Skip to content

Commit

Permalink
add disabled_backends, deduplicate the arch package managers
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Sep 29, 2024
1 parent 2e49e23 commit 9632d67
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 245 deletions.
39 changes: 0 additions & 39 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ rust-version = "1.78"
[dependencies]
color-eyre = "0.6.3"
clap = { version = "4.5.18", features = ["derive"] }
path-absolutize = "3.1.1"
regex = { version = "1.10.6", default-features = false, features = ["std"] }
walkdir = "2.5.0"
libc = "0.2.159"
log = { version = "0.4.22", features = ["std"] }
serde = { version = "1.0.210", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ can be set. The listed values are the defaults.
```toml
aur_helper = "paru" # AUR helper to use on Arch Linux (paru, yay, ...)
aur_rm_args = [] # additional args to pass to AUR helper when removing packages (optional)
arch_rm_args = [] # additional args to pass to AUR helper when removing packages (optional)
disabled_backends = [] # backends that pacdef should not manage, e.g. ["python"], this can reduce runtime if the package manager is notoriously slow (like pip)
warn_not_symlinks = true # warn if a group file is not a symlink
Expand Down
16 changes: 12 additions & 4 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Which arch package manager to use. For example: pacman, paru, yay,
# etc..
arch_package_manager = "paru"

# Extra arguments passed to pacman when removing an arch package.
aur_rm_args = [""]
arch_rm_args = [""]

# Whether to install flatpak packages systemwide or for the current user.
flatpak_systemwide = true

# Backends to disable from all pacdef behavior. See the README.md for
# the list of backend names
disabled_backends = ["pip"]

# Which group files apply for which hostnames
[hostname_groups]
pc = ["example_config"]
laptop = ["example_config"]
server = ["example_config"]
pc = ["example_group"]
laptop = ["example_group"]
server = ["example_group"]
26 changes: 22 additions & 4 deletions src/backends/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ macro_rules! query_infos {
pub fn query_installed_packages(config: &Config) -> Result<Self> {
Ok(Self {
$(
$backend: $backend::query_installed_packages(config)?,
$backend:
if is_enabled(&$backend.to_string(), config) {
$backend::query_installed_packages(config)?
} else {
Default::default()
},
)*
})
}
Expand All @@ -138,7 +143,9 @@ macro_rules! install_options {

pub fn install_packages(self, no_confirm: bool, config: &Config) -> Result<()> {
$(
$backend::install_packages(&self.$backend, no_confirm, config)?;
if is_enabled(&$backend.to_string(), config) {
$backend::install_packages(&self.$backend, no_confirm, config)?;
}
)*

Ok(())
Expand All @@ -164,7 +171,9 @@ macro_rules! modification_options {

pub fn modify_packages(self, config: &Config) -> Result<()> {
$(
$backend::modify_packages(&self.$backend, config)?;
if is_enabled(&$backend.to_string(), config) {
$backend::modify_packages(&self.$backend, config)?;
}
)*

Ok(())
Expand All @@ -190,7 +199,9 @@ macro_rules! remove_options {

pub fn remove_packages(self, no_confirm: bool, config: &Config) -> Result<()> {
$(
$backend::remove_packages(&self.$backend, no_confirm, config)?;
if is_enabled(&$backend.to_string(), config) {
$backend::remove_packages(&self.$backend, no_confirm, config)?;
}
)*

Ok(())
Expand All @@ -199,3 +210,10 @@ macro_rules! remove_options {
};
}
apply_public_backends!(remove_options);

fn is_enabled(backend: &str, config: &Config) -> bool {
!config
.disabled_backends
.iter()
.any(|x| x.to_lowercase() == backend.to_lowercase())
}
53 changes: 30 additions & 23 deletions src/backends/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ 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)]
pub struct Arch {
pub command: &'static str,
}
pub struct Arch;

pub type ArchPackageId = String;

Expand All @@ -37,16 +35,25 @@ pub struct ArchRemoveOptions {}
impl Arch {
pub fn query_installed_packages(
&self,
_: &Config,
config: &Config,
) -> Result<BTreeMap<ArchPackageId, ArchQueryInfo>> {
if !command_found("pacman") {
if !command_found(&config.arch_package_manager) {
return Ok(BTreeMap::new());
}

let explicit =
run_command_for_stdout(["pacman", "--query", "--explicit", "--quiet"], Perms::Same)?;
let dependency =
run_command_for_stdout(["pacman", "--query", "--deps", "--quiet"], Perms::Same)?;
let explicit = run_command_for_stdout(
[
&config.arch_package_manager,
"--query",
"--explicit",
"--quiet",
],
Perms::Same,
)?;
let dependency = run_command_for_stdout(
[&config.arch_package_manager, "--query", "--deps", "--quiet"],
Perms::Same,
)?;

Ok(dependency
.lines()
Expand All @@ -63,10 +70,10 @@ impl Arch {
&self,
packages: &BTreeMap<ArchPackageId, ArchInstallOptions>,
no_confirm: bool,
_: &Config,
config: &Config,
) -> Result<()> {
run_command(
[self.command, "--sync"]
[&config.arch_package_manager, "--sync"]
.into_iter()
.chain(Some("--no_confirm").filter(|_| no_confirm))
.chain(packages.keys().map(String::as_str))
Expand All @@ -80,15 +87,17 @@ impl Arch {
pub fn modify_packages(
&self,
packages: &BTreeMap<ArchPackageId, ArchModificationOptions>,
_: &Config,
config: &Config,
) -> Result<()> {
run_command(
[self.command, "--database", "--asdeps"].into_iter().chain(
packages
.iter()
.filter(|(_, m)| m.make_implicit)
.map(|(p, _)| p.as_str()),
),
[&config.arch_package_manager, "--database", "--asdeps"]
.into_iter()
.chain(
packages
.iter()
.filter(|(_, m)| m.make_implicit)
.map(|(p, _)| p.as_str()),
),
Perms::AsRoot,
)
}
Expand All @@ -100,9 +109,9 @@ impl Arch {
config: &Config,
) -> Result<()> {
run_command(
[self.command, "--remove", "--recursive"]
[&config.arch_package_manager, "--remove", "--recursive"]
.into_iter()
.chain(config.aur_rm_args.iter().map(String::as_str))
.chain(config.arch_rm_args.iter().map(String::as_str))
.chain(Some("--no_confirm").filter(|_| no_confirm))
.chain(packages.keys().map(String::as_str)),
Perms::AsRoot,
Expand All @@ -119,9 +128,7 @@ impl Arch {
x.clone().try_into::<StringPackageStruct>()?.package,
x.clone().try_into()?,
)),
_ => Err(eyre!(
"pacman/yay/paru packages must be either a string or a table"
)),
_ => Err(eyre!("arch packages must be either a string or a table")),
}
}
}
48 changes: 0 additions & 48 deletions src/backends/pacman.rs

This file was deleted.

48 changes: 0 additions & 48 deletions src/backends/paru.rs

This file was deleted.

Loading

0 comments on commit 9632d67

Please sign in to comment.