format: fix formatting

This commit is contained in:
Strix 2023-10-14 22:39:44 +02:00
parent e3552e9208
commit 9e102ffb52
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
12 changed files with 80 additions and 85 deletions

View file

@ -1,5 +1,5 @@
use std::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PKGR {}

View file

@ -1,10 +1,10 @@
use std::process::exit;
use crate::package::identifier::PackageIdentifier;
use crate::package::installer::{InstallType, PackageInstaller};
use crate::package::Package;
use clap::{Parser, Subcommand};
use log::{debug, error, info, trace, warn};
use manifest::Manifest;
use crate::package::installer::{InstallType, PackageInstaller};
use crate::package::Package;
use std::process::exit;
#[derive(Parser, Debug)]
#[clap(name = "pkgr", version)]
@ -86,9 +86,11 @@ impl Command {
info!("Install complete.");
info!("Install took {}ms.", duration.as_nanos() as f64 / 1000000.0);
}
Command::Remove { package_identifier, index } => {
Command::Remove {
package_identifier,
index,
} => {
if let PackageIdentifier::URI(_) = package_identifier {
error!("URI is unsupported when removing applications.");
exit(1);
@ -113,7 +115,10 @@ impl Command {
info!("PKGR AUTHORS: {}", env!("CARGO_PKG_AUTHORS"));
info!("PKGR DESCRIPTION: {}", env!("CARGO_PKG_DESCRIPTION"));
info!("");
info!("PKGR_LOG_LEVEL: {}", std::env::var("PKGR_LOG_LEVEL").unwrap_or_else(|_| "info".to_string()));
info!(
"PKGR_LOG_LEVEL: {}",
std::env::var("PKGR_LOG_LEVEL").unwrap_or_else(|_| "info".to_string())
);
}
Command::None => {
error!("No command was specified.");

View file

@ -1,26 +1,18 @@
use log::SetLoggerError;
use fern::Dispatch;
use std::env;
use colored::Colorize;
use fern::Dispatch;
use log::SetLoggerError;
use std::env;
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;
}
_ => {
@ -28,30 +20,29 @@ pub fn setup_logger() -> Result<(), SetLoggerError> {
"{} {}",
// Some logic so messages look nice
if message.to_string().len() > 0 {
match record
.level()
.to_string()
.chars()
.nth(0)
.unwrap_or('T')
{
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() },
}
.to_string()
} else {
"".to_string()
},
message.to_string().bright_white()
))
}
}
})
.level(env::var("PKGR_LOG_LEVEL")
.level(
env::var("PKGR_LOG_LEVEL")
.unwrap_or_else(|_| "info".to_string())
.parse()
.unwrap_or(log::LevelFilter::Info))
.unwrap_or(log::LevelFilter::Info),
)
.chain(std::io::stdout())
.apply()
}

View file

