Skip to content

Commit

Permalink
Merge pull request #18 from tamada/release/v0.2.1
Browse files Browse the repository at this point in the history
Release/v0.2.1
  • Loading branch information
tamada authored May 3, 2024
2 parents 2f4e2e5 + 668e1a8 commit 93ace08
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "totebag"
version = "0.2.0"
version = "0.2.1"
description = "A tool for archiving files and directories and extracting several archive formats."
repository = "https://github.com/tamada/totebag"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# totebag

[![Version](https://shields.io/badge/Version-0.2.0-blue)](https://github.com/tamada/totebag/releases/tag/v0.2.0)
[![Version](https://shields.io/badge/Version-0.2.1-blue)](https://github.com/tamada/totebag/releases/tag/v0.2.1)
[![MIT License](https://shields.io/badge/License-MIT-blue)](https://github.com/tamada/totebag/blob/main/LICENSE)

[![build](https://github.com/tamada/totebag/actions/workflows/build.yaml/badge.svg)](https://github.com/tamada/totebag/actions/workflows/build.yaml)
Expand Down
9 changes: 7 additions & 2 deletions src/archiver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::File;
use std::fs::{create_dir_all, File};
use std::path::PathBuf;

use crate::cli::{ToatError, Result};
Expand Down Expand Up @@ -58,7 +58,7 @@ pub struct ArchiverOpts {
impl ArchiverOpts {
pub fn new(opts: &CliOpts) -> Self {
let args = opts.args.clone();
let dest = opts.dest.clone().unwrap_or_else(|| {
let dest = opts.output.clone().unwrap_or_else(|| {
PathBuf::from(".")
});
ArchiverOpts {
Expand All @@ -83,6 +83,11 @@ impl ArchiverOpts {
if p.is_file() && p.exists() && !self.overwrite {
return Err(ToatError::FileExists(self.dest.clone()))
}
if let Some(parent) = p.parent() {
if !parent.exists() {
let _ = create_dir_all(parent);
}
}
match File::create(self.dest.as_path()) {
Err(e) => Err(ToatError::IOError(e)),
Ok(f) => Ok(f),
Expand Down
12 changes: 8 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ pub type Result<T> = std::result::Result<T, ToatError>;
pub struct CliOpts {
#[clap(short = 'm', long = "mode", default_value_t = RunMode::Auto, value_name = "MODE", required = false, ignore_case = true, value_enum, help = "Mode of operation.")]
pub mode: RunMode,
#[clap(short = 'd', long = "dest", default_value = ".", value_name = "DEST", required = false, help = "Destination of the extraction results (extract mode).")]
pub dest: Option<PathBuf>,
#[clap(short = 'o', long = "output", default_value = "totebag.zip", value_name = "OUTPUT", required = false, help = "Output file (archive mode).")]
#[clap(short = 'o', short_alias = 'd', long = "output", alias = "dest", value_name = "DEST", required = false, help = "Output file in archive mode, or output directory in extraction mode")]
pub output: Option<PathBuf>,
#[clap(long = "to-archive-name-dir", help = "extract files to DEST/ARCHIVE_NAME directory (extract mode).", default_value_t = false)]
pub to_archive_name_dir: bool,
Expand Down Expand Up @@ -49,7 +47,13 @@ impl CliOpts {
fn is_all_args_archives(args: &[PathBuf]) -> bool {
args.iter().all(|arg| {
let name = arg.to_str().unwrap().to_lowercase();
name.ends_with(".zip") || name.ends_with(".tar") || name.ends_with(".tar.gz") || name.ends_with(".tgz") || name.ends_with(".tar.bz2") || name.ends_with(".tbz2") || name.ends_with(".rar")
let exts = vec![".zip", ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz2", ".rar", ".jar", ".war", ".ear", ];
for ext in exts.iter() {
if name.ends_with(ext) {
return true
}
}
return false
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait Extractor {
}

pub fn create_extract_opts(opts: &CliOpts) -> ExtractorOpts {
let d = opts.dest.clone();
let d = opts.output.clone();
ExtractorOpts {
dest: d.unwrap_or_else(|| {
PathBuf::from(".")
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,17 @@ fn main() -> Result<()> {
match perform(CliOpts::parse()) {
Ok(_) => Ok(()),
Err(e) => {
eprintln!("Error: {:?}", e);
Err(e)
match e {
ToatError::NoArgumentsGiven => println!("No arguments given. Use --help for usage."),
ToatError::FileNotFound(p) => println!("{}: file not found", p.to_str().unwrap()),
ToatError::FileExists(p) => println!("{}: file already exists", p.to_str().unwrap()),
ToatError::IOError(e) => println!("IO error: {}", e),
ToatError::ArchiverError(s) => println!("Archive error: {}", s),
ToatError::UnsupportedFormat(f) => println!("{}: unsupported format", f),
ToatError::UnknownError(s) => println!("Unknown error: {}", s),
}
std::process::exit(1);
}

}
}

Expand Down

0 comments on commit 93ace08

Please sign in to comment.