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 2 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
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,7 @@ 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_domain(name: &str, user: &GlobalUser, target: &Target) -> Result<(), failure::Error> {
gusvargas marked this conversation as resolved.
Show resolved Hide resolved
let msg = format!(
"Registering your subdomain, {}.workers.dev, this could take up to a minute.",
name
Expand Down Expand Up @@ -126,6 +132,41 @@ pub fn subdomain(name: &str, user: &GlobalUser, target: &Target) -> Result<(), f
Ok(())
}

pub fn subdomain(
name: Option<&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);
gusvargas marked this conversation as resolved.
Show resolved Hide resolved
return match (name, subdomain) {
(None, Ok(None)) => {
let msg = format!(
"No subdomain registered. Use `wrangler subdomain <name>` to register one."
);
message::user_error(&msg);
Ok(())
}
(None, Ok(Some(subdomain))) => {
let msg = format!("{}.workers.dev", subdomain);
message::info(&msg);
Ok(())
}
(Some(name), Ok(None)) => register_domain(&name, &user, &target),
(Some(_), Ok(Some(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(())
}
(_, Err(error)) => Err(error),
};
}

fn already_has_subdomain(errors: Vec<Error>) -> bool {
for error in errors {
if error.code == 10036 {
Expand Down
7 changes: 2 additions & 5 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,9 +497,7 @@ 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)?;
} else if let Some(kv_matches) = matches.subcommand_matches("kv:namespace") {
Expand Down