[SERVER-36803] Ability to prompt for user input in shell Created: 22/Aug/18  Updated: 06/Dec/22  Resolved: 17/Dec/20

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Kevin Pulo Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 1
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-24391 Prompt for password on user creation ... Closed
related to SERVER-32849 Prompt before write commands with shell Open
related to SERVER-22551 Make db.dropDatabase() shell helper t... Closed
Assigned Teams:
Server Tooling & Methods
Participants:

 Description   

Like passwordPrompt() from SERVER-24391, but without disabling echo and with the ability to provide a custom prompt. (Unfortunately it can't be called prompt(), because this is already used as a way of specifying the interactive REPL prompt.)

This would be generally useful in a variety of places. Some examples (not suggesting we necessarily do any of these, but such a function would allow users to build this functionality into their .mongorc, if they wanted):

> db.auth()
Enter username: myusername
Enter password:

> use mydatabase
switched to db mydatabase
> db.dropDatabase()
ARE YOU SURE?  Type the name of the database to continue: no
Database name does not match, aborting.
> db.dropDatabase()
ARE YOU SURE?  Type the name of the database to continue: mydatabase
{ "ok" : 1 }

> safeMode()
Shell safe mode enabled.
> use proddb
switched to db proddb
> db.vital.remove( { foo: "bar" } )
This will remove 18 documents from proddb.vital.  Proceed (y/N)? y
WriteResult({ "nRemoved" : 18 })



 Comments   
Comment by Robert Guo (Inactive) [ 17/Dec/20 ]

We will no longer be adding new features to the old mongo shell outside of a small number of exceptions. Please consider switching to the new shell, mongosh, for general use cases.

If this ticket is desired for Server development, please reopen.

Comment by Asya Kamsky [ 21/Dec/18 ]

In the last skunkworks I implemented this as function confirmOkay which just returns whether the user typed "yes" back in to its prompting.   I didn't check it in as part of SERVER-36196 because that was the determination if the session is interactive (when shell is in script mode you do not want to prompt, I would think) so that was a prerequisite.

I would change this ticket title to be "Ability to prompt in interactive shell" the code looked something like this in shell_utils.cpp:

+BSONObj promptOkay(const BSONObj& a, void*) {
+    Prompter prompter1("Okay to run this query anyway? (type yes) > ");
+    return BSON("" << prompter1.confirmOkay());
+}
+ 
...
+bool Prompter::confirmOkay() {
+    std::string input = "";
+    std::cout << _prompt;
+    getline(std::cin, input);
+    return (input=="yes");
+}
+

plus in shell_utils.h

+    bool confirmOkay();

I didn't submit it because I didn't get around to setting it up to accept a prompting string and I didn't write tests, but leaving this here just as an FYI.

Generated at Thu Feb 08 04:44:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.