Skip to content

Commit

Permalink
extract dir (#169)
Browse files Browse the repository at this point in the history
* path and hash

* b32

* create_dir_recursively

* dir
  • Loading branch information
sergey-shandar authored Mar 14, 2024
1 parent 7ce783a commit c84a73a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `blockset get` can create directories recursively. PR [#168](https://github.com/datablockset/blockset/pull/168)
## 0.5.0

- `blockset get` can create directories recursively. PR [#168](https://github.com/datablockset/blockset/pull/168).
- `blockset add` works with directories. PR [#165](https://github.com/datablockset/blockset/pull/165).

## 0.4.2
Expand Down
3 changes: 2 additions & 1 deletion blockset-lib/src/app/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ fn dir(path: &str) -> Option<&str> {
}

fn create_file_path_recursively<T: Io>(io: &T, path: &str) -> io::Result<()> {
dir(path).map_or(Ok(()), |d| io.create_dir_recursively(d))
dir(path).map(|d| io.create_dir_recursively(d));
Ok(())
}

pub fn create_file_recursively<T: Io>(io: &T, path: &str) -> io::Result<T::File> {
Expand Down
20 changes: 13 additions & 7 deletions blockset-lib/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ fn read_to_tree_file(
}
}

fn str_to_hash(s: &str) -> io::Result<U224> {
s.from_base32::<U224>().ok_or(invalid_input("invalid hash"))
}

fn get_hash(a: &mut impl Iterator<Item = String>) -> io::Result<U224> {
let b32 = a.next().ok_or(invalid_input("missing hash"))?;
b32.from_base32::<U224>()
.ok_or(invalid_input("invalid hash"))
str_to_hash(&b32)
}

fn validate(a: &mut impl Iterator<Item = String>, stdout: &mut impl Write) -> io::Result<()> {
Expand Down Expand Up @@ -166,11 +169,14 @@ pub fn run(io: &impl Io) -> io::Result<()> {
restore(io, &d, &mut w)?;
let json: JsObjectRef<_> = try_move(parse_json(io, GLOBAL, buffer)?)?;
for (k, v) in json.items() {
stdout.println([
js_string_to_string(k)?.as_str(),
": ",
js_string_to_string(&try_move(v.clone())?)?.as_str(),
])?;
let file = js_string_to_string(k)?;
let hash = js_string_to_string(&try_move(v.clone())?)?;
restore(
io,
&str_to_hash(&hash)?,
&mut create_file_recursively(io, (path.to_owned() + &file).as_str())?,
)?;
// stdout.println([&path, ": ", &hash])?;
}
Ok(())
} else {
Expand Down
7 changes: 5 additions & 2 deletions blockset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ The `blockset` application is a command line program that can store and retrieve
```console
blockset hash ./README.md
```
- add content of a file to the local storage `cdt0/`:
- add content of a file or a directory to the local storage `cdt0/`:
```console
blockset add ./README.md
blockset add ./LICENSE --to-posix-eol
```
- get a file by a content hash
- get a file or a directory by a content hash
```console
blockset get ngd7zembwj6f2tsh4gyxrcyx26h221e3f2wdgfbtq87nd ./old.md
```
```console
blockset get ngd7zembwj6f2tsh4gyxrcyx26h221e3f2wdgfbtq87nd ./dir/
```
- information about the repository
```console
blockset info
Expand Down

0 comments on commit c84a73a

Please sign in to comment.