refactor: changed structure.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Didier Slof 2023-08-06 00:19:40 +02:00
parent 6335be4b04
commit 759c219f96
Signed by: didier
GPG key ID: 01E71F18AA4398E5
8 changed files with 113 additions and 89 deletions

View file

@ -7,6 +7,7 @@ use log::{debug, error, info, trace, warn};
use std::process::exit;
use crate::CONFIG;
use crate::process::Process;
use crate::types::fetch::TryFetch;
#[derive(Parser, Debug)]
#[clap(name = "pkgr", version)]
@ -60,7 +61,7 @@ impl Command {
info!("Parsing package...");
trace!("Fetching package: {}", package_identifier);
let mut pkg = Package::fetch(package_identifier.clone()).unwrap();
let mut pkg = Package::try_fetch(package_identifier.clone()).unwrap();
debug!("manifest size: {}kb", pkg.pkgfile.manifest.len() / 1024);
debug!("files size: {}kb", pkg.pkgfile.data.len() / 1024);

View file

@ -4,13 +4,13 @@ use clap::Parser;
use log::trace;
mod commands;
mod logging;
mod package;
mod process;
mod tmpfs;
mod config;
mod types;
thread_local! {
static CONFIG: config::Config = config::Config::from_path("/etc/pkgr.toml")

View file

@ -1,55 +0,0 @@
use crate::package::identifier::PackageLocator;
use pkgfile::PKGFile;
use reqwest::blocking::get;
use std::error::Error;
use std::fmt::Display;
use std::io::Read;
#[derive(Debug)]
pub enum FetchError {
HTTPError(reqwest::Error),
IOError(std::io::Error),
ParseError,
}
impl Display for FetchError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
FetchError::HTTPError(e) => write!(f, "HTTP Error: {}", e),
FetchError::IOError(e) => write!(f, "IO Error: {}", e),
FetchError::ParseError => write!(f, "Parse Error"),
}
}
}
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))
}
pub fn fetch_by_uri<S: Into<String>>(uri: S) -> Result<PKGFile, FetchError> {
// get file contents as bytes
let mut bytes = Vec::new();
match get(uri.into()) {
Ok(response) => {
match response.take(1024 * 1024).read_to_end(&mut bytes) {
Ok(_) => (),
Err(e) => return Err(FetchError::IOError(e)),
};
}
Err(e) => return Err(FetchError::HTTPError(e)),
};
// parse bytes as PKGFile
match PKGFile::try_from(bytes) {
Ok(pkgfile) => Ok(pkgfile),
Err(_e) => Err(FetchError::ParseError),
}
}
pub fn fetch_by_package_locator(_package_locator: PackageLocator) -> Result<PKGFile, FetchError> {
// TODO: search index for package locator
Ok(PKGFile::default())
}

View file

@ -8,6 +8,7 @@ use std::process::exit;
use crate::CONFIG;
use crate::package::identifier::{PackageIdentifier, PackageLocator};
use crate::package::Package;
use crate::types::fetch::TryFetch;
pub mod errors;
@ -67,7 +68,7 @@ impl PackageInstaller {
let build_manifest = self.manifest.build.clone().unwrap();
// TODO: Check dependencies
for pkg_tuple in build_manifest.dependencies {
let mut pkg = Package::fetch(
let mut pkg = Package::try_fetch(
PackageIdentifier::PackageLocator(
PackageLocator::from(pkg_tuple)
)

View file

@ -1,6 +1,11 @@
use std::fmt::Display;
use std::path::Path;
use log::{info, trace};
use reqwest::blocking::get;
use pkgfile::PKGFile;
use crate::package::identifier::PackageIdentifier;
use crate::types::fetch::TryFetch;
pub mod fetch;
pub mod identifier;
pub mod installer;
@ -13,7 +18,7 @@ pub struct Package {
impl Package {
/// Create a new package from a package identifier and a package file.
pub fn new(identifier: identifier::PackageIdentifier, pkgfile: pkgfile::PKGFile) -> Package {
pub fn new(identifier: PackageIdentifier, pkgfile: PKGFile) -> Package {
Package {
identifier,
pkgfile,
@ -22,29 +27,6 @@ impl Package {
}
}
/// Fetch a package from a package identifier.
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()
@ -96,3 +78,54 @@ impl Package {
unimplemented!();
}
}
#[derive(Debug)]
pub enum FetchError {
HTTPError(reqwest::Error),
IOError(std::io::Error),
ParseError,
}
impl Display for FetchError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
FetchError::HTTPError(e) => write!(f, "HTTP Error: {}", e),
FetchError::IOError(e) => write!(f, "IO Error: {}", e),
FetchError::ParseError => write!(f, "Parse Error"),
}
}
}
impl TryFetch<PackageIdentifier> for Package {
type Error = FetchError;
/// Fetch a package from a package identifier.
fn try_fetch(query: PackageIdentifier) -> Result<Package, Self::Error> {
let pkgfile = match &query {
PackageIdentifier::Path(s) => match PKGFile::try_from(Path::new(&s)) {
Ok(p) => Ok(p),
Err(e) => Err(FetchError::ParseError)
},
PackageIdentifier::URI(s) => {
let mut bytes = Vec::new();
match get::<String>(s.into()) {
Ok(response) => {
if let Ok(b) = response.bytes() {
bytes.extend(b);
}
},
Err(e) => return Err(FetchError::HTTPError(e)),
};
// parse bytes as PKGFile
match PKGFile::try_from(bytes) {
Ok(pkgfile) => Ok(pkgfile),
Err(_e) => Err(FetchError::ParseError),
}
}
PackageIdentifier::PackageLocator(l) => Ok(PKGFile::default())
};
pkgfile
.map(|p| Package::new(query, p))
}
}

10
pkgr/src/types/fetch.rs Normal file
View file

@ -0,0 +1,10 @@
/// Get a result from an external source
pub trait Fetch<Q, R = Self> {
fn fetch(query: Q) -> R;
}
/// Try to get a result from an external source
pub trait TryFetch<Q, R = Self> {
type Error;
fn try_fetch(query: Q) -> Result<R, Self::Error>;
}

1
pkgr/src/types/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod fetch;