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};
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 {
pub struct PacmanInstallBuilder {
packages: Vec<String>,
as_deps: bool,
no_confirm: bool,
}
impl PacmanInstallArgs {
impl PacmanInstallBuilder {
pub fn from_options(options: Options) -> Self {
Self::default()
.as_deps(options.asdeps)
@ -50,4 +32,19 @@ impl PacmanInstallArgs {
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;
mod args;
mod builder;
mod internal;
mod operations;
mod wrapper;
#[tokio::main(flavor = "current_thread")]
async fn main() {

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

Loading…
Cancel
Save