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

Print subdomain if name arg not provided to wrangler subdomain #747

Merged
merged 6 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub use publish::preview::preview;
pub use publish::preview::HTTPMethod;
pub use publish::publish;
use regex::Regex;
pub use subdomain::subdomain;
pub use subdomain::get_subdomain;
pub use subdomain::set_subdomain;
pub use whoami::whoami;

/// Run the given command and return its stdout.
Expand Down
59 changes: 50 additions & 9 deletions src/commands/subdomain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Subdomain {
}

impl Subdomain {
pub fn get(account_id: &str, user: &GlobalUser) -> Result<String, failure::Error> {
fn request(account_id: &str, user: &GlobalUser) -> Result<Response, failure::Error> {
let addr = subdomain_addr(account_id);

let client = http::auth_client(None, user);
Expand All @@ -27,12 +27,24 @@ impl Subdomain {
)
}

let res: Response = serde_json::from_str(&res.text()?)?;
Ok(serde_json::from_str(&res.text()?)?)
}

pub fn get(account_id: &str, user: &GlobalUser) -> Result<String, failure::Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename get_as_option to get and just get rid of the one that doesn't return an option. Would require a bit more refactoring though across the codebase to handle , doesn't necessarily need to be in this PR. My thinking is that then each other function that requires a subdomain to exist can provide their own error messages when it doesn't exist. Not sure how I feel 100% about that though - probably best to just defer that to another refactor.

let res = Subdomain::request(account_id, user)?;
Ok(res
.result
.expect("Oops! We expected a subdomain name, but found none.")
.subdomain)
}

pub fn get_as_option(
account_id: &str,
user: &GlobalUser,
) -> Result<Option<String>, failure::Error> {
let res = Subdomain::request(account_id, user)?;
Ok(res.result.map(|r| r.subdomain))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

}
}

#[derive(Deserialize)]
Expand All @@ -58,13 +70,11 @@ fn subdomain_addr(account_id: &str) -> String {
)
}

pub fn subdomain(name: &str, user: &GlobalUser, target: &Target) -> Result<(), failure::Error> {
if target.account_id.is_empty() {
failure::bail!(format!(
"{} You must provide an account_id in your wrangler.toml before creating a subdomain!",
emoji::WARN
))
}
fn register_subdomain(
name: &str,
user: &GlobalUser,
target: &Target,
) -> Result<(), failure::Error> {
let msg = format!(
"Registering your subdomain, {}.workers.dev, this could take up to a minute.",
name
Expand Down Expand Up @@ -126,6 +136,37 @@ pub fn subdomain(name: &str, user: &GlobalUser, target: &Target) -> Result<(), f
Ok(())
}

pub fn set_subdomain(name: &str, user: &GlobalUser, target: &Target) -> Result<(), failure::Error> {
if target.account_id.is_empty() {
failure::bail!(format!(
"{} You must provide an account_id in your wrangler.toml before creating a subdomain!",
emoji::WARN
))
}
let subdomain = Subdomain::get_as_option(&target.account_id, user)?;
if let Some(subdomain) = subdomain {
let msg = format!("This account already has a registered subdomain. You can only register one subdomain per account. Your subdomain is {}.workers.dev", subdomain);
message::user_error(&msg);
Ok(())
} else {
register_subdomain(&name, &user, &target)
}
}

pub fn get_subdomain(user: &GlobalUser, target: &Target) -> Result<(), failure::Error> {
let subdomain = Subdomain::get_as_option(&target.account_id, user)?;
if let Some(subdomain) = subdomain {
let msg = format!("{}.workers.dev", subdomain);
message::info(&msg);
Ok(())
} else {
let msg =
format!("No subdomain registered. Use `wrangler subdomain <name>` to register one.");
message::user_error(&msg);
Ok(())
}
}

fn already_has_subdomain(errors: Vec<Error>) -> bool {
for error in errors {
if error.code == 10036 {
Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ fn run() -> Result<(), failure::Error> {
.arg(
Arg::with_name("name")
.help("the subdomain on workers.dev you'd like to reserve")
.index(1)
.required(true),
.index(1),
),
)
.subcommand(SubCommand::with_name("whoami").about(&*format!(
Expand Down Expand Up @@ -498,11 +497,13 @@ fn run() -> Result<(), failure::Error> {
info!("Getting User settings");
let user = settings::global_user::GlobalUser::new()?;

let name = matches
.value_of("name")
.expect("The subdomain name you are requesting must be provided.");
let name = matches.value_of("name");

commands::subdomain(name, &user, &target)?;
if let Some(name) = name {
commands::subdomain::set_subdomain(&name, &user, &target)?;
} else {
commands::subdomain::get_subdomain(&user, &target)?;
}
} else if let Some(kv_matches) = matches.subcommand_matches("kv:namespace") {
let manifest = settings::target::Manifest::new(config_path)?;
let user = settings::global_user::GlobalUser::new()?;
Expand Down