Skip to content

Commit 3ecddcf

Browse files
authored
Merge pull request #19 from weaponsforge/feature/weaponsforge-18
Feature/weaponsforge 18
2 parents 4ae3c07 + 8ecc7f1 commit 3ecddcf

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
.tempfile
22
.env
33
.dbs
4-
*/
4+
*/
5+
*.zip
6+
*.json
7+
*.txt

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ Windows batch script to automate switching of MongoDB database credentials for a
108108
| 3 | Drop Database | It deletes a database defined in the MongoDB connection credentials. Currently available only for localhost MongoDB. |
109109
| 4 | List Databases | Lists the available databases from the defined MongoDB connection credentials. |
110110
| 5 | List Local Databases | Lists the available localhost databases. |
111-
| 6 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
112-
| 7 | Reset | Resets the database conection details |
111+
| 6 | Create Local Database and User | Creates a local database and a local user and password associated with the local database.<br>It also creates an empty collection using the database name with a `"_"` prefix i.e., `"_mydatabase"` |
112+
| 7 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
113+
| 8 | Reset | Resets the database conection details |
113114
| x | Exit | Exit the script. |
114115

115116

main.bat

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ EXIT /B 0
6666
echo [3] Drop Database
6767
echo [4] List Databases
6868
echo [5] List Local Databases
69-
echo [6] Update Connection Credentials
70-
echo [7] Reset
69+
echo [6] Create Local Database and User
70+
echo [7] Update Connection Credentials
71+
echo [8] Reset
7172
echo [x] Exit
7273
set "choice=-1"
7374
echo.
@@ -84,8 +85,9 @@ EXIT /B 0
8485
set /A NextScreen=_ViewDatabaseCredentials
8586
Goto ShowDatabases
8687
)
87-
if %choice% EQU 6 Goto SetDatabaseCredentials
88-
if %choice% EQU 7 Goto ResetData
88+
if %choice% EQU 6 Goto CreateDatabaseAndUser
89+
if %choice% EQU 7 Goto SetDatabaseCredentials
90+
if %choice% EQU 8 Goto ResetData
8991
if %choice% == x EXIT /B 0
9092

9193
Goto ViewDatabaseCredentials
@@ -344,6 +346,76 @@ EXIT /B 0
344346
GoTo SetDatabaseCredentials
345347
EXIT /B 0
346348

349+
:: Creates a local database and local database user
350+
:CreateDatabaseAndUser
351+
setlocal enabledelayedexpansion
352+
353+
cls
354+
echo ----------------------------------------------------------
355+
echo CREATE DATABASE AND USER
356+
echo ----------------------------------------------------------
357+
358+
set "databaseName="
359+
set "databaseUser="
360+
set "userPassword="
361+
362+
set /p databaseName="Enter the database name:"
363+
set /p databaseUser="Enter the database user:"
364+
set /p userPassword="Enter the database user password:"
365+
echo.
366+
367+
echo Do you want to create the database and user
368+
echo on host [%MONGO_HOST%]?
369+
echo - Database: %databaseName%
370+
echo - User: %databaseUser%
371+
echo - Passsword: %userPassword%
372+
echo.
373+
374+
set "continue=Y"
375+
echo Press enter to continue
376+
set /p continue=Type "n" and press enter to cancel:
377+
378+
if %continue% EQU n (
379+
set "retry=Y"
380+
set /p retry="Retry? [Y/n]:"
381+
382+
if /i "!retry!"=="n" (
383+
GoTo ViewDatabaseCredentials
384+
) else (
385+
GoTo CreateDatabaseAndUser
386+
)
387+
) else (
388+
(if %MONGO_HOST% EQU localhost (
389+
echo Creating local database and user...
390+
391+
%MONGO_SHELL% !databaseName! --eval "db.createCollection('_!databaseName!')"
392+
%MONGO_SHELL% !databaseName! --eval "db.createUser({user: '!databaseUser!' , pwd: '!userPassword!', roles: [{ role: 'readWrite', db: '!databaseName!' }]})"
393+
394+
echo Success!
395+
set /A NextScreen=_ViewDatabaseCredentials
396+
GoTo ShowDatabases
397+
) else (
398+
echo Creating remote database...
399+
400+
%MONGO_SHELL% mongodb+srv://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST%/!databaseName! --eval "db.createCollection('_!databaseName!'); db.adminCommand({ listDatabases: 1, nameOnly:true })" > .dbs
401+
402+
echo.
403+
echo Success!
404+
echo [NOTE]: Creating database users on remote hosts are currently not supported.
405+
echo [NOTE]: Skipping creating remote database user...
406+
407+
echo.
408+
echo ----------------------------------------------------------
409+
echo Host: %MONGO_HOST%
410+
echo Available databases:
411+
findstr /C:name .dbs
412+
413+
set /p go=Press enter to continue...
414+
GoTo ViewDatabaseCredentials
415+
))
416+
)
417+
EXIT /B 0
418+
347419

348420
::----------------------------------------------------------
349421
:: Utility helper scripts

0 commit comments

Comments
 (0)