@ -1,15 +1,15 @@
use std::process::Command;
use crate::commands::Cli;
use crate::process::Process;
use clap::Parser;
use colored::Colorize;
use crate::commands::Cli;
use log::trace;
use crate::process::Process;
use std::process::Command;
mod process;
mod commands;
mod package;
mod tmpfs;
mod logging;
mod package;
mod process;
mod tmpfs;
fn main() {
logging::setup_logger().expect("Unable to setup logger.");

View file

@ -1,9 +1,9 @@
use crate::package::identifier::PackageLocator;
use pkgfile::PKGFile;
use reqwest::blocking::get;
use std::error::Error;
use std::fmt::Display;
use std::io::Read;
use pkgfile::PKGFile;
use reqwest::blocking::get;
use crate::package::identifier::PackageLocator;
#[derive(Debug)]
pub enum FetchError {
@ -26,9 +26,8 @@ impl Error for FetchError {}
pub fn fetch_by_path<S: Into<String>>(path: S) -> Result<PKGFile, FetchError> {
std::fs::read(path.into())
.map_err(|e| FetchError::IOError(e)).and_then(|bytes| {
PKGFile::try_from(bytes).map_err(|_| FetchError::ParseError)
})
.map_err(|e| FetchError::IOError(e))
.and_then(|bytes| PKGFile::try_from(bytes).map_err(|_| FetchError::ParseError))
}
pub fn fetch_by_uri<S: Into<String>>(uri: S) -> Result<PKGFile, FetchError> {

View file

@ -15,7 +15,7 @@ impl Display for BinError {
#[derive(Debug)]
pub enum BuildError {
InvalidManifest
InvalidManifest,
}
impl Display for BuildError {
@ -30,7 +30,7 @@ impl Display for BuildError {
pub enum InstallError {
BuildError(BuildError),
BinError(BinError),
Generic
Generic,
}
impl ToString for InstallError {

View file

@ -1,23 +1,23 @@
use std::fmt::Display;
use log::{debug, trace};
use crate::tmpfs::TempDir;
use errors::{BinError, BuildError, InstallError};
use log::{debug, trace};
use manifest::Manifest;
use pkgfile::PKGFile;
use crate::tmpfs::TempDir;
use std::fmt::Display;
pub mod errors;
#[derive(Debug)]
pub enum InstallType {
Build,
Bin
Bin,
}
#[derive(Debug)]
pub struct PackageInstaller {
manifest: Manifest,
pkgfile: PKGFile,
install_type: InstallType
install_type: InstallType,
}
impl PackageInstaller {
@ -25,7 +25,7 @@ impl PackageInstaller {
PackageInstaller {
manifest: m,
pkgfile: p,
install_type: i
install_type: i,
}
}
@ -46,8 +46,8 @@ impl PackageInstaller {
tmpdir.push(&self.manifest.package.name);
trace!("extracting package into: {}", tmpdir.to_string());
match self.extract_to(tmpdir.to_string()) {
Ok(_) => {},
Err(e) => return Err(e)
Ok(_) => {}
Err(e) => return Err(e),
}
debug!("extracted package in: {}", tmpdir.to_string());
Ok(())
@ -60,14 +60,13 @@ impl PackageInstaller {
let build_manifest = self.manifest.build.clone().unwrap();
// TODO: Check dependencies
Ok(())
}
pub fn install(&self) -> Result<(), InstallError> {
match self.install_type {
InstallType::Bin => self.bin().map_err(|e| InstallError::BinError(e)),
InstallType::Build => self.build().map_err(|e| InstallError::BuildError(e))
InstallType::Build => self.build().map_err(|e| InstallError::BuildError(e)),
}
}
}

View file

@ -1,8 +1,8 @@
use log::trace;
pub mod fetch;
pub mod identifier;
pub mod installer;
pub mod fetch;
pub struct Package {
pub identifier: identifier::PackageIdentifier,
@ -23,43 +23,47 @@ impl Package {
}
/// Fetch a package from a package identifier.
pub fn fetch(package_identifier: identifier::PackageIdentifier) -> Result<Package, fetch::FetchError> {
pub fn fetch(
package_identifier: identifier::PackageIdentifier,
) -> Result<Package, fetch::FetchError> {
match &package_identifier {
identifier::PackageIdentifier::Path(path) => {
trace!("fetching package from path: {}", path);
let pkgfile = fetch::fetch_by_path(path).unwrap();
Ok(Package::new(package_identifier, pkgfile))
},
}
identifier::PackageIdentifier::URI(url) => {
trace!("fetching package from uri: {}", url);
let pkgfile = fetch::fetch_by_uri(url).unwrap();
Ok(Package::new(package_identifier, pkgfile))
},
}
identifier::PackageIdentifier::PackageLocator(locator) => {
trace!("fetching package from locator: {}", locator);
let pkgfile = fetch::fetch_by_package_locator(locator.clone()).unwrap();
Ok(Package::new(package_identifier, pkgfile))
},
}
}
}
/// Get the package manifest.
pub fn manifest(&self) -> manifest::Manifest {
manifest::Manifest::try_from(self.pkgfile.manifest.clone())
.unwrap()
manifest::Manifest::try_from(self.pkgfile.manifest.clone()).unwrap()
}
/// Install the package.
pub fn install(&mut self) -> Result<(), installer::errors::InstallError> {
let manifest = self.manifest();
let installer = installer::PackageInstaller::new(manifest.clone(), self.pkgfile.clone(), installer::InstallType::Bin);
let installer = installer::PackageInstaller::new(
manifest.clone(),
self.pkgfile.clone(),
installer::InstallType::Bin,
);
match installer.install() {
Ok(_) => {
self.is_installed = true;
Ok(())
},
Err(e) => Err(e)
}
Err(e) => Err(e),
}
}

View file

@ -10,11 +10,7 @@ pub struct Process {
impl Process {
pub fn new(command: Vec<String>, cwd: Option<String>, env: Vec<String>) -> Process {
Process {
command,
cwd,
env,
}
Process { command, cwd, env }
}
pub fn command<S: Into<String>>(cmd: Vec<S>) -> Process {
@ -27,7 +23,10 @@ impl Process {
.current_dir(self.cwd.clone().unwrap_or(".".to_string()))
.envs(self.env.iter().map(|v| {
let mut split = v.split('=');
(split.next().unwrap().to_string(), split.next().unwrap().to_string())
(
split.next().unwrap().to_string(),
split.next().unwrap().to_string(),
)
}))
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())

View file

@ -1,10 +1,10 @@
// create functions that spawn threads for Stdout and Stderr
// that print the output to info!(target: "command:stdout", ...) and info!(target: "command:stderr", ...) respectively
use std::process::Child;
use std::io::{BufRead, BufReader};
use std::process::Child;
use log::{info, error};
use log::{error, info};
pub fn spawn_output_handlers(child: &mut Child) {
let stdout = child.stdout.take().unwrap();

View file

@ -9,9 +9,7 @@ impl TempDir {
if !path.exists() {
std::fs::create_dir_all(&path).unwrap();
}
TempDir {
path
}
TempDir { path }
}
pub fn push<S: Into<String>>(&mut self, path: S) {