This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
Add bucket::upload, bucket::delete #514
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ed02e8
Add bucket::upload, bucket::delete
ashleymichal 8f79074
Apply suggestions from code review
ashleymichal a144833
use failure::bail directly
ashleymichal 4d2190a
remove use of `ref` keyword
ashleymichal 7c72229
add bucket commands to docs
ashleymichal 63415ea
separate parse_directory for upload/delete
ashleymichal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,109 @@ | ||||||
extern crate base64; | ||||||
|
||||||
use crate::commands::kv::delete_bulk::delete_bulk; | ||||||
use crate::commands::kv::write_bulk::write_bulk; | ||||||
|
||||||
use cloudflare::endpoints::workerskv::write_bulk::KeyValuePair; | ||||||
|
||||||
use walkdir::WalkDir; | ||||||
|
||||||
use std::ffi::OsString; | ||||||
use std::fs::metadata; | ||||||
use std::path::Path; | ||||||
|
||||||
use crate::terminal::message; | ||||||
|
||||||
pub fn upload(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> { | ||||||
let pairs: Result<Vec<KeyValuePair>, failure::Error> = match &metadata(filename) { | ||||||
Ok(file_type) if file_type.is_dir() => directory_keys_values(filename), | ||||||
Ok(_file_type) => { | ||||||
// any other file types (files, symlinks) | ||||||
failure::bail!("wrangler kv:bucket upload takes a directory") | ||||||
} | ||||||
Err(e) => failure::bail!("{}", e), | ||||||
}; | ||||||
|
||||||
write_bulk(namespace_id, pairs?) | ||||||
} | ||||||
|
||||||
pub fn delete(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> { | ||||||
let keys: Result<Vec<String>, failure::Error> = match &metadata(filename) { | ||||||
Ok(file_type) if file_type.is_dir() => directory_keys_only(filename), | ||||||
Ok(_) => { | ||||||
// any other file types (namely, symlinks) | ||||||
failure::bail!( | ||||||
"{} should be a file or directory, but is a symlink", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
filename.display() | ||||||
) | ||||||
} | ||||||
Err(e) => failure::bail!("{}", e), | ||||||
}; | ||||||
|
||||||
delete_bulk(namespace_id, keys?) | ||||||
} | ||||||
|
||||||
fn directory_keys_values(directory: &Path) -> Result<Vec<KeyValuePair>, failure::Error> { | ||||||
let mut upload_vec: Vec<KeyValuePair> = Vec::new(); | ||||||
for entry in WalkDir::new(directory) { | ||||||
let entry = entry.unwrap(); | ||||||
ashleymichal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
let path = entry.path(); | ||||||
if path.is_file() { | ||||||
let key = generate_key(path, directory)?; | ||||||
|
||||||
let value = std::fs::read(path)?; | ||||||
|
||||||
// Need to base64 encode value | ||||||
let b64_value = base64::encode(&value); | ||||||
message::working(&format!("Parsing {}...", key.clone())); | ||||||
upload_vec.push(KeyValuePair { | ||||||
key: key, | ||||||
value: b64_value, | ||||||
expiration: None, | ||||||
expiration_ttl: None, | ||||||
base64: Some(true), | ||||||
}); | ||||||
} | ||||||
} | ||||||
Ok(upload_vec) | ||||||
} | ||||||
|
||||||
fn directory_keys_only(directory: &Path) -> Result<Vec<String>, failure::Error> { | ||||||
let mut upload_vec: Vec<String> = Vec::new(); | ||||||
for entry in WalkDir::new(directory) { | ||||||
let entry = entry.unwrap(); | ||||||
let path = entry.path(); | ||||||
if path.is_file() { | ||||||
let key = generate_key(path, directory)?; | ||||||
|
||||||
upload_vec.push(key); | ||||||
} | ||||||
} | ||||||
Ok(upload_vec) | ||||||
} | ||||||
|
||||||
// Courtesy of Steve Kalabnik's PoC :) Used for bulk operations (write, delete) | ||||||
fn generate_key(path: &Path, directory: &Path) -> Result<String, failure::Error> { | ||||||
let path = path.strip_prefix(directory).unwrap(); | ||||||
|
||||||
// next, we have to re-build the paths: if we're on Windows, we have paths with | ||||||
// `\` as separators. But we want to use `/` as separators. Because that's how URLs | ||||||
// work. | ||||||
let mut path_with_forward_slash = OsString::new(); | ||||||
|
||||||
for (i, component) in path.components().enumerate() { | ||||||
// we don't want a leading `/`, so skip that | ||||||
if i > 0 { | ||||||
path_with_forward_slash.push("/"); | ||||||
} | ||||||
|
||||||
path_with_forward_slash.push(component); | ||||||
} | ||||||
|
||||||
// if we have a non-utf8 path here, it will fail, but that's not realistically going to happen | ||||||
let path = path_with_forward_slash.to_str().expect(&format!( | ||||||
"found a non-UTF-8 path, {:?}", | ||||||
path_with_forward_slash | ||||||
)); | ||||||
|
||||||
Ok(path.to_string()) | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📦