#![doc = doc_self!()]
use std::sync::LazyLock;
use async_trait::async_trait;
use futures::prelude::*;
use indoc::indoc;
use tap::prelude::*;
use super::{Pm, PmHelper, PromptStrategy, Strategy};
use crate::{config::Config, error::Result, exec::Cmd};
macro_rules! doc_self {
() => {
indoc! {"
The [PackageKit Console Client](https://www.freedesktop.org/software/PackageKit).
"}
};
}
use doc_self;
#[doc = doc_self!()]
#[derive(Debug)]
pub struct Pkcon {
cfg: Config,
}
static STRAT_PROMPT: LazyLock<Strategy> = LazyLock::new(|| Strategy {
prompt: PromptStrategy::native_no_confirm(["-y"]),
..Strategy::default()
});
impl Pkcon {
#[must_use]
#[allow(missing_docs)]
pub const fn new(cfg: Config) -> Self {
Self { cfg }
}
}
#[async_trait]
impl Pm for Pkcon {
fn name(&self) -> &'static str {
"pkcon"
}
fn cfg(&self) -> &Config {
&self.cfg
}
async fn q(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
if kws.is_empty() {
Cmd::new(["pkcon", "get-packages", "--filter", "installed"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
} else {
self.qs(kws, flags).await
}
}
async fn qc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["pkcon", "get-update-detail"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn qi(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.si(kws, flags).await
}
async fn qii(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.sii(kws, flags).await
}
async fn ql(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["pkcon", "get-files"]).kws(kws).flags(flags))
.await
}
async fn qo(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["pkcon", "what-provides"]).kws(kws).flags(flags))
.await
}
async fn qs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["pkcon", "get-packages", "--filter", "installed"])
.flags(flags)
.pipe(|cmd| self.search_regex(cmd, kws))
.await
}
async fn qu(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["pkcon", "get-updates"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn r(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
stream::iter(kws)
.map(Ok)
.try_for_each(|kw| {
Cmd::with_sudo(["pkcon", "remove"])
.kws([kw])
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
})
.await
}
async fn rs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
stream::iter(kws)
.map(Ok)
.try_for_each(|kw| {
Cmd::with_sudo(["pkcon", "remove", "--autoremove"])
.kws([kw])
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
})
.await
}
async fn s(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(if self.cfg.needed {
&["pkcon", "install"][..]
} else {
&["pkcon", "install", "--allow-reinstall"][..]
})
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn si(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["pkcon", "get-details"]).kws(kws).flags(flags))
.await
}
async fn sii(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["pkcon", "required-by"]).kws(kws).flags(flags))
.await
}
async fn ss(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["pkcon", "search", "name"]).kws(kws).flags(flags))
.await
}
async fn su(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["pkcon", "update"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn suy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.sy(&[], flags).await?;
self.su(kws, flags).await
}
async fn sw(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["pkcon", "install", "--only-download"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn sy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::with_sudo(["pkcon", "refresh"]).flags(flags))
.await?;
if !kws.is_empty() {
self.s(kws, flags).await?;
}
Ok(())
}
}