Skip to content

Commit

Permalink
chore: Merge pull request #7 from acridotheres/2-support-all-core-v01…
Browse files Browse the repository at this point in the history
…0-features

Support all core v0.2.2 features
  • Loading branch information
Le0X8 authored Aug 2, 2024
2 parents 6fb858c + 337b7c4 commit 3fea1c0
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 201 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Run CI
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: full
jobs:
ci:
runs-on: ubuntu-latest

steps:
- name: Set up Rust
uses: actions/checkout@v4
- name: Install cargo-audit
run: cargo install cargo-audit
- name: Build
run: cargo build --verbose
- name: Run clippy
run: cargo clippy --verbose -- -D warnings
- name: Run audit
run: cargo audit
16 changes: 16 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"recommendations": [
"github.vscode-github-actions",
"ms-vscode.hexeditor",
"wakatime.vscode-wakatime",
"github.vscode-pull-request-github",
"fill-labs.dependi",
"tamasfe.even-better-toml",
"github.remotehub",
"dustypomerleau.rust-syntax",
"rust-lang.rust-analyzer",
"vadimcn.vscode-lldb",
"usernamehw.errorlens",
"gruntfuggly.todo-tree"
]
}
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"editor.tabSize": 4
"editor.tabSize": 4,
"errorLens.enabled": true,
"errorLens.gutterIconsEnabled": true,
"errorLens.gutterIconSet": "squareRounded",
"todo-tree.regex.regex": "(//|#|<!--|;|/\\*|^|^[ \\t]*(-|\\d+.))\\s*($TAGS)|todo!|unimplemented!",
"rust-analyzer.check.command": "clippy",
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true,
},
}
4 changes: 2 additions & 2 deletions 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.2" }
byte-unit = { version = "5.1.4", features = ["u128"] }
clap = { version = "4.5.7", features = ["derive"] }
5 changes: 5 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod create;
pub mod extract;
pub mod list;
pub mod metadata;
pub mod version;
24 changes: 24 additions & 0 deletions src/commands/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use corelib::{
archive::{self, EntrySource},
file::FsFile,
formats,
};

pub fn create(format: String, input: String, output: String, buffer_size: u64) {
let files: Vec<EntrySource> = input
.split(';')
.map(|file| {
let file = file.split(':').collect::<Vec<&str>>();
let source_path = file.first().unwrap();
let mut target_path = source_path;
if let Some(path) = file.get(1) {
target_path = path;
}
EntrySource {
path: target_path.to_string(),
source: FsFile::new(&source_path.to_string()),
}
})
.collect();
archive::create(formats::from_string(&format), output, files, buffer_size).unwrap();
}
24 changes: 24 additions & 0 deletions src/commands/extract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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();
}
39 changes: 39 additions & 0 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 OriginalArchiveMetadata::Zip(metadata) = *metadata;

println!();
for (i, file) in (0_u32..).zip(metadata.files.iter()) {
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)
.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);
};
}
}
}
}
42 changes: 42 additions & 0 deletions src/commands/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 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)
.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());
}
8 changes: 0 additions & 8 deletions src/json.rs

This file was deleted.

90 changes: 49 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod json;
mod text;
#![allow(clippy::too_many_arguments)]

mod commands;

use clap::Parser;

Expand All @@ -10,10 +11,6 @@ struct Args {
#[arg(short, long)]
version: bool,

/// Output as JSON
#[arg(short, long)]
json: bool,

/// Show archive metadata
#[arg(short, long)]
metadata: bool,
Expand All @@ -22,7 +19,7 @@ struct Args {
#[arg(short = 't', long = "type")]
input_type: Option<String>,

/// Path to input file
/// Path to input file (if creating, multiple files can be separated by semicolons)
#[arg(short, long)]
input: Option<String>,

Expand All @@ -38,6 +35,10 @@ struct Args {
#[arg(short = 'x', long = "extract")]
extract: bool,

/// Create archive
#[arg(short, long)]
create: bool,

/// by path
#[arg(short = 'p', long = "path", value_name = "PATH")]
path: Option<String>,
Expand All @@ -49,6 +50,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 @@ -60,6 +69,8 @@ fn get_command(args: &Args) -> &'static str {
"list"
} else if args.extract {
"extract"
} else if args.create {
"create"
} else {
""
}
Expand All @@ -70,39 +81,36 @@ 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();

if !args.json {
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"),
},
_ => println!("Nothing to do, try --help"),
}
} else {
match command {
"version" => json::get_version(),
_ => print!("{{}}\n"),
}
match command {
"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,
),
"create" => commands::create::create(
args.input_type.unwrap(),
args.input.unwrap(),
args.output.unwrap(),
args.buffer,
),
_ => println!("Nothing to do, try --help"),
}
}
Loading

0 comments on commit 3fea1c0

Please sign in to comment.