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

Commit

Permalink
Ensure KV subcommands check for presence of required fields in wrangl…
Browse files Browse the repository at this point in the history
…er.toml (#665)

* Ensure KV subcommands check for presence of required fields in wrangler.toml
  • Loading branch information
gabbifish authored Sep 23, 2019
1 parent c47dc3a commit 81d11a8
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/commands/kv/bucket/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub fn sync(
path: &Path,
verbose: bool,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;

// First, upload all existing files in given directory
if verbose {
message::info("Preparing to upload updated files...");
Expand Down
2 changes: 2 additions & 0 deletions src/commands/kv/bulk/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub fn delete(
namespace_id: &str,
filename: &Path,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;

match kv::interactive_delete(&format!(
"Are you sure you want to delete all keys in {}?",
filename.display()
Expand Down
2 changes: 2 additions & 0 deletions src/commands/kv/bulk/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub fn put(
namespace_id: &str,
filename: &Path,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;

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)?;
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/key/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn delete(
id: &str,
key: &str,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let client = kv::api_client(user)?;

match kv::interactive_delete(&format!("Are you sure you want to delete key \"{}\"?", key)) {
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/key/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::settings::global_user::GlobalUser;
use crate::settings::target::Target;

pub fn get(target: &Target, user: GlobalUser, id: &str, key: &str) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let api_endpoint = format!(
"https://api.cloudflare.com/client/v4/accounts/{}/storage/kv/namespaces/{}/values/{}",
target.account_id,
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/key/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn list(
namespace_id: &str,
prefix: Option<&str>,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let key_list = KeyList::new(target, user, namespace_id, prefix)?;

print!("["); // Open json list bracket
Expand Down
2 changes: 2 additions & 0 deletions src/commands/kv/key/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub fn put(
expiration: Option<&str>,
expiration_ttl: Option<&str>,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;

let api_endpoint = format!(
"https://api.cloudflare.com/client/v4/accounts/{}/storage/kv/namespaces/{}/values/{}",
target.account_id,
Expand Down
20 changes: 20 additions & 0 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ fn give_status_code_context(status_code: StatusCode) {
fn help(error_code: u16) -> &'static str {
// https://api.cloudflare.com/#workers-kv-namespace-errors
match error_code {
7003 | 7000 => {
"Your wrangler.toml is likely missing the field \"account_id\", which is required to write to Workers KV."
}
// namespace errors
10010 | 10011 | 10012 | 10013 | 10014 | 10018 => {
"Run `wrangler kv:namespace list` to see your existing namespaces with IDs"
Expand All @@ -138,6 +141,23 @@ fn help(error_code: u16) -> &'static str {
}
}

pub fn validate_target(target: &Target) -> Result<(), failure::Error> {
let mut missing_fields = Vec::new();

if target.account_id.is_empty() {
missing_fields.push("account_id")
};

if !missing_fields.is_empty() {
failure::bail!(
"Your wrangler.toml is missing the following field(s): {:?}",
missing_fields
)
} else {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::commands::kv;
Expand Down
2 changes: 2 additions & 0 deletions src/commands/kv/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub fn create(
user: GlobalUser,
binding: &str,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;

let client = kv::api_client(user)?;

if !validate_binding(binding) {
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/namespace/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::settings::target::Target;
use crate::terminal::message;

pub fn delete(target: &Target, user: GlobalUser, id: &str) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let client = kv::api_client(user)?;

match kv::interactive_delete(&format!(
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/namespace/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::settings::global_user::GlobalUser;
use crate::settings::target::Target;

pub fn list(target: &Target, user: GlobalUser) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let client = kv::api_client(user)?;

let response = client.request(&ListNamespaces {
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/namespace/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::settings::target::Target;
use crate::terminal::message;

pub fn site(target: &Target, user: &GlobalUser) -> Result<WorkersKvNamespace, failure::Error> {
kv::validate_target(target)?;
let client = kv::api_client(user.to_owned())?;

let title = format!("__{}-{}", target.name, "workers_sites_assets");
Expand Down

0 comments on commit 81d11a8

Please sign in to comment.