From 7a5681647204a91d7616164a3790342631d2a575 Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 17 Jul 2023 20:40:01 +0200 Subject: [PATCH] refactor: change name from builder to installer --- pkgr/src/commands.rs | 4 +- pkgr/src/logging.rs | 57 +++++++++++++++++++ pkgr/src/main.rs | 44 +++----------- .../package/{builder => installer}/errors.rs | 0 .../src/package/{builder => installer}/mod.rs | 4 +- pkgr/src/package/mod.rs | 2 +- pkgr/src/{tmp.rs => tmpfs.rs} | 0 7 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 pkgr/src/logging.rs rename pkgr/src/package/{builder => installer}/errors.rs (100%) rename pkgr/src/package/{builder => installer}/mod.rs (97%) rename pkgr/src/{tmp.rs => tmpfs.rs} (100%) diff --git a/pkgr/src/commands.rs b/pkgr/src/commands.rs index e0f7ec0..cc3dfcf 100644 --- a/pkgr/src/commands.rs +++ b/pkgr/src/commands.rs @@ -3,7 +3,7 @@ use crate::package::identifier::PackageIdentifier; use clap::{Parser, Subcommand}; use log::{debug, error, info, trace, warn}; use manifest::Manifest; -use crate::package::builder::{InstallType, PackageInstaller}; +use crate::package::installer::{InstallType, PackageInstaller}; use crate::package::fetch::fetch_package; #[derive(Parser, Debug)] @@ -56,6 +56,7 @@ impl Command { .duration_since(std::time::UNIX_EPOCH) .unwrap(); + info!("Parsing package..."); trace!("Fetching package: {}", package_identifier); let pkgfile = fetch_package(package_identifier.clone()).unwrap(); debug!("size: manifest({}kb) data({}kb)", pkgfile.manifest.len() / 1024, pkgfile.data.len() / 1024); @@ -64,6 +65,7 @@ impl Command { debug!("manifest pkg name: {}", manifest.package.name); trace!("creating installer"); let installer = PackageInstaller::new(manifest.clone(), pkgfile, if *build { InstallType::Build } else { InstallType::Bin }); + info!("Installing: {}...", &manifest.package.name); trace!("starting install"); match installer.install() { diff --git a/pkgr/src/logging.rs b/pkgr/src/logging.rs new file mode 100644 index 0000000..6ee98a5 --- /dev/null +++ b/pkgr/src/logging.rs @@ -0,0 +1,57 @@ +use log::SetLoggerError; +use fern::Dispatch; +use std::env; +use colored::Colorize; + +pub fn setup_logger() -> Result<(), SetLoggerError> { + Dispatch::new() + .format(|out, message, record| { + match record.metadata().target() { + "command:stdout" => { + out.finish(format_args!( + "{} {}", + "::".cyan(), + message.to_string() + )); + return; + } + "command:stderr" => { + out.finish(format_args!( + "{} {}", + "!!".red(), + message.to_string() + )); + return; + } + _ => { + out.finish(format_args!( + "{} {}", + // Some logic so messages look nice + if message.to_string().len() > 0 { + match record + .level() + .to_string() + .chars() + .nth(0) + .unwrap_or('T') + { + 'T' => "[TRACE]".bright_blue(), + 'D' => "??".green(), + 'I' => "=>".blue(), + 'W' => "##".yellow(), + 'E' => "!!".red(), + _ => "**".blue(), + }.to_string() + } else { "".to_string() }, + message.to_string().bright_white() + )) + } + } + }) + .level(env::var("PKGR_LOG_LEVEL") + .unwrap_or_else(|_| "info".to_string()) + .parse() + .unwrap_or(log::LevelFilter::Info)) + .chain(std::io::stdout()) + .apply() +} diff --git a/pkgr/src/main.rs b/pkgr/src/main.rs index ba5f97d..aaa5387 100644 --- a/pkgr/src/main.rs +++ b/pkgr/src/main.rs @@ -1,15 +1,18 @@ +use std::process::Command; use clap::Parser; use colored::Colorize; use crate::commands::Cli; -use log::{SetLoggerError, trace}; -use std::env; +use log::trace; +use crate::process::Process; +mod process; mod commands; mod package; -mod tmp; +mod tmpfs; +mod logging; fn main() { - setup_logger().expect("Unable to setup logger."); + logging::setup_logger().expect("Unable to setup logger."); #[cfg(not(debug_assertions))] { @@ -31,36 +34,3 @@ fn main() { trace!("Exiting..."); } - -fn setup_logger() -> Result<(), SetLoggerError> { - fern::Dispatch::new() - .format(|out, message, record| { - out.finish(format_args!( - "{} {}", - // Some logic so messages look nice - if message.to_string().len() > 0 { - match record - .level() - .to_string() - .chars() - .nth(0) - .unwrap_or('T') - { - 'T' => "[TRACE]".cyan(), - 'D' => "??".green(), - 'I' => "=>".blue(), - 'W' => "##".yellow(), - 'E' => "!!".red(), - _ => "**".blue(), - }.to_string() - } else { "".to_string() }, - message.to_string().bright_white() - )) - }) - .level(env::var("PKGR_LOG_LEVEL") - .unwrap_or_else(|_| "info".to_string()) - .parse() - .unwrap_or(log::LevelFilter::Info)) - .chain(std::io::stdout()) - .apply() -} diff --git a/pkgr/src/package/builder/errors.rs b/pkgr/src/package/installer/errors.rs similarity index 100% rename from pkgr/src/package/builder/errors.rs rename to pkgr/src/package/installer/errors.rs diff --git a/pkgr/src/package/builder/mod.rs b/pkgr/src/package/installer/mod.rs similarity index 97% rename from pkgr/src/package/builder/mod.rs rename to pkgr/src/package/installer/mod.rs index b841a78..53602c6 100644 --- a/pkgr/src/package/builder/mod.rs +++ b/pkgr/src/package/installer/mod.rs @@ -3,9 +3,9 @@ use log::{debug, trace}; use errors::{BinError, BuildError, InstallError}; use manifest::Manifest; use pkgfile::PKGFile; -use crate::tmp::TempDir; +use crate::tmpfs::TempDir; -mod errors; +pub mod errors; #[derive(Debug)] pub enum InstallType { diff --git a/pkgr/src/package/mod.rs b/pkgr/src/package/mod.rs index 5c559fc..f838f7e 100644 --- a/pkgr/src/package/mod.rs +++ b/pkgr/src/package/mod.rs @@ -1,3 +1,3 @@ pub mod identifier; -pub mod builder; +pub mod installer; pub mod fetch; \ No newline at end of file diff --git a/pkgr/src/tmp.rs b/pkgr/src/tmpfs.rs similarity index 100% rename from pkgr/src/tmp.rs rename to pkgr/src/tmpfs.rs