diff --git a/pkgr/src/commands.rs b/pkgr/src/commands.rs index d63e2e3..4393869 100644 --- a/pkgr/src/commands.rs +++ b/pkgr/src/commands.rs @@ -107,11 +107,11 @@ impl Command { error!("Update is not yet implemented."); } Command::Debug => { - trace!("Trace message."); - debug!("Debug message."); - info!("Info message."); - warn!("Warning message."); - error!("Error message."); + trace!("Trace message.\nWith newline."); + debug!("Debug message.\nWith newline."); + info!("Info message.\nWith newline."); + warn!("Warning message.\nWith newline."); + error!("Error message.\nWith newline."); info!(""); Process::command(vec!["sh", "-c", "echo whoami: $(whoami)"]) diff --git a/pkgr/src/config/mod.rs b/pkgr/src/config/mod.rs index 4bb3397..9a932d2 100644 --- a/pkgr/src/config/mod.rs +++ b/pkgr/src/config/mod.rs @@ -4,12 +4,15 @@ use serde::{Deserialize, Serialize}; pub struct Config { #[serde(default)] pub build_by_default: bool, + #[serde(default)] + pub tmp_dir: Option, } impl Default for Config { fn default() -> Config { Config { build_by_default: false, + tmp_dir: Some(String::from("/tmp/pkgr")) } } } diff --git a/pkgr/src/logging.rs b/pkgr/src/logging.rs index 225ef27..5e32d9d 100644 --- a/pkgr/src/logging.rs +++ b/pkgr/src/logging.rs @@ -1,40 +1,57 @@ use colored::Colorize; use fern::Dispatch; -use log::SetLoggerError; +use log::{Record, SetLoggerError}; use std::env; +fn format_regular>(log: S, record: &Record) -> String { + let log = log.into(); + let line_prefix = |line: String, extend: bool| { + let prefix = if extend { + match record.level() { + log::Level::Trace => " :".bright_blue(), + log::Level::Debug => " :".green(), + log::Level::Info => " :".blue(), + log::Level::Warn => " :".yellow(), + log::Level::Error => " :".red(), + }.to_string() + } else { + match record.level() { + log::Level::Trace => "[TRACE]".bright_blue(), + log::Level::Debug => "??".green(), + log::Level::Info => "=>".blue(), + log::Level::Warn => "##".yellow(), + log::Level::Error => "!!".red() + }.to_string() + }; + return format!("{} {}", prefix, line); + }; + + let mut lines = log.lines().peekable(); + let mut output = match lines.peek() { + Some(line) => line_prefix(lines.next().unwrap().to_string(), false), + None => return "".to_string(), + }; + + for line in lines { + output.push_str(&*format!("\n{}", line_prefix(line.to_string(), true))); + } + + output +} + 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())); + out.finish(format_args!("{} {}", ">>".cyan(), message.to_string())); return; } "command:stderr" => { - out.finish(format_args!("{} {}", "!!".red(), message.to_string())); + 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() - )) - } + _ => out.finish(format_args!("{}", format_regular(message.to_string(), record))), } }) .level( diff --git a/pkgr/src/package/identifier.rs b/pkgr/src/package/identifier.rs index 427b775..3fde5e7 100644 --- a/pkgr/src/package/identifier.rs +++ b/pkgr/src/package/identifier.rs @@ -7,15 +7,15 @@ use regex::Regex; pub enum PackageIdentifierError { InvalidPackageLocator(String), InvalidURI(String), + InvalidPath(String), } impl std::fmt::Display for PackageIdentifierError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - PackageIdentifierError::InvalidPackageLocator(s) => { - write!(f, "Invalid package locator: {}", s) - } + PackageIdentifierError::InvalidPackageLocator(s) => write!(f, "Invalid package locator: {}", s), PackageIdentifierError::InvalidURI(s) => write!(f, "Invalid URI: {}", s), + PackageIdentifierError::InvalidPath(s) => write!(f, "Invalid path: {}", s), } } } @@ -71,7 +71,18 @@ impl FromStr for PackageIdentifier { return Err(PackageIdentifierError::InvalidURI(s.to_string())); } Ok(PackageIdentifier::URI(s.to_string())) - } else if std::path::Path::new(s).exists() { + } else if s.starts_with("/") + || s.starts_with("./") + || s.starts_with("../") + || s.starts_with("~/") + { + let path = std::path::Path::new(s); + if s.ends_with("/") + || !path.exists() + || path.is_dir() + { + return Err(PackageIdentifierError::InvalidPath(s.to_string())); + } return Ok(PackageIdentifier::Path(s.to_string())); } else { let pl = match PackageLocator::from_str(s) { diff --git a/pkgr/src/package/installer/errors.rs b/pkgr/src/package/installer/errors.rs index 637e891..9e86f1b 100644 --- a/pkgr/src/package/installer/errors.rs +++ b/pkgr/src/package/installer/errors.rs @@ -37,8 +37,8 @@ pub enum InstallError { impl ToString for InstallError { fn to_string(&self) -> String { match self { - InstallError::BuildError(e) => format!("Build error: {}", e), - InstallError::BinError(e) => format!("Bin error: {}", e), + InstallError::BuildError(e) => format!("Build error: \n{}", e), + InstallError::BinError(e) => format!("Bin error: \n{}", e), InstallError::InvalidManifest => "Invalid manifest".to_string(), InstallError::Generic => "Install error".to_string(), } diff --git a/pkgr/src/package/installer/mod.rs b/pkgr/src/package/installer/mod.rs index 8181ea8..5da81f9 100644 --- a/pkgr/src/package/installer/mod.rs +++ b/pkgr/src/package/installer/mod.rs @@ -38,7 +38,10 @@ impl PackageInstaller { return Err(BinError::UnpackError("package has no data".to_string())); } if std::path::Path::new(&path).exists() { - return Err(BinError::UnpackError("path already exists".to_string())); + return Err(BinError::UnpackError(format!( + "path already exists: {}", + path + ))); } tar::Archive::new(self.pkgfile.data.as_slice()) .unpack(&path) diff --git a/pkgr/src/tmpfs.rs b/pkgr/src/tmpfs.rs index c9ea6f8..d21aff7 100644 --- a/pkgr/src/tmpfs.rs +++ b/pkgr/src/tmpfs.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use crate::CONFIG; pub struct TempDir { path: PathBuf, @@ -20,9 +21,11 @@ impl TempDir { impl Default for TempDir { fn default() -> TempDir { TempDir::new({ - let mut t = std::env::temp_dir(); - t.push("pkgr"); - t + if let Some(d) = CONFIG.with(|c| c.tmp_dir.clone()) { + PathBuf::from(d) + } else { + PathBuf::from("/tmp/pkgr") + } }) } }