Add wrapper for pacman install

Signed-off-by: trivernis <trivernis@protonmail.com>
i18n
trivernis 2 years ago
parent 43e55480b8
commit adeeb75ba6
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -11,6 +11,7 @@ pub mod error;
pub mod exit_code;
pub mod fs_utils;
mod initialise;
pub mod resolve;
pub mod rpc;
mod sort;
pub mod structs;

@ -0,0 +1,14 @@
use alpm::Alpm;
use lazy_static::lazy_static;
use pacmanconf::Config;
fn get_alpm() -> Alpm {
alpm_utils::alpm_with_conf(get_pacman_config()).unwrap()
}
fn get_pacman_config() -> &'static Config {
lazy_static! {
static ref PACMAN_CONF: Config = Config::new().unwrap();
}
&PACMAN_CONF
}

@ -7,9 +7,9 @@ use crate::args::{InstallArgs, Operation, QueryArgs, RemoveArgs, SearchArgs};
use crate::internal::detect;
use crate::internal::exit_code::AppExitCode;
use crate::internal::{init, sort, start_sudoloop, structs::Options};
use std::str::FromStr;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::EnvFilter;
use std::str::FromStr;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
@ -17,6 +17,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
mod args;
mod internal;
mod operations;
mod wrapper;
#[tokio::main(flavor = "current_thread")]
async fn main() {
@ -64,8 +65,7 @@ async fn main() {
/// Can be used for debug purposes _or_ verbose output
fn init_logger() {
const DEFAULT_ENV_FILTER: &str = "warn";
let filter_string =
std::env::var("AME_LOG").unwrap_or_else(|_| DEFAULT_ENV_FILTER.to_string());
let filter_string = std::env::var("AME_LOG").unwrap_or_else(|_| DEFAULT_ENV_FILTER.to_string());
let env_filter =
EnvFilter::from_str(&*filter_string).expect("failed to parse env filter string");
tracing_subscriber::fmt::SubscriberBuilder::default()

@ -1,17 +1,9 @@
use crate::internal::commands::ShellCommand;
use crate::internal::error::SilentUnwrap;
use crate::internal::exit_code::AppExitCode;
use crate::wrapper::pacman::{PacmanInstallArgs, PacmanWrapper};
use crate::{crash, info, log, Options};
pub async fn install(packages: Vec<String>, options: Options) {
info!("Installing packages {} from repos", &packages.join(", "));
let mut opers = vec!["-S", "--needed"];
if options.noconfirm {
opers.push("--noconfirm");
}
if options.asdeps {
opers.push("--asdeps");
}
let verbosity = options.verbosity;
if !packages.is_empty() {
@ -19,14 +11,11 @@ pub async fn install(packages: Vec<String>, options: Options) {
log!("Installing from repos: {:?}", &packages);
}
let status = ShellCommand::pacman()
.elevated()
.args(opers)
.args(&packages)
.wait()
.await
.silent_unwrap(AppExitCode::PacmanError);
if !status.success() {
let result = PacmanWrapper::install(
PacmanInstallArgs::from_options(options).packages(packages.clone()),
)
.await;
if result.is_err() {
crash!(
AppExitCode::PacmanError,
"An error occured while installing packages: {}, aborting",

@ -0,0 +1 @@
pub mod pacman;

@ -0,0 +1,53 @@
use crate::internal::{commands::ShellCommand, error::AppResult, structs::Options};
pub struct PacmanWrapper;
impl PacmanWrapper {
pub async fn install(args: PacmanInstallArgs) -> AppResult<()> {
let mut command = ShellCommand::pacman().elevated().arg("-S").arg("--needed");
if args.no_confirm {
command = command.arg("--noconfirm")
}
if args.as_deps {
command = command.arg("--asdeps")
}
command.args(args.packages).wait_success().await
}
}
#[derive(Debug, Default)]
pub struct PacmanInstallArgs {
packages: Vec<String>,
as_deps: bool,
no_confirm: bool,
}
impl PacmanInstallArgs {
pub fn from_options(options: Options) -> Self {
Self::default()
.as_deps(options.asdeps)
.no_confirm(options.noconfirm)
}
pub fn packages<I: IntoIterator<Item = String>>(mut self, packages: I) -> Self {
let mut packages = packages.into_iter().collect();
self.packages.append(&mut packages);
self
}
pub fn no_confirm(mut self, no_confirm: bool) -> Self {
self.no_confirm = no_confirm;
self
}
pub fn as_deps(mut self, as_deps: bool) -> Self {
self.as_deps = as_deps;
self
}
}
Loading…
Cancel
Save