Change wrapper to builder pattern (fancy stuff)

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

@ -1,31 +1,13 @@
use crate::internal::{commands::ShellCommand, error::AppResult, structs::Options}; 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)] #[derive(Debug, Default)]
pub struct PacmanInstallArgs { pub struct PacmanInstallBuilder {
packages: Vec<String>, packages: Vec<String>,
as_deps: bool, as_deps: bool,
no_confirm: bool, no_confirm: bool,
} }
impl PacmanInstallArgs { impl PacmanInstallBuilder {
pub fn from_options(options: Options) -> Self { pub fn from_options(options: Options) -> Self {
Self::default() Self::default()
.as_deps(options.asdeps) .as_deps(options.asdeps)
@ -50,4 +32,19 @@ impl PacmanInstallArgs {
self self
} }
#[tracing::instrument(level = "debug")]
pub async fn install(self) -> AppResult<()> {
let mut command = ShellCommand::pacman().elevated().arg("-S").arg("--needed");
if self.no_confirm {
command = command.arg("--noconfirm")
}
if self.as_deps {
command = command.arg("--asdeps")
}
command.args(self.packages).wait_success().await
}
} }

@ -15,9 +15,9 @@ use tracing_subscriber::EnvFilter;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
mod args; mod args;
mod builder;
mod internal; mod internal;
mod operations; mod operations;
mod wrapper;
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {

@ -1,5 +1,5 @@
use crate::builder::pacman::PacmanInstallBuilder;
use crate::internal::exit_code::AppExitCode; use crate::internal::exit_code::AppExitCode;
use crate::wrapper::pacman::{PacmanInstallArgs, PacmanWrapper};
use crate::{crash, info, log, Options}; use crate::{crash, info, log, Options};
pub async fn install(packages: Vec<String>, options: Options) { pub async fn install(packages: Vec<String>, options: Options) {
@ -11,10 +11,11 @@ pub async fn install(packages: Vec<String>, options: Options) {
log!("Installing from repos: {:?}", &packages); log!("Installing from repos: {:?}", &packages);
} }
let result = PacmanWrapper::install( let result = PacmanInstallBuilder::from_options(options)
PacmanInstallArgs::from_options(options).packages(packages.clone()), .packages(packages.clone())
) .install()
.await; .await;
if result.is_err() { if result.is_err() {
crash!( crash!(
AppExitCode::PacmanError, AppExitCode::PacmanError,

Loading…
Cancel
Save