diff --git a/Cargo.lock b/Cargo.lock index d7b0e3b..9253985 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,7 +189,7 @@ dependencies = [ [[package]] name = "ouch" -version = "0.1.1" +version = "0.1.2" dependencies = [ "bzip2 0.4.2", "clap", diff --git a/Cargo.toml b/Cargo.toml index 8fe65de..b392b8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ouch" -version = "0.1.1" +version = "0.1.2" authors = ["Vinícius Rodrigues Miguel "] edition = "2018" readme = "README.md" diff --git a/README.md b/README.md index 2c191f3..5cda38f 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,17 @@ `ouch` is the Obvious Unified Compression (and decompression) Helper. -| Supported formats | .tar | .zip | .tar.{.lz,.gz, .bz} | .zip.{.lz, .gz, .bz, .bz2} | .bz | .gz | .lz, .lzma | +| Supported formats | .tar | .zip | .tar.{.lz*,.gz, .bz} | .zip.{.lz*, .gz, .bz*} | .bz | .gz | .lz, .lzma | |-------------------|------|------|------------------------------|------------------------------|-----|-----|------------| | Decompression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | -| Compression | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | +| Compression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ## How does it work? `ouch` infers commands from the extensions of its command-line options. ``` -ouch 0.1.1 +ouch 0.1.2 Vinícius R. Miguel ouch is a unified compression & decompression utility diff --git a/src/cli.rs b/src/cli.rs index 210730f..fd0fdf6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -27,7 +27,7 @@ pub struct Command { pub fn clap_app<'a, 'b>() -> clap::App<'a, 'b> { clap::App::new("ouch") - .version("0.1.1") + .version("0.1.2") .about("ouch is a unified compression & decompression utility") .after_help( "ouch infers what to based on the extensions of the input files and output file received. diff --git a/src/compressors/bzip.rs b/src/compressors/bzip.rs index d34866d..16dd99f 100644 --- a/src/compressors/bzip.rs +++ b/src/compressors/bzip.rs @@ -3,7 +3,7 @@ use std::{fs, io::Write, path::PathBuf}; use colored::Colorize; use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; -use crate::utils::ensure_exists; +use crate::utils::{ensure_exists, check_for_multiple_files}; use super::{Compressor, Entry}; @@ -11,10 +11,7 @@ pub struct BzipCompressor {} impl BzipCompressor { fn compress_files(files: Vec, format: CompressionFormat) -> OuchResult> { - if files.len() != 1 { - eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "error".red(), format, format); - return Err(Error::InvalidInput); - } + check_for_multiple_files(&files, &format)?; let path = &files[0]; ensure_exists(path)?; let contents = { diff --git a/src/compressors/gzip.rs b/src/compressors/gzip.rs index 135d8cf..09bac81 100644 --- a/src/compressors/gzip.rs +++ b/src/compressors/gzip.rs @@ -2,8 +2,11 @@ use std::{fs, io::Write, path::PathBuf}; use colored::Colorize; -use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; -use crate::utils::ensure_exists; +use crate::{error::OuchResult, extension::CompressionFormat, file::File}; +use crate::utils::{ + ensure_exists, + check_for_multiple_files +}; use super::{Compressor, Entry}; @@ -11,10 +14,7 @@ pub struct GzipCompressor {} impl GzipCompressor { pub fn compress_files(files: Vec, format: CompressionFormat) -> OuchResult> { - if files.len() != 1 { - eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "error".red(), format, format); - return Err(Error::InvalidInput); - } + check_for_multiple_files(&files, &format)?; let path = &files[0]; ensure_exists(path)?; diff --git a/src/compressors/lzma.rs b/src/compressors/lzma.rs index 8d37224..6c70c7c 100644 --- a/src/compressors/lzma.rs +++ b/src/compressors/lzma.rs @@ -2,8 +2,11 @@ use std::{fs, io::Write, path::PathBuf}; use colored::Colorize; -use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; -use crate::utils::ensure_exists; +use crate::{error::{OuchResult}, extension::CompressionFormat, file::File}; +use crate::utils::{ + ensure_exists, + check_for_multiple_files +}; use super::{Compressor, Entry}; @@ -11,10 +14,7 @@ pub struct LzmaCompressor {} impl LzmaCompressor { pub fn compress_files(files: Vec, format: CompressionFormat) -> OuchResult> { - if files.len() != 1 { - eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "error".red(), format, format); - return Err(Error::InvalidInput); - } + check_for_multiple_files(&files, &format)?; let path = &files[0]; ensure_exists(path)?; diff --git a/src/compressors/tar.rs b/src/compressors/tar.rs index 8900c16..b2ed02f 100644 --- a/src/compressors/tar.rs +++ b/src/compressors/tar.rs @@ -12,20 +12,10 @@ pub struct TarCompressor {} impl TarCompressor { - // TODO: this function does not seem to be working correctly ;/ - fn make_archive_from_memory(input: File) -> OuchResult> { - - let _contents = match input.contents_in_memory { - Some(bytes) => bytes, - None => { - eprintln!("{}: reached TarCompressor::make_archive_from_memory without known content.", "internal error".red()); - return Err(Error::InvalidInput); - } - }; - - println!("todo"); - - Ok(vec![]) + // TODO: implement this + fn make_archive_from_memory(_input: File) -> OuchResult> { + println!("{}: .tar.tar and .zip.tar is currently unimplemented.", "error".red()); + Err(Error::InvalidZipArchive("")) } fn make_archive_from_files(input_filenames: Vec) -> OuchResult> { diff --git a/src/error.rs b/src/error.rs index c3b37bc..2d6e9d0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,10 +24,6 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::InvalidInput => write!( - f, - "When `-o/--output` is omitted, all input files should be compressed files." - ), Error::MissingExtensionError(filename) => { write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename) }, @@ -38,9 +34,9 @@ impl fmt::Display for Error { Error::FileNotFound => { write!(f, "file not found!") } - err => { + _err => { // TODO - write!(f, "todo: missing description for error {:?}", err) + write!(f, "") } } } diff --git a/src/utils.rs b/src/utils.rs index bb90769..9412e6c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,7 @@ -use std::{fs, path::Path}; +use std::{fs, path::{Path, PathBuf}}; use colored::Colorize; -use crate::{error::OuchResult, file::File}; +use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; pub (crate) fn ensure_exists<'a, P>(path: P) -> OuchResult<()> where @@ -13,6 +13,16 @@ where Ok(()) } +pub (crate) fn check_for_multiple_files(files: &Vec, format: &CompressionFormat) -> OuchResult<()> { + + if files.len() != 1 { + eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "error".red(), format, format); + return Err(Error::InvalidInput); + } + + Ok(()) +} + pub (crate) fn create_path_if_non_existent(path: &Path) -> OuchResult<()> { if !path.exists() { println!(