mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-05 02:55:31 +00:00
WIP refactor
This commit is contained in:
parent
90ad9d8f8a
commit
77d7613967
@ -115,6 +115,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
kind: CommandKind::Compression(input_files),
|
||||
output: Some(File {
|
||||
path: output_file.into(),
|
||||
contents: None,
|
||||
extension: Some(output_file_extension.unwrap())
|
||||
}),
|
||||
});
|
||||
@ -130,6 +131,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
kind: CommandKind::Decompression(input_files),
|
||||
output: Some(File {
|
||||
path: output_file.into(),
|
||||
contents: None,
|
||||
extension: None
|
||||
})
|
||||
});
|
||||
|
@ -1,10 +1,7 @@
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{fs, io::{Cursor, Read}, path::{Path, PathBuf}};
|
||||
|
||||
use colored::Colorize;
|
||||
use tar;
|
||||
use tar::{self, Archive};
|
||||
|
||||
use crate::{error::OuchResult, utils};
|
||||
use crate::file::File;
|
||||
@ -15,12 +12,12 @@ pub struct TarDecompressor {}
|
||||
|
||||
impl TarDecompressor {
|
||||
|
||||
fn unpack_files(from: &Path, into: &Path) -> OuchResult<Vec<PathBuf>> {
|
||||
fn unpack_files(from: &File, into: &Path) -> OuchResult<Vec<PathBuf>> {
|
||||
|
||||
println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from);
|
||||
let mut files_unpacked = vec![];
|
||||
|
||||
let file = fs::File::open(from)?;
|
||||
let file = fs::File::open(&from.path)?;
|
||||
let mut archive = tar::Archive::new(file);
|
||||
|
||||
for file in archive.entries()? {
|
||||
@ -50,7 +47,7 @@ impl Decompressor for TarDecompressor {
|
||||
|
||||
utils::create_path_if_non_existent(destination_path)?;
|
||||
|
||||
let files_unpacked = Self::unpack_files(&from.path, destination_path)?;
|
||||
let files_unpacked = Self::unpack_files(&from, destination_path)?;
|
||||
|
||||
Ok(DecompressionResult::FilesUnpacked(files_unpacked))
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ impl ZipDecompressor {
|
||||
|
||||
let mut unpacked_files = vec![];
|
||||
|
||||
// placeholder return
|
||||
println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from);
|
||||
|
||||
let file = fs::File::open(from)?;
|
||||
|
@ -20,13 +20,7 @@ pub struct Evaluator {
|
||||
}
|
||||
|
||||
impl Evaluator {
|
||||
// todo: remove this?
|
||||
pub fn new(command: Command) -> Self {
|
||||
Self { command }
|
||||
}
|
||||
|
||||
fn get_decompressor(
|
||||
&self,
|
||||
file: &File,
|
||||
) -> error::OuchResult<(Option<Box<dyn Decompressor>>, Box<dyn Decompressor>)> {
|
||||
if file.extension.is_none() {
|
||||
@ -95,17 +89,17 @@ impl Evaluator {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decompress_file(&self, file: &File) -> error::OuchResult<()> {
|
||||
let output_file = &self.command.output;
|
||||
let (first_decompressor, second_decompressor) = self.get_decompressor(file)?;
|
||||
fn decompress_file(file: File, output: &Option<File>) -> error::OuchResult<()> {
|
||||
// let output_file = &command.output;
|
||||
let (first_decompressor, second_decompressor) = Self::get_decompressor(&file)?;
|
||||
|
||||
let decompression_result = second_decompressor.decompress(file, output_file)?;
|
||||
let decompression_result = second_decompressor.decompress(&file, output)?;
|
||||
|
||||
match decompression_result {
|
||||
DecompressionResult::FileInMemory(bytes) => {
|
||||
// We'll now decompress a file currently in memory.
|
||||
// This will currently happen in the case of .bz, .xz and .lzma
|
||||
Self::decompress_file_in_memory(bytes, file, first_decompressor, output_file)?;
|
||||
Self::decompress_file_in_memory(bytes, &file, first_decompressor, output)?;
|
||||
}
|
||||
DecompressionResult::FilesUnpacked(files) => {
|
||||
// If the file's last extension was an archival method,
|
||||
@ -121,8 +115,10 @@ impl Evaluator {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn evaluate(&mut self) -> error::OuchResult<()> {
|
||||
match &self.command.kind {
|
||||
pub fn evaluate(command: Command) -> error::OuchResult<()> {
|
||||
let output = command.output.clone();
|
||||
|
||||
match command.kind {
|
||||
CommandKind::Compression(files_to_compress) => {
|
||||
for _file in files_to_compress {
|
||||
todo!();
|
||||
@ -130,7 +126,7 @@ impl Evaluator {
|
||||
}
|
||||
CommandKind::Decompression(files_to_decompress) => {
|
||||
for file in files_to_decompress {
|
||||
self.decompress_file(file)?;
|
||||
Self::decompress_file(file, &output)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,13 @@ use std::path::PathBuf;
|
||||
use crate::extension::{CompressionFormat, Extension};
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct File {
|
||||
/// File's (relative) path
|
||||
pub path: PathBuf,
|
||||
/// The bytes that compose the file.
|
||||
/// Only used when the whole file is kept in-memory
|
||||
pub contents: Option<Vec<u8>>,
|
||||
/// Note: extension here might be a misleading name since
|
||||
/// we don't really care about any extension other than supported compression ones.
|
||||
///
|
||||
@ -19,6 +22,7 @@ impl From<(PathBuf, CompressionFormat)> for File {
|
||||
fn from((path, format): (PathBuf, CompressionFormat)) -> Self {
|
||||
Self {
|
||||
path,
|
||||
contents: None,
|
||||
extension: Some(Extension::from(format)),
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ fn main() -> OuchResult<()>{
|
||||
let matches = cli::get_matches();
|
||||
match cli::Command::try_from(matches) {
|
||||
Ok(command) => {
|
||||
let mut eval = evaluator::Evaluator::new(command);
|
||||
match eval.evaluate() {
|
||||
// let mut eval = evaluator::Evaluator::new();
|
||||
match evaluator::Evaluator::evaluate(command) {
|
||||
Ok(_) => {},
|
||||
Err(err) => print_error(err)
|
||||
}
|
||||
|
@ -21,11 +21,13 @@ mod cli {
|
||||
kind: Decompression(vec![
|
||||
File {
|
||||
path: "file.zip".into(),
|
||||
contents: None,
|
||||
extension: Some(Extension::from(Zip))
|
||||
}
|
||||
]),
|
||||
output: Some(File {
|
||||
path: "folder".into(),
|
||||
contents: None,
|
||||
extension: None
|
||||
}),
|
||||
}
|
||||
@ -45,10 +47,12 @@ mod cli {
|
||||
kind: Decompression(vec![
|
||||
File {
|
||||
path: "file.zip".into(),
|
||||
contents: None,
|
||||
extension: Some(Extension::from(Zip))
|
||||
},
|
||||
File {
|
||||
path: "file.tar".into(),
|
||||
contents: None,
|
||||
extension: Some(Extension::from(Tar))
|
||||
}
|
||||
],),
|
||||
@ -84,6 +88,7 @@ mod cli {
|
||||
output: Some(
|
||||
File {
|
||||
path: "file.tar".into(),
|
||||
contents: None,
|
||||
extension: Some(Extension::from(Tar))
|
||||
}
|
||||
),
|
||||
|
Loading…
x
Reference in New Issue
Block a user