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

Commit

Permalink
feat: #340 first draft of wrangler kv add <namespace>
Browse files Browse the repository at this point in the history
* add KV emoji
  • Loading branch information
ashleymichal committed Aug 5, 2019
1 parent d81bc7c commit 7ca264c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/commands/kv/add_namespace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use cloudflare::apiclient::APIClient;

use cloudflare::workerskv::create_namespace::CreateNamespace;
use cloudflare::workerskv::create_namespace::CreateNamespaceParams;

use crate::terminal::message;

pub fn add_namespace(title: &str) -> Result<(), failure::Error> {
let client = super::api_client()?;
let account_id = super::account_id()?;

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

let response = client.request(&CreateNamespace {
account_identifier: &account_id,
params: CreateNamespaceParams {
title: title.to_string(),
},
});

super::print_response(response);

Ok(())
}
68 changes: 68 additions & 0 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use cloudflare::auth::Credentials;
use cloudflare::response::APIFailure;
use cloudflare::response::APIResponse;
use cloudflare::response::APIResult;
use cloudflare::HTTPAPIClient;

use crate::settings;
use crate::terminal::message;

mod add_namespace;

pub use add_namespace::add_namespace;

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

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() {
panic!("Your wrangler.toml is missing the account_id field which is required to create KV namespaces!");
}
Ok(project.account_id)
}

fn print_response<T: APIResult>(response: APIResponse<T>) {
match response {
Ok(success) => message::success(&format!("Success: {:#?}", success.result)),
Err(e) => match e {
APIFailure::Error(_status, errors) => {
for error in errors {
message::warn(&format!("Error {}: {}", error.code, error.message,));

let suggestion = help(error.code);
if !suggestion.is_empty() {
message::help(suggestion);
}
}
}
APIFailure::Invalid(reqwest_err) => message::warn(&format!("Error: {}", reqwest_err)),
},
}
}

fn help(error_code: u16) -> &'static str {
// https://api.cloudflare.com/#workers-kv-namespace-errors
match error_code {
// namespace errors
10010 | 10011 | 10012 | 10013 | 10014 | 10018 => {
"Run `wrangler kv list` to see your existing namespaces with IDs"
}
10009 | 10020 => "Run `wrangler kv list <namespaceID>` to see your existing keys", // key errors
// TODO: link to more info
// limit errors
10022 | 10023 | 10024 | 10030 => "See documentation",
// TODO: link to tool for this
// legacy namespace errors
10021 | 10035 | 10038 => "Consider moving this namespace",
// cloudflare account errors
10017 | 10026 | 10027 | 10031 | 10032 | 10037 => {
"Check your account settings in the Cloudflare dashboard"
}
_ => "",
}
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod build;
pub mod config;
pub mod generate;
pub mod init;
pub mod kv;
pub mod publish;
pub mod subdomain;
pub mod whoami;
Expand Down
22 changes: 22 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ fn run() -> Result<(), failure::Error> {
.author("ashley g williams <[email protected]>")
.setting(AppSettings::ArgRequiredElseHelp)
.setting(AppSettings::DeriveDisplayOrder)
.subcommand(
SubCommand::with_name("kv")
.about(&*format!(
"{} Interact with your Workers KV Store",
emoji::KV
))
.subcommand(
SubCommand::with_name("add")
.arg(
Arg::with_name("title")
)
)
)
.subcommand(
SubCommand::with_name("generate")
.about(&*format!(
Expand Down Expand Up @@ -233,6 +246,15 @@ fn run() -> Result<(), failure::Error> {
.expect("The subdomain name you are requesting must be provided.");

commands::subdomain(name, &user, &project)?;
} else if let Some(kv_matches) = matches.subcommand_matches("kv") {
match kv_matches.subcommand() {
("add", Some(add_matches)) => {
let title = add_matches.value_of("title").unwrap();
commands::kv::add_namespace(title)?;
}
("", None) => println!("kv expects a subcommand"),
_ => unreachable!(),
}
}
Ok(())
}
5 changes: 3 additions & 2 deletions src/terminal/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ pub static DANCERS: Emoji = Emoji("πŸ‘― ", "");
pub static EYES: Emoji = Emoji("πŸ‘€ ", "");
pub static INBOX: Emoji = Emoji("πŸ“₯ ", "");
pub static INFO: Emoji = Emoji("πŸ’β€ ", "");
pub static KV: Emoji = Emoji("πŸ—‚οΈ ", "");
pub static MICROSCOPE: Emoji = Emoji("πŸ”¬ ", "");
pub static SHEEP: Emoji = Emoji("πŸ‘ ", "");
pub static SLEUTH: Emoji = Emoji("πŸ•΅οΈβ€β™‚οΈ", "");
pub static SPARKLES: Emoji = Emoji("✨ ", "");
pub static SLEUTH: Emoji = Emoji("πŸ•΅οΈβ€β™‚οΈ ", "");
pub static SPARKLES: Emoji = Emoji("✨ ", "");
pub static SWIRL: Emoji = Emoji("πŸŒ€ ", "");
pub static UP: Emoji = Emoji("πŸ†™ ", "");
pub static WARN: Emoji = Emoji("⚠️ ", "");
Expand Down

0 comments on commit 7ca264c

Please sign in to comment.