#![doc = doc_self!()]
use std::sync::LazyLock;
use async_trait::async_trait;
use indoc::indoc;
use tap::prelude::*;
use super::{NoCacheStrategy, Pm, PmHelper, PromptStrategy, Strategy};
use crate::{config::Config, error::Result, exec::Cmd};
macro_rules! doc_self {
() => {
indoc! {r"
The [Alpine Linux package management system](https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management).
"}
};
}
use doc_self;
#[doc = doc_self!()]
#[derive(Debug)]
pub struct Apk {
cfg: Config,
}
static STRAT_PROMPT: LazyLock<Strategy> = LazyLock::new(|| Strategy {
prompt: PromptStrategy::CustomPrompt,
..Strategy::default()
});
static STRAT_INSTALL: LazyLock<Strategy> = LazyLock::new(|| Strategy {
prompt: PromptStrategy::CustomPrompt,
no_cache: NoCacheStrategy::with_flags(["--no-cache"]),
..Strategy::default()
});
impl Apk {
#[must_use]
#[allow(missing_docs)]
pub const fn new(cfg: Config) -> Self {
Self { cfg }
}
}
#[async_trait]
impl Pm for Apk {
fn name(&self) -> &'static str {
"apk"
}
fn cfg(&self) -> &Config {
&self.cfg
}
async fn q(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
if kws.is_empty() {
self.run(Cmd::new(["apk", "info"]).flags(flags)).await
} else {
self.qs(kws, flags).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(["apk", "info", "-L"]).kws(kws).flags(flags))
.await
}
async fn qo(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["apk", "info", "--who-owns"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn qs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.search_regex(Cmd::new(["apk", "info", "-d"]).flags(flags), kws)
.await
}
async fn qu(&self, _kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["apk", "version", "-l", "<"]).flags(flags))
.await
}
async fn r(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["apk", "del"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn rn(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["apk", "del", "--purge"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn rns(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["apk", "del", "--purge", "-r"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn rs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.r(kws, flags).await
}
async fn s(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["apk", "add"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_INSTALL))
.await
}
async fn sc(&self, _kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["apk", "cache", "-v", "clean"])
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn scc(&self, _kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["rm", "-vrf", "/var/cache/apk/*"])
.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(["apk", "info", "-a"]).kws(kws).flags(flags))
.await
}
async fn sii(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["apk", "info", "-r"]).kws(kws).flags(flags))
.await
}
async fn sl(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["apk", "search"]).kws(kws).flags(flags))
.await
}
async fn ss(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["apk", "search", "-v"]).kws(kws).flags(flags))
.await
}
async fn su(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(if kws.is_empty() {
&["apk", "upgrade"][..]
} else {
&["apk", "add", "-u"][..]
})
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_INSTALL))
.await
}
async fn suy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(if kws.is_empty() {
["apk", "upgrade", "-U", "-a"]
} else {
["apk", "add", "-U", "-u"]
})
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_INSTALL))
.await
}
async fn sw(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["apk", "fetch"])
.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(["apk", "update"]).kws(kws).flags(flags))
.await?;
if !kws.is_empty() {
self.s(kws, flags).await?;
}
Ok(())
}
async fn u(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::with_sudo(["apk", "add", "--allow-untrusted"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_INSTALL))
.await
}
}