Skip to content

Commit

Permalink
chore: Updated to the new v0.2.0 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Aug 1, 2024
1 parent 159b5fc commit f016cb7
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
acridotheres_core = { git = "https://github.com/acridotheres/core.git", tag="v0.1.1" }
acridotheres_core = { git = "https://github.com/acridotheres/core.git", tag="v0.2.0" }
byte-unit = { version = "5.1.4", features = ["u128"] }
clap = { version = "4.5.7", features = ["derive"] }
4 changes: 4 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod metadata;
pub mod version;
pub mod list;
pub mod extract;
5 changes: 5 additions & 0 deletions src/commands/extract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use corelib::{archive, formats};

pub fn extract(format: String, input: String, output: String, index: Option<u32>, path: Option<String>, all: bool, check_integrity: bool, buffer_size: u64) {
archive::extract(formats::from_string(&format), input, output, index, path, all, check_integrity, buffer_size).unwrap();
}
43 changes: 43 additions & 0 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use corelib::{
archive::{self, OriginalArchiveMetadata},
formats::{self, Formats},
};

pub fn list(format_string: String, input: String, check_integrity: bool, buffer_size: u64) {
let format = formats::from_string(&format_string);
let metadata = archive::metadata(format, input, check_integrity, buffer_size).unwrap();

match format {
Formats::Zip => {
let metadata = match *metadata {
OriginalArchiveMetadata::Zip(metadata) => metadata,
};

let mut i: u32 = 0;
println!("");
for file in &metadata.files {
println!("{}", file.path);
println!("{}", "=".repeat(file.path.len()));
println!("Index: {}", i);
if file.is_directory {
println!("Directory\n");
} else {
println!(
"Size: {} ({} compressed)",
byte_unit::Byte::from_u64(file.uncompressed_size.into())
.get_appropriate_unit(byte_unit::UnitType::Decimal),
byte_unit::Byte::from_u64(file.size.into())
.get_appropriate_unit(byte_unit::UnitType::Decimal)
);
println!(
"Last modified: {}",
file.modified.format("%Y-%m-%d %H:%M:%S")
);
println!("CRC-32 checksum: 0x{:x}", file.checksum);
println!("Compression method: {}\n", file.compression);
};
i += 1;
}
}
}
}
44 changes: 44 additions & 0 deletions src/commands/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use corelib::{
archive::{self, OriginalArchiveMetadata},
formats::{self, Formats},
};

pub fn metadata(format_string: String, input: String, check_integrity: bool, buffer_size: u64) {
let format = formats::from_string(&format_string);
let metadata = archive::metadata(format, input, check_integrity, buffer_size).unwrap();

match format {
Formats::Zip => {
let metadata = match *metadata {
OriginalArchiveMetadata::Zip(metadata) => metadata,
};

println!("Type: {}", format_string);
let mut file_count: u32 = 0;
let mut dir_count: u32 = 0;
for file in &metadata.files {
if file.is_directory {
dir_count += 1;
continue;
}
file_count += 1;
}
println!("Files: {}", file_count);
println!("Directories: {}", dir_count);
let mut total_size = 0;
for file in &metadata.files {
total_size += file.uncompressed_size;
}
let total_size = byte_unit::Byte::from_u64(total_size.into())
.get_appropriate_unit(byte_unit::UnitType::Decimal);
println!("Total size (uncompressed): {}", total_size);
let mut total_size = 0;
for file in &metadata.files {
total_size += file.size;
}
let total_size = byte_unit::Byte::from_u64(total_size.into())
.get_appropriate_unit(byte_unit::UnitType::Decimal);
println!("Total size (compressed): {}", total_size);
}
}
}
5 changes: 5 additions & 0 deletions src/commands/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn version() {
println!("Acridotheres\n");
println!("cli v{}", env!("CARGO_PKG_VERSION"));
println!("core v{}", corelib::get_version());
}
59 changes: 33 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod text;
//mod text;
mod commands;

use clap::Parser;

Expand Down Expand Up @@ -44,6 +45,14 @@ struct Args {
/// "all" (combine with -x or similar)
#[arg(short, long)]
all: bool,

/// Skip integrity checks (not recommended, faster)
#[arg(long, default_value = "false")]
skip_integrity_checks: bool,

/// Buffer size for file operations in bytes
#[arg(long, default_value = "16777216")]
buffer: u64,
}

fn get_command(args: &Args) -> &'static str {
Expand All @@ -65,32 +74,30 @@ fn main() {

let command = get_command(&args);

let format = args.input_type.unwrap_or_else(|| {
// add format detection here
"".to_string()
});
let format = format.as_str();

match command {
"version" => text::get_version(),
"metadata" => match format {
"zip" => text::zip_metadata(args.input.unwrap()),
_ => println!("Unknown format"),
},
"list" => match format {
"zip" => text::zip_list(args.input.unwrap()),
_ => println!("Unknown format"),
},
"extract" => match format {
"zip" => text::zip_extract(
args.input.unwrap(),
args.output.unwrap(),
args.index,
args.path,
args.all,
),
_ => println!("Unknown format"),
},
"version" => commands::version::version(),
"metadata" => commands::metadata::metadata(
args.input_type.unwrap(),
args.input.unwrap(),
!args.skip_integrity_checks,
args.buffer,
),
"list" => commands::list::list(
args.input_type.unwrap(),
args.input.unwrap(),
!args.skip_integrity_checks,
args.buffer,
),
"extract" => commands::extract::extract(
args.input_type.unwrap(),
args.input.unwrap(),
args.output.unwrap(),
args.index,
args.path,
args.all,
!args.skip_integrity_checks,
args.buffer,
),
_ => println!("Nothing to do, try --help"),
}
}
148 changes: 0 additions & 148 deletions src/text.rs

This file was deleted.

0 comments on commit f016cb7

Please sign in to comment.