Skip to content

Commit

Permalink
Merge pull request #7 from blocklessnetwork/cli-refactor-clap
Browse files Browse the repository at this point in the history
Cli refactor clap
  • Loading branch information
Joinhack authored Jul 27, 2023
2 parents 2682672 + 8763121 commit 6bcd6c0
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 191 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ repository = "https://github.com/blocklessnetwork/car-utils"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "4.1.8"
clap = { version = "4.3.19", features = ["derive"] }
blockless-car = "0.1.5"
66 changes: 44 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,108 @@ if you wanna lean about WASM runtime, please vist "https://github.com/blocklessn
## How to intsall.

use cargo install to install the command

```
cargo install car-utils
```

car-utils install in the cargo bin directory.

## How to use.
execute the command `car-utils -help` to show the command help.

execute the command `car-utils --help` to show the command help.

```
Usage: car-utils [COMMAND]
Usage: car-utils <COMMAND>
Commands:
ar archive local file system to a car file.
ls list the car files
ex extract the car files
ar Archive local file system to a car file
cat View cid content from a car file
ls List the car files
cid List the car cid
ex Extract the car files
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-h, --help Print help
-V, --version Print version
```

### ar subcommand

archive the local directory to the car file.

```
archive local file system to a car file.
Archive local file system to a car file
Usage: car-utils ar -c <car> -s <source>
Usage: car-utils ar -c <CAR> -s <SOURCE>
Options:
-c <car> the car file for archive
-s <source> the source directory to archived
-c <CAR> the car file for archive.
-s <SOURCE> the source directory to be archived.
-h, --help Print help
```

### ls subcommand

list file structures in the car file.

```
list the car files
List the car files
Usage: car-utils ls <car>
Usage: car-utils ls <CAR>
Arguments:
<car> the car file for list.
<CAR> the car file for list.
Options:
-h, --help Print help
```

#### cid subcommand

list file cids in the car file.

```
list the car cid
List the car cid
Usage: car-utils cid <car>
Usage: car-utils cid <CAR>
Arguments:
<car> the car file for list.
<CAR> the car file for list.
Options:
-h, --help Print help
```

### ex subcommand

extract the files in the car file to the target directory.

```
Usage: car-utils ex [OPTIONS] -c <car>
Extract the car files
Usage: car-utils ex [OPTIONS] -c <CAR>
Options:
-c <car> the car file for extract
-t <target> the target directory to extract
-c <CAR> The car file to extract
-t <TARGET> Target directory to extract to
-h, --help Print help
```

#### cat subcommand

cat cid content from a car file.

```
Usage: car-utils cat -c <cid> <car>
View cid content from a car file
Usage: car-utils cat -c <CID> <CAR>
Arguments:
<car> the car file for cat.
<CAR> the car file to cat.
Options:
-c <cid> the cid of content for cat.
-c <CID> the cid of content to cat.
-h, --help Print help
```
30 changes: 18 additions & 12 deletions src/archive.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use crate::error::UtilError;
use blockless_car::utils::archive_local;
use std::path::Path;

use crate::error::UtilError;
#[derive(Debug, clap::Parser)]
pub struct ArchiveCommand {
#[clap(short, help = "the car file for archive.")]
car: String,

#[clap(short, help = "the source directory to be archived.")]
source: String,
}

