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
Print subdomain if name
arg not provided to wrangler subdomain
#747
Merged
EverlastingBugstopper
merged 6 commits into
cloudflare:master
from
gusvargas:optional-subdomain-arg
Oct 4, 2019
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
34f8080
Print subdomain if `name` arg not provided
fb74a8a
Merge branch 'master' into optional-subdomain-arg
gusvargas b78c68c
Merge branch 'master' into optional-subdomain-arg
EverlastingBugstopper 50c873c
Smaller get/set subdomoain functions for clarity
c24969c
match is the last expr in the fn
1dd74e1
Simpler matching
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -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); | ||
|
@@ -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> { | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 |
||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
|
@@ -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 | ||
|
@@ -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 { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
toget
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.