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

Commit

Permalink
Add bulk write functionality. Currently only supports reading json fi…
Browse files Browse the repository at this point in the history
…les (directory support to come)
  • Loading branch information
gabbifish committed Aug 15, 2019
1 parent fbdbd66 commit 613f6b0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ fn account_id() -> Result<String, failure::Error> {

fn print_error(e: ApiFailure) {
match e {
<<<<<<< HEAD
ApiFailure::Error(_status, api_errors) => {
for error in api_errors.errors {
=======
ApiFailure::Error(_status, errors) => {
for error in errors.errors {
>>>>>>> Fix breaking changes in latest version of cloudflare-rs
message::warn(&format!("Error {}: {}", error.code, error.message,));
message::warn(&format!("Error {}: {}", error.code, error.message));

let suggestion = help(error.code);
if !suggestion.is_empty() {
Expand Down
40 changes: 40 additions & 0 deletions src/commands/kv/write_bulk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use cloudflare::apiclient::ApiClient;

use std::fs;

use cloudflare::workerskv::write_bulk::BulkWrite;
use cloudflare::workerskv::write_bulk::BulkWriteParams;
use cloudflare::workerskv::write_bulk::KeyValuePair;

use crate::terminal::message;

pub fn write_bulk(namespace_id: &str, filename: &str, expiration: Option<&str>, ttl: Option<&str>, base64: bool) -> Result<(), failure::Error> {
let client = super::api_client()?;
let account_id = super::account_id()?;

// Read key-value pairs from json file into Vec<KeyValuePair>
let data = fs::read_to_string(filename)?;
let pairs: Vec<KeyValuePair> = serde_json::from_str(&data)?;

let msg = format!("Uploading key-value pairs from \"{}\" to namespace {}", filename, namespace_id);
message::working(&msg);

let response = client.request(&BulkWrite {
account_identifier: &account_id,
namespace_identifier: namespace_id,
bulk_key_value_pairs: pairs,

params: BulkWriteParams {
expiration: expiration,
expiration_ttl: ttl,
base64: base64,
},
});

match response {
Ok(_success) => message::success(&format!("Success")),
Err(e) => super::print_error(e),
}

Ok(())
}
48 changes: 47 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ fn run() -> Result<(), failure::Error> {
.subcommand(
SubCommand::with_name("list")
)
.subcommand(
SubCommand::with_name("bulk-write")
.arg(
Arg::with_name("id")
.help("the id of your Workers KV namespace")
.index(1),
)
.arg(
Arg::with_name("filename")
.help("the json file of key-value pairs to upload, in form [{\"key\":..., \"value\":...}\"...]")
.index(2),
)
.arg(
Arg::with_name("expiration")
.short("e")
.long("expiration")
.takes_value(true)
.value_name("SECONDS")
.help("the time, measured in number of seconds since the UNIX epoch, at which the entries should expire"),
)
.arg(
Arg::with_name("time-to-live")
.short("t")
.long("ttl")
.value_name("SECONDS")
.takes_value(true)
.help("the number of seconds for which the entries should be visible before they expire. At least 60"),
)
.arg(
Arg::with_name("base64")
.long("base64")
.takes_value(false)
.help("the server should base64 decode the value before storing it. Useful for writing values that wouldn't otherwise be valid JSON strings, such as images."),
)
)
)
.subcommand(
SubCommand::with_name("generate")
Expand Down Expand Up @@ -286,9 +321,20 @@ fn run() -> Result<(), failure::Error> {
let title = rename_matches.value_of("title").unwrap();
commands::kv::rename_namespace(id, title)?;
}
("list", Some(_create_matches)) => {
("list", Some(_list_matches)) => {
commands::kv::list_namespaces()?;
}
("bulk-write", Some(bulk_write_matches)) => {
let id = bulk_write_matches.value_of("id").unwrap();
let filename = bulk_write_matches.value_of("filename").unwrap();
let expiration = bulk_write_matches.value_of("expiration");
let ttl = bulk_write_matches.value_of("time-to-live");
let base64 = match bulk_write_matches.occurrences_of("release") {
1 => true,
_ => false,
};
commands::kv::write_bulk(id, filename, expiration, ttl, base64)?;
}
("", None) => message::warn("kv expects a subcommand"),
_ => unreachable!(),
}
Expand Down

0 comments on commit 613f6b0

Please sign in to comment.