Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the -n, --no flag usage and add an alias for the compress subcommand #22

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
});
}

let subcommands = &["compress"];
let subcommands = &[
"c", "compress"
];

let mut flags_info = vec![flag!('y', "yes"), flag!('n', "no")];

let parsed_args = match oof::pop_subcommand(&mut args, subcommands) {
Some(&"compress") => {
Some(&"c") | Some(&"compress") => {
// `ouch compress` subcommand
let (args, flags) = oof::filter_flags(args, &flags_info)?;
let mut files: Vec<PathBuf> = args.into_iter().map(PathBuf::from).collect();
Expand Down Expand Up @@ -133,13 +135,10 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
// Parse flags
let (args, mut flags) = oof::filter_flags(args, &flags_info)?;

let files = args.into_iter().map(canonicalize);
for file in files.clone() {
if let Err(err) = file {
return Err(err);
}
}
let files = files.map(Result::unwrap).collect();
let files = args
.into_iter()
.map(canonicalize)
.collect::<Result<Vec<_>, _>>()?;

let output_folder = flags.take_arg("output").map(PathBuf::from);

Expand Down
8 changes: 4 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type BoxedDecompressor = Box<dyn Decompressor>;

fn get_compressor(file: &File) -> crate::Result<(Option<BoxedCompressor>, BoxedCompressor)> {
let extension = match &file.extension {
Some(extension) => extension.clone(),
Some(extension) => extension,
None => {
// This is reached when the output file given does not have an extension or has an unsupported one
return Err(crate::Error::MissingExtensionError(file.path.to_path_buf()));
Expand All @@ -58,7 +58,7 @@ fn get_compressor(file: &File) -> crate::Result<(Option<BoxedCompressor>, BoxedC

// Supported first compressors:
// .tar and .zip
let first_compressor: Option<Box<dyn Compressor>> = match extension.first_ext {
let first_compressor: Option<Box<dyn Compressor>> = match &extension.first_ext {
Some(ext) => match ext {
CompressionFormat::Tar => Some(Box::new(TarCompressor)),
CompressionFormat::Zip => Some(Box::new(ZipCompressor)),
Expand All @@ -84,7 +84,7 @@ fn get_compressor(file: &File) -> crate::Result<(Option<BoxedCompressor>, BoxedC

fn get_decompressor(file: &File) -> crate::Result<(Option<BoxedDecompressor>, BoxedDecompressor)> {
let extension = match &file.extension {
Some(extension) => extension.clone(),
Some(extension) => extension,
None => {
// This block *should* be unreachable
eprintln!(
Expand All @@ -104,7 +104,7 @@ fn get_decompressor(file: &File) -> crate::Result<(Option<BoxedDecompressor>, Bo
CompressionFormat::Bzip => Box::new(BzipDecompressor),
};

let first_decompressor: Option<Box<dyn Decompressor>> = match extension.first_ext {
let first_decompressor: Option<Box<dyn Decompressor>> = match &extension.first_ext {
Some(ext) => match ext {
CompressionFormat::Tar => Some(Box::new(TarDecompressor)),
CompressionFormat::Zip => Some(Box::new(ZipDecompressor)),
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn permission_for_overwriting(
flags: &oof::Flags,
confirm: &Confirmation,
) -> crate::Result<bool> {
match (flags.is_present("yes"), flags.is_present("false")) {
match (flags.is_present("yes"), flags.is_present("no")) {
(true, true) => {
unreachable!("This should've been cutted out in the ~/src/cli.rs filter flags function.")
}
Expand Down