/// archive the local file system to car file
/// `target` is the car file
/// `source` is the directory where the archive is prepared.
pub(crate) fn archive_local_fs(
target: impl AsRef<Path>,
source: impl AsRef<Path>,
) -> Result<(), UtilError> {
let target = target.as_ref();
let file = std::fs::File::create(target).unwrap();
archive_local(source, file)?;
Ok(())
impl ArchiveCommand {
/// archive the local file system to car file
/// `target` is the car file
/// `source` is the directory where the archive is prepared.
pub(crate) fn execute(&self) -> Result<(), UtilError> {
let file = std::fs::File::create(self.car.as_ref() as &Path).unwrap(); // todo handle error
archive_local(self.source.as_ref() as &Path, file)?;
Ok(())
}
}
38 changes: 24 additions & 14 deletions src/cat.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
use std::{path::Path, fs::File};

use crate::error::UtilError;
use blockless_car::reader as car_reader;
use std::{fs::File, path::Path};

#[derive(Debug, clap::Parser)]
pub struct CatCommand {
#[clap(help = "the car file to cat.")]
car: String,

#[clap(short, help = "the cid of content to cat.")]
cid: String,
}

pub(crate) fn cat_content(path: impl AsRef<Path>, cid: &str) -> Result<(), UtilError> {
let path = path.as_ref();
if !path.exists() {
return Err(UtilError::new(format!(
"car file [{}] is not exist.",
path.to_str().unwrap()
)));
impl CatCommand {
pub(crate) fn execute(&self) -> Result<(), UtilError> {
let path: &Path = self.car.as_ref();
if !path.exists() {
return Err(UtilError::new(format!(
"the car file [{}] does not exist.",
self.car
)));
}
let file = File::open(path)?;
let mut reader = car_reader::new_v1(file)?;
blockless_car::utils::cat_ipld_str(&mut reader, &self.cid)?;
Ok(())
}
let file = File::open(path)?;
let mut reader = car_reader::new_v1(file)?;
blockless_car::utils::cat_ipld_str(&mut reader, cid)?;
Ok(())
}
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{fmt::Display, process::ExitCode};

use blockless_car::error::CarError;

#[derive(Debug)]
pub(crate) struct UtilError {
pub(crate) err: String,
pub(crate) code: u8,
Expand Down
46 changes: 29 additions & 17 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ use crate::error::UtilError;
use blockless_car::reader::{self as car_reader, CarReader};
use blockless_car::utils::extract_ipld;

/// extract car file to local file system.
/// `car` the car file for extract.
/// `target` target directory to extract.
pub(crate) fn extract_car(car: impl AsRef<Path>, target: Option<&String>) -> Result<(), UtilError> {
let path = car.as_ref();
if !path.exists() {
return Err(UtilError::new(format!(
"car file [{}] is not exist.",
path.to_str().unwrap()
)));
}
let file = File::open(path)?;
let mut reader = car_reader::new_v1(file)?;
let roots = reader.header().roots();
for cid in roots {
extract_ipld(&mut reader, cid, target)?;
#[derive(Debug, clap::Parser)]
pub struct ExCommand {
#[clap(short, help = "The car file to extract")]
car: String,

#[clap(short, help = "Target directory to extract to")]
target: Option<String>,
}

impl ExCommand {
/// extract car file to local file system.
/// `car` the car file to extract.
/// `target` target directory to extract.
pub(crate) fn execute(&self) -> Result<(), UtilError> {
let path: &Path = self.car.as_ref();
if !path.exists() {
return Err(UtilError::new(format!(
"car file [{}] is not exist.",
path.to_str().unwrap()
)));
}
let file = File::open(path)?;
let mut reader = car_reader::new_v1(file)?;
let roots = reader.header().roots();
for cid in roots {
let target: Option<&Path> = self.target.as_ref().map(|s| s.as_ref());
extract_ipld(&mut reader, cid, target)?;
}
Ok(())
}
Ok(())
}
43 changes: 26 additions & 17 deletions src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ use std::path::Path;

use crate::error::UtilError;

/// list files from car file.
/// `path` is the car file path.
pub(crate) fn list_car_file(path: impl AsRef<Path>, is_cid: bool) -> Result<(), UtilError> {
let path = path.as_ref();
if !path.exists() {
return Err(UtilError::new(format!(
"car file [{}] is not exist.",
path.to_str().unwrap()
)));
}
let file = File::open(path)?;
let mut reader = car_reader::new_v1(file)?;
if is_cid {
utils::list_cid(&mut reader)?;
} else {
utils::list(&mut reader)?;
#[derive(Debug, clap::Parser)]
pub struct LsCommand {
#[clap(help = "the car file for list.")]
car: String,
}

impl LsCommand {
/// list files from car file.
/// `path` is the car file path.
pub(crate) fn execute(&self, is_cid: bool) -> Result<(), UtilError> {
// Ok(list_car_file(&self.car, is_cid)?)
let path: &Path = self.car.as_ref();
if !path.exists() {
return Err(UtilError::new(format!(
"car file [{}] is not exist.",
path.to_str().unwrap()
)));
}
let file = File::open(path)?;
let mut reader = car_reader::new_v1(file)?;
if is_cid {
utils::list_cid(&mut reader)?;
} else {
utils::list(&mut reader)?;
}
Ok(())
}
Ok(())
}
Loading

0 comments on commit 6bcd6c0

Please sign in to comment.