Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #577 from cloudflare/alewis/bulk-delete
Browse files Browse the repository at this point in the history
Allow use of same json file to bulk delete as bulk put
  • Loading branch information
ashleymichal authored Sep 12, 2019
2 parents e71e699 + 399cf3c commit ec5ff3f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
17 changes: 10 additions & 7 deletions docs/content/kv_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $ wrangler kv:namespace create "MY_KV"
}
✨ Add the following to your wrangler.toml:
kv-namespaces = [
{ binding: "MY_KV", id: "e29b263ab50e42ce9b637fa8370175e8" }
{ binding = "MY_KV", id = "e29b263ab50e42ce9b637fa8370175e8" }
]
```
Make sure to add the `kv-namespaces` output above to your `wrangler.toml`. You can now
Expand Down Expand Up @@ -53,12 +53,12 @@ one for staging and one for production). So, if you have a `wrangler.toml` with
```toml
[env.staging]
kv-namespaces = [
{ binding: "MY_KV", id: "e29b263ab50e42ce9b637fa8370175e8" }
{ binding = "MY_KV", id = "e29b263ab50e42ce9b637fa8370175e8" }
]

[env.production]
kv-namespaces = [
{ binding: "MY_KV", id: "a825455ce00f4f7282403da85269f8ea" }
{ binding = "MY_KV", id = "a825455ce00f4f7282403da85269f8ea" }
]
```

Expand Down Expand Up @@ -134,7 +134,7 @@ $ wrangler kv:namespace create "MY_KV"
}
✨ Add the following to your wrangler.toml:
kv-namespaces = [
{ binding: "MY_KV", id: "e29b263ab50e42ce9b637fa8370175e8" }
{ binding = "MY_KV", id = "e29b263ab50e42ce9b637fa8370175e8" }
]
```
Expand Down Expand Up @@ -309,12 +309,15 @@ $ wrangler kv:bulk put --binding=KV allthethingsupload.json
Requires `--binding` or `--namespace-id` argument.
Deletes all specified keys within a given namespace.
Takes as an argument a JSON file with a list of keys to delete; for example:
Takes as an argument a JSON file with a list of key-value pairs to delete (see JSON spec above). An example of JSON input:
```json
[
"key1",
"key2"
{
"key": "test_key",
"value": "test_value",
"expiration_ttl": 3600
}
]
```
Expand Down
9 changes: 6 additions & 3 deletions src/commands/kv/bulk/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fs::metadata;
use std::path::Path;

use cloudflare::endpoints::workerskv::delete_bulk::DeleteBulk;
use cloudflare::endpoints::workerskv::write_bulk::KeyValuePair;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
Expand All @@ -31,12 +32,12 @@ pub fn delete(
Err(e) => failure::bail!(e),
}

let keys: Result<Vec<String>, failure::Error> = match &metadata(filename) {
let pairs: Result<Vec<KeyValuePair>, failure::Error> = match &metadata(filename) {
Ok(file_type) if file_type.is_file() => {
let data = fs::read_to_string(filename)?;
let keys_vec = serde_json::from_str(&data);
if keys_vec.is_err() {
failure::bail!("Failed to decode JSON. Please make sure to follow the format, [\"test_key_1\", \"test_key_2\", ...]")
failure::bail!("Failed to decode JSON. Please make sure to follow the format, [{\"key\": \"test_key\", \"value\": \"test_value\"}, ...]")
} else {
Ok(keys_vec.unwrap())
}
Expand All @@ -45,7 +46,9 @@ pub fn delete(
Err(e) => failure::bail!("{}", e),
};

delete_bulk(target, user, namespace_id, keys?)
let keys: Vec<String> = pairs?.iter().map(|kv| kv.key.to_owned()).collect();

delete_bulk(target, user, namespace_id, keys)
}

fn delete_bulk(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/kv/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn create(
};
println!(
"kv-namespaces = [ \n\
\t {{ binding: \"{}\", id: \"{}\" }} \n\
\t {{ binding = \"{}\", id = \"{}\" }} \n\
]",
binding, success.result.id
);
Expand Down

0 comments on commit ec5ff3f

Please sign in to comment.