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 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of single and bulk deletion from KV
- Loading branch information
Showing
5 changed files
with
156 additions
and
32 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
extern crate base64; | ||
|
||
use cloudflare::framework::apiclient::ApiClient; | ||
use walkdir::WalkDir; | ||
|
||
use std::fs; | ||
use std::fs::metadata; | ||
use std::path::Path; | ||
|
||
use cloudflare::endpoints::workerskv::delete_bulk::DeleteBulk; | ||
use failure::bail; | ||
|
||
use crate::terminal::message; | ||
|
||
pub fn delete_bulk(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> { | ||
let client = super::api_client()?; | ||
let account_id = super::account_id()?; | ||
|
||
// If the provided argument for delete_bulk is a json file, parse it | ||
// and delete its listed keys. If the argument is a directory, delete key-value | ||
// pairs where keys are the relative pathnames of files in the directory. | ||
let mut data; | ||
let keys: Result<Vec<String>, failure::Error> = match metadata(filename) { | ||
Ok(ref file_type) if file_type.is_file() => { | ||
data = fs::read_to_string(filename)?; | ||
Ok(serde_json::from_str(&data)?) | ||
} | ||
Ok(ref file_type) if file_type.is_dir() => parse_directory(filename), | ||
Ok(_file_type) => { | ||
// any other file types (namely, symlinks) | ||
bail!( | ||
"{} should be a file or directory, but is a symlink", | ||
filename.display() | ||
) | ||
} | ||
Err(e) => bail!(e), | ||
}; | ||
|
||
let response = client.request(&DeleteBulk { | ||
account_identifier: &account_id, | ||
namespace_identifier: namespace_id, | ||
bulk_keys: keys?, | ||
}); | ||
|
||
match response { | ||
Ok(_success) => message::success("Success"), | ||
Err(e) => super::print_error(e), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn parse_directory(directory: &Path) -> Result<Vec<String>, failure::Error> { | ||
let mut delete_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 = super::generate_key(path, directory)?; | ||
|
||
message::working(&format!("Deleting {}...", key.clone())); | ||
delete_vec.push(key); | ||
} | ||
} | ||
Ok(delete_vec) | ||
} |
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,25 @@ | ||
use cloudflare::endpoints::workerskv::delete_key::DeleteKey; | ||
use cloudflare::framework::apiclient::ApiClient; | ||
|
||
use crate::terminal::message; | ||
|
||
pub fn delete_key(id: &str, key: &str) -> Result<(), failure::Error> { | ||
let client = super::api_client()?; | ||
let account_id = super::account_id()?; | ||
|
||
let msg = format!("Deleting key \"{}\"", key); | ||
message::working(&msg); | ||
|
||
let response = client.request(&DeleteKey { | ||
account_identifier: &account_id, | ||
namespace_identifier: id, | ||
key: key, // this is url encoded within cloudflare-rs | ||
}); | ||
|
||
match response { | ||
Ok(_success) => message::success("Success"), | ||
Err(e) => super::print_error(e), | ||
} | ||
|
||
Ok(()) | ||
} |
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