This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #340 first draft of wrangler kv add <namespace>
* add KV emoji
- Loading branch information
1 parent
d81bc7c
commit 7ca264c
Showing
5 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
_ => "", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!( | ||
|
@@ -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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters