Repository navigation
CLI - Refactor yes/no prompts into a YesOrNo builder #6340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bfops
wants to merge
17
commits into
master
Choose a base branch
from
bfops/yes-or-no-builder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dbe30e0
Refactor yes/no prompts into a YesOrNo builder
bfops 4a6beeb
Require an explicit default and nonempty message for YesOrNo
bfops bd07870
Apply suggestion from @bfops
bfops e123249
Restore login-flow comments in the builder refactor
bfops c1be452
Require an explicit force setting for YesOrNo
bfops 4432c84
Use YesOrNo force setting for child database deletion
bfops c8b37b9
Always print the database tree before child deletion confirmation
bfops 1b914c1
Remove redundant YesOrNo unit tests
bfops a7f4747
Apply suggestion from @bfops
bfops 5702659
Return an error when a yes/no prompt reaches EOF
bfops b0b99c2
Separate prompt input read from EOF check
bfops 16196f5
Comment EOF error check
bfops c2c15a5
Apply suggestion from @bfops
bfops 1656d20
Merge branch 'master' into bfops/yes-or-no-builder
bfops 59ebb61
Send Enter to test default deletion refusal
bfops e7ffc83
Skip login prompts in publish smoketests
bfops 91d62d2
Merge branch 'master' into bfops/yes-or-no-builder
bfops 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -329,20 +329,53 @@ pub fn host_or_url_to_host_and_protocol(host_or_url: &str) -> (&str, Option<&str | |
| } | ||
| } | ||
|
|
||
| /// Prompt the user for `y` or `n` from stdin. | ||
| /// | ||
| /// Return `false` unless the input is `y`. | ||
| pub fn y_or_n(force: bool, prompt: &str) -> anyhow::Result<bool> { | ||
| if force { | ||
| println!("Skipping confirmation due to --yes"); | ||
| return Ok(true); | ||
| /// Prompt the user for `n` or `n` from stdin | ||
| pub struct YesOrNo { | ||
| message: String, | ||
| force: Option<bool>, | ||
| default: Option<bool>, | ||
| } | ||
|
|
||
| impl YesOrNo { | ||
| pub fn new(message: impl Into<String>) -> Self { | ||
| Self { | ||
| message: message.into(), | ||
| force: None, | ||
| default: None, | ||
| } | ||
| } | ||
|
|
||
| pub fn force(mut self, force: bool) -> Self { | ||
| self.force = Some(force); | ||
| self | ||
| } | ||
|
|
||
| /// Set the response when Enter is pressed without an answer. | ||
| pub fn default(mut self, default: bool) -> Self { | ||
| self.default = Some(default); | ||
| self | ||
| } | ||
|
|
||
| pub fn prompt(self) -> anyhow::Result<bool> { | ||
| assert!(!self.message.trim().is_empty(), "YesOrNo requires a message"); | ||
| let force = self.force.expect("YesOrNo requires an explicit force setting"); | ||
|
bfops marked this conversation as resolved.
|
||
| let default = self.default.expect("YesOrNo requires an explicit default"); | ||
| if force { | ||
| println!("Skipping confirmation due to --yes"); | ||
| return Ok(true); | ||
| } | ||
| let choices = if default { "Y/n" } else { "y/N" }; | ||
| print!("{} [{choices}]", self.message); | ||
| std::io::stdout().flush()?; | ||
| let mut input = String::new(); | ||
| let num_read = std::io::stdin().read_line(&mut input)?; | ||
| anyhow::ensure!(num_read != 0, "Unexpected EOF while reading response"); | ||
| let input = input.trim().to_lowercase(); | ||
| if input.is_empty() { | ||
| return Ok(default); | ||
| } | ||
| Ok(input == "y" || input == "yes") | ||
|
Collaborator
Author
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. note: this is the previous behavior, but since we now have an optional default, I could see an argument for requiring a clear yes or no, and ambiguous input causing a re-prompt. I'm happy to update that here/now if we think it's worth it. |
||
| } | ||
| let mut input = String::new(); | ||
| print!("{prompt} [y/N]"); | ||
| std::io::stdout().flush()?; | ||
| std::io::stdin().read_line(&mut input)?; | ||
| let input = input.trim().to_lowercase(); | ||
| Ok(input == "y" || input == "yes") | ||
| } | ||
|
|
||
| pub fn decode_identity(token: &String) -> anyhow::Result<String> { | ||
|
|
@@ -371,14 +404,16 @@ pub async fn get_login_token_or_log_in( | |
| return Ok(token.clone()); | ||
| } | ||
|
|
||
| // Note: We pass `force: false` to `y_or_n` because if we're running non-interactively we want to default to "no", not yes! | ||
| // Note: We leave `force` false because if we're running non-interactively we want to default to "no", not yes! | ||
| let full_login = interactive | ||
| && y_or_n( | ||
| false, | ||
| && YesOrNo::new( | ||
| // It would be "ideal" if we could print the `spacetimedb.com` by deriving it from the `default_auth_host` constant, | ||
| // but this will change _so_ infrequently that it's not even worth the time to write that code and test it. | ||
| "You are not logged in. Would you like to log in with spacetimedb.com?", | ||
| )?; | ||
| ) | ||
| .force(false) | ||
| .default(false) | ||
| .prompt()?; | ||
|
|
||
| if full_login { | ||
| let host = Url::parse(DEFAULT_AUTH_HOST)?; | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Note: I changed printing behavior here. Now in non-interactive mode we still print the database tree info and the prompt (followed by "skipping confirmation due to --yes").
That just seems clearer and better to me? Totally fine to revert it though.