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 #526 from cloudflare/alewis/refactor-pass-project
Browse files Browse the repository at this point in the history
refactor: pass project to kv commands in prep for environments
  • Loading branch information
ashleymichal authored Sep 5, 2019
2 parents c5a4995 + 5f20b4e commit e3800e2
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 75 deletions.
16 changes: 10 additions & 6 deletions src/commands/kv/create_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use cloudflare::framework::apiclient::ApiClient;

use cloudflare::endpoints::workerskv::create_namespace::CreateNamespace;
use cloudflare::endpoints::workerskv::create_namespace::CreateNamespaceParams;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

pub fn create_namespace(title: &str) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
pub fn create_namespace(
project: &Project,
user: GlobalUser,
title: &str,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

let msg = format!("Creating namespace with title \"{}\"", title);
message::working(&msg);

let response = client.request(&CreateNamespace {
account_identifier: &account_id,
account_identifier: &project.account_id,
params: CreateNamespaceParams {
title: title.to_string(),
},
Expand Down
26 changes: 18 additions & 8 deletions src/commands/kv/delete_bulk.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
extern crate base64;

use cloudflare::framework::apiclient::ApiClient;

use std::fs;
use std::fs::metadata;
use std::path::Path;

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

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

const MAX_PAIRS: usize = 10000;

pub fn delete_json(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> {
pub fn delete_json(
project: &Project,
user: GlobalUser,
namespace_id: &str,
filename: &Path,
) -> Result<(), failure::Error> {
match kv::interactive_delete(&format!(
"Are you sure you want to delete all keys in {}?",
filename.display()
Expand All @@ -35,12 +41,16 @@ pub fn delete_json(namespace_id: &str, filename: &Path) -> Result<(), failure::E
Err(e) => failure::bail!(e),
};

delete_bulk(namespace_id, keys?)
delete_bulk(project, user, namespace_id, keys?)
}

fn delete_bulk(namespace_id: &str, keys: Vec<String>) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
fn delete_bulk(
project: &Project,
user: GlobalUser,
namespace_id: &str,
keys: Vec<String>,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

// Check number of pairs is under limit
if keys.len() > MAX_PAIRS {
Expand All @@ -52,7 +62,7 @@ fn delete_bulk(namespace_id: &str, keys: Vec<String>) -> Result<(), failure::Err
}

let response = client.request(&DeleteBulk {
account_identifier: &account_id,
account_identifier: &project.account_id,
namespace_identifier: namespace_id,
bulk_keys: keys,
});
Expand Down
14 changes: 10 additions & 4 deletions src/commands/kv/delete_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ use cloudflare::endpoints::workerskv::delete_key::DeleteKey;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

pub fn delete_key(id: &str, key: &str) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
pub fn delete_key(
project: &Project,
user: GlobalUser,
id: &str,
key: &str,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

match kv::interactive_delete(&format!("Are you sure you want to delete key \"{}\"?", key)) {
Ok(true) => (),
Expand All @@ -21,7 +27,7 @@ pub fn delete_key(id: &str, key: &str) -> Result<(), failure::Error> {
message::working(&msg);

let response = client.request(&DeleteKey {
account_identifier: &account_id,
account_identifier: &project.account_id,
namespace_identifier: id,
key: key, // this is url encoded within cloudflare-rs
});
Expand Down
16 changes: 10 additions & 6 deletions src/commands/kv/delete_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use cloudflare::framework::apiclient::ApiClient;

use cloudflare::endpoints::workerskv::remove_namespace::RemoveNamespace;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

pub fn delete_namespace(id: &str) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
pub fn delete_namespace(
project: &Project,
user: GlobalUser,
id: &str,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

match kv::interactive_delete(&format!(
"Are you sure you want to delete namespace {}?",
Expand All @@ -25,7 +29,7 @@ pub fn delete_namespace(id: &str) -> Result<(), failure::Error> {
message::working(&msg);

let response = client.request(&RemoveNamespace {
account_identifier: &account_id,
account_identifier: &project.account_id,
namespace_identifier: id,
});

Expand Down
14 changes: 10 additions & 4 deletions src/commands/kv/list_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ use cloudflare::framework::apiclient::ApiClient;
use serde_json::value::Value as JsonValue;

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;

// Note: this function only prints keys in json form, given that
// the number of entries in each json blob is variable (so csv and tsv
// representation won't make sense)
pub fn list_keys(id: &str, prefix: Option<&str>) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
pub fn list_keys(
project: &Project,
user: GlobalUser,
id: &str,
prefix: Option<&str>,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

let params = ListNamespaceKeysParams {
limit: None, // Defaults to 1000 (the maximum)
Expand All @@ -22,7 +28,7 @@ pub fn list_keys(id: &str, prefix: Option<&str>) -> Result<(), failure::Error> {
};

let mut request_params = ListNamespaceKeys {
account_identifier: &account_id,
account_identifier: &project.account_id,
namespace_identifier: id,
params: params,
};
Expand Down
9 changes: 5 additions & 4 deletions src/commands/kv/list_namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ use cloudflare::framework::apiclient::ApiClient;
use prettytable::{Cell, Row, Table};

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

pub fn list_namespaces() -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
pub fn list_namespaces(project: &Project, user: GlobalUser) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

message::working("Fetching namespaces...");

let response = client.request(&ListNamespaces {
account_identifier: &account_id,
account_identifier: &project.account_id,
});

match response {
Expand Down
15 changes: 2 additions & 13 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cloudflare::framework::HttpApiClient;
use http::status::StatusCode;
use percent_encoding::{percent_encode, PATH_SEGMENT_ENCODE_SET};

use crate::settings;
use crate::settings::global_user::GlobalUser;
use crate::terminal::message;

mod create_namespace;
Expand Down Expand Up @@ -34,21 +34,10 @@ const INTERACTIVE_RESPONSE_LEN: usize = 1;
const YES: &str = "y";
const NO: &str = "n";

fn api_client() -> Result<HttpApiClient, failure::Error> {
let user = settings::global_user::GlobalUser::new()?;

fn api_client(user: GlobalUser) -> Result<HttpApiClient, failure::Error> {
Ok(HttpApiClient::new(Credentials::from(user)))
}

fn account_id() -> Result<String, failure::Error> {
let project = settings::project::Project::new()?;
// we need to be certain that account id is present to make kv calls
if project.account_id.is_empty() {
failure::bail!("Your wrangler.toml is missing the account_id field which is required to create KV namespaces!");
}
Ok(project.account_id)
}

fn print_error(e: ApiFailure) {
match e {
ApiFailure::Error(status, api_errors) => {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/kv/read_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::settings::project::Project;

pub fn read_key(
project: &Project,
user: &GlobalUser,
user: GlobalUser,
id: &str,
key: &str,
) -> Result<(), failure::Error> {
Expand All @@ -23,7 +23,7 @@ pub fn read_key(
kv::url_encode_key(key)
);

let client = http::auth_client(user);
let client = http::auth_client(&user);

let mut res = client.get(&api_endpoint).send()?;

Expand Down
17 changes: 11 additions & 6 deletions src/commands/kv/rename_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use cloudflare::framework::apiclient::ApiClient;

use cloudflare::endpoints::workerskv::rename_namespace::RenameNamespace;
use cloudflare::endpoints::workerskv::rename_namespace::RenameNamespaceParams;
use cloudflare::framework::apiclient::ApiClient;

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

pub fn rename_namespace(id: &str, title: &str) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
pub fn rename_namespace(
project: &Project,
user: GlobalUser,
id: &str,
title: &str,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

let msg = format!("Renaming namespace {} to have title \"{}\"", id, title);
message::working(&msg);

let response = client.request(&RenameNamespace {
account_identifier: &account_id,
account_identifier: &project.account_id,
namespace_identifier: &id,
params: RenameNamespaceParams {
title: title.to_string(),
Expand Down
23 changes: 17 additions & 6 deletions src/commands/kv/write_bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ use cloudflare::endpoints::workerskv::write_bulk::KeyValuePair;
use cloudflare::endpoints::workerskv::write_bulk::WriteBulk;

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::project::Project;
use crate::terminal::message;

const MAX_PAIRS: usize = 10000;

pub fn write_json(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> {
pub fn write_json(
project: &Project,
user: GlobalUser,
namespace_id: &str,
filename: &Path,
) -> Result<(), failure::Error> {
let pairs: Result<Vec<KeyValuePair>, failure::Error> = match metadata(filename) {
Ok(ref file_type) if file_type.is_file() => {
let data = fs::read_to_string(filename)?;
Expand All @@ -24,12 +31,16 @@ pub fn write_json(namespace_id: &str, filename: &Path) -> Result<(), failure::Er
Err(e) => failure::bail!(e),
};

write_bulk(namespace_id, pairs?)
write_bulk(project, user, namespace_id, pairs?)
}

fn write_bulk(namespace_id: &str, pairs: Vec<KeyValuePair>) -> Result<(), failure::Error> {
let client = kv::api_client()?;
let account_id = kv::account_id()?;
fn write_bulk(
project: &Project,
user: GlobalUser,
namespace_id: &str,
pairs: Vec<KeyValuePair>,
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

// Validate that bulk upload is within size constraints
if pairs.len() > MAX_PAIRS {
Expand All @@ -41,7 +52,7 @@ fn write_bulk(namespace_id: &str, pairs: Vec<KeyValuePair>) -> Result<(), failur
}

let response = client.request(&WriteBulk {
account_identifier: &account_id,
account_identifier: &project.account_id,
namespace_identifier: namespace_id,
bulk_key_value_pairs: pairs,
});
Expand Down
4 changes: 2 additions & 2 deletions src/commands/kv/write_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::terminal::message;

pub fn write_key(
project: &Project,
user: &GlobalUser,
user: GlobalUser,
id: &str,
key: &str,
value: &str,
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn write_key(
body_text = value.to_string();
}

let client = http::auth_client(user);
let client = http::auth_client(&user);

let url_into_str = url?.into_string();
let mut res = client.put(&url_into_str).body(body_text).send()?;
Expand Down
Loading

0 comments on commit e3800e2

Please sign in to comment.