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

Commit

Permalink
adds preview to kv:namespace create
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Jun 10, 2020
1 parent bbfcfce commit 6327057
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 40 deletions.
114 changes: 85 additions & 29 deletions src/commands/kv/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,43 @@ use crate::commands::kv;
use crate::http;
use crate::kv::namespace::create;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;
use crate::settings::toml::{ConfigKvNamespace, KvNamespace, Manifest};
use crate::terminal::message;

pub fn run(
target: &Target,
manifest: &Manifest,
is_preview: bool,
env: Option<&str>,
user: &GlobalUser,
binding: &str,
) -> Result<(), failure::Error> {
kv::validate_target(target)?;
let target = manifest.get_target(env, is_preview)?;
kv::validate_target(&target)?;
validate_binding(binding)?;

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

let client = http::cf_v4_client(user)?;
let result = create(&client, target, &title);
let result = create(&client, &target, &title);

match result {
Ok(success) => {
let namespace = success.result;
message::success(&format!("Success: {:#?}", namespace));
if target.kv_namespaces.is_empty() {
match env {
Some(env) => message::success(&format!(
"Add the following to your wrangler.toml under [env.{}]:",
env
)),
None => message::success("Add the following to your wrangler.toml:"),
};
println!(
"kv-namespaces = [ \n\
\t {{ binding = \"{}\", id = \"{}\" }} \n\
]",
binding, namespace.id
);
} else {
match env {
Some(env) => message::success(&format!(
"Add the following to your wrangler.toml's \"kv-namespaces\" array in [env.{}]:",
env
)),
None => message::success("Add the following to your wrangler.toml's \"kv-namespaces\" array:"),
};
println!("{{ binding = \"{}\", id = \"{}\" }}", binding, namespace.id);
}
message::success("Success!");
println!(
"{}",
toml_modification_instructions(
KvNamespace {
binding: binding.to_string(),
id: namespace.id,
},
manifest.kv_namespaces.as_ref(),
env,
is_preview,
)
);
}
Err(e) => print!("{}", kv::format_error(e)),
}
Expand All @@ -68,6 +58,72 @@ fn validate_binding(binding: &str) -> Result<(), failure::Error> {
Ok(())
}

fn toml_modification_instructions(
new_namespace: KvNamespace,
all_namespaces: Option<&Vec<ConfigKvNamespace>>,
env: Option<&str>,
is_preview: bool,
) -> String {
let mut msg = "Add the following to your wrangler.toml".to_string();

if all_namespaces.is_some() {
msg.push_str(" in your kv_namespaces array");
}

if let Some(env) = env {
msg.push_str(&format!(" under [env.{}]", env));
}

msg.push_str(":\n");

let existing_namespace = if let Some(all_namespaces) = all_namespaces {
all_namespaces
.iter()
.find(|namespace| namespace.binding == new_namespace.binding)
} else {
None
};

let mut inline_msg = format!("{{ binding = \"{}\", ", new_namespace.binding);
if let Some(existing_namespace) = existing_namespace {
if is_preview {
inline_msg.push_str(&format!("preview_id: \"{}\"", new_namespace.id));
if let Some(existing_namespace_id) = &existing_namespace.id {
inline_msg.push_str(&format!(", id = \"{}\"", existing_namespace_id));
}
} else {
inline_msg.push_str(&format!("id: \"{}\"", new_namespace.id));
if let Some(existing_namespace_preview_id) = &existing_namespace.preview_id {
inline_msg.push_str(&format!(
", preview_id = \"{}\"",
existing_namespace_preview_id
));
}
}
} else {
if is_preview {
inline_msg.push_str("preview_id");

} else {
inline_msg.push_str("id")
}
inline_msg.push_str(" = \"");
inline_msg.push_str(&new_namespace.id);
inline_msg.push_str("\"");
};
inline_msg.push_str(" }}");

if all_namespaces.is_some() {
msg.push_str(&inline_msg);
} else {
msg.push_str("kv-namespaces = [ \n");
msg.push_str(&format!("\t {}", &inline_msg));
msg.push_str("]");
}

msg
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ fn run() -> Result<(), failure::Error> {
.required(true)
.index(1)
)
.arg(kv_preview_arg.clone())
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("delete")
.about("Delete namespace")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.arg(kv_preview_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(silent_verbose_arg.clone())
Expand Down Expand Up @@ -840,15 +842,15 @@ fn run() -> Result<(), failure::Error> {
} else if let Some(kv_matches) = matches.subcommand_matches("kv:namespace") {
let manifest = settings::toml::Manifest::new(config_path)?;
let user = settings::global_user::GlobalUser::new()?;

match kv_matches.subcommand() {
("create", Some(create_matches)) => {
is_preview = create_matches.is_present("preview");
let env = create_matches.value_of("env");
let target = manifest.get_target(env, is_preview)?;
let binding = create_matches.value_of("binding").unwrap();
commands::kv::namespace::create(&target, env, &user, binding)?;
commands::kv::namespace::create(&manifest, is_preview, env, &user, binding)?;
}
("delete", Some(delete_matches)) => {
is_preview = delete_matches.is_present("preview");
let env = delete_matches.value_of("env");
let target = manifest.get_target(env, is_preview)?;
let namespace_id = match delete_matches.value_of("binding") {
Expand Down
2 changes: 1 addition & 1 deletion src/settings/toml/kv_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::settings::binding::Binding;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ConfigKvNamespace {
pub id: String,
pub binding: String,
pub id: Option<String>,
pub preview_id: Option<String>,
}

Expand Down
20 changes: 13 additions & 7 deletions src/settings/toml/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl Manifest {
name: self.name.clone(), // MAY inherit
kv_namespaces: get_namespaces(self.kv_namespaces.clone(), preview)?, // MUST NOT inherit
site: self.site.clone(), // MUST NOT inherit
vars: self.vars.clone(), // MAY inherit
vars: self.vars.clone(), // MAY inherit,
};

let environment = self.get_environment(environment_name)?;
Expand Down Expand Up @@ -408,8 +408,10 @@ fn get_namespaces(
namespaces.into_iter().map(|ns| {
if preview {
if let Some(preview_id) = &ns.preview_id {
if preview_id == &ns.id {
message::warn("Specifying the same KV namespace ID for both preview and production sessions may cause bugs in your production worker! Proceed with caution.");
if let Some(id) = &ns.id {
if preview_id == id {
message::warn("Specifying the same KV namespace ID for both preview and production sessions may cause bugs in your production worker! Proceed with caution.");
}
}
Ok(KvNamespace {
id: preview_id.to_string(),
Expand All @@ -419,10 +421,14 @@ fn get_namespaces(
failure::bail!("In order to preview a worker with KV namespaces, you must designate a preview_id for each KV namespace you'd like to preview.")
}
} else {
Ok(KvNamespace {
id: ns.id.to_string(),
binding: ns.binding,
})
if let Some(id) = &ns.id {
Ok(KvNamespace {
id: id.to_string(),
binding: ns.binding,
})
} else {
failure::bail!("You must specify the namespace ID in the id field for the namespace with binding \"{}\"", &ns.binding)
}
}
}).collect()
} else {
Expand Down

0 comments on commit 6327057

Please sign in to comment.