Skip to content

Commit 25d8394

Browse files
authored
add --yes flag to skip confirmation for mscolab db commands (Open-MSS#2964)
1 parent d83d36d commit 25d8394

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

docs/mscolab.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ Steps to Run MSColab Server
3434
- To start the server run :code:`mscolab start`.
3535
- If you ever want to reset or add dummy data to your database you can use the commands :code:`mscolab db --reset` and :code:`mscolab db --seed` respectively.
3636

37+
Both commands support a :code:`--yes` (or :code:`-y`) flag to skip the interactive
38+
confirmation prompt. This is useful for non-interactive usage such as CI pipelines
39+
or tutorial scripts.
40+
41+
Examples::
42+
43+
mscolab db --reset --yes
44+
mscolab db --seed --yes
45+
3746

3847

3948
Notes for server administrators

mslib/mscolab/mscolab.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ def handle_start(args=None):
5858
start_server(APP, sockio, cm, fm)
5959

6060

61-
def confirm_action(confirmation_prompt):
61+
def confirm_action(confirmation_prompt, assume_yes=False):
62+
if assume_yes:
63+
return True
64+
6265
while True:
6366
confirmation = input(confirmation_prompt).lower()
6467
if confirmation == "n" or confirmation == "":
@@ -375,17 +378,27 @@ def main():
375378
default=None)
376379

377380
database_parser = subparsers.add_parser("db", help="Manage mscolab database")
378-
database_parser = database_parser.add_mutually_exclusive_group(required=True)
379-
database_parser.add_argument("--reset", help="Reset database", action="store_true")
380-
database_parser.add_argument("--seed", help="Seed database", action="store_true")
381-
database_parser.add_argument("--users_by_file", type=argparse.FileType('r'),
382-
help="adds users into database, fileformat: suggested_username name <email>")
383-
database_parser.add_argument("--delete_users_by_file", type=argparse.FileType('r'),
384-
help="removes users from the database, fileformat: email")
385-
database_parser.add_argument("--default_operation", help="adds all users into a default TEMPLATE operation",
386-
action="store_true")
387-
database_parser.add_argument("--add_all_to_all_operation", help="adds all users into all other operations",
388-
action="store_true")
381+
382+
db_actions = database_parser.add_mutually_exclusive_group(required=True)
383+
db_actions.add_argument("--reset", help="Reset database", action="store_true")
384+
db_actions.add_argument("--seed", help="Seed database", action="store_true")
385+
db_actions.add_argument("--users_by_file", type=argparse.FileType("r"),
386+
help="adds users into database, fileformat: suggested_username name <email>")
387+
db_actions.add_argument("--delete_users_by_file", type=argparse.FileType("r"),
388+
help="removes users from the database, fileformat: email")
389+
db_actions.add_argument("--default_operation",
390+
help="adds all users into a default TEMPLATE operation",
391+
action="store_true")
392+
db_actions.add_argument("--add_all_to_all_operation",
393+
help="adds all users into all other operations",
394+
action="store_true")
395+
396+
database_parser.add_argument(
397+
"-y", "--yes",
398+
action="store_true",
399+
help="Skip confirmation prompt"
400+
)
401+
389402
sso_conf_parser = subparsers.add_parser("sso_conf", help="single sign on process configurations")
390403
sso_conf_parser = sso_conf_parser.add_mutually_exclusive_group(required=True)
391404
sso_conf_parser.add_argument("--init_sso_crts",
@@ -418,20 +431,26 @@ def main():
418431

419432
elif args.action == "db":
420433
if args.reset:
421-
confirmation = confirm_action("Are you sure you want to reset the database? This would delete "
422-
"all your data! (y/[n]):")
434+
confirmation = confirm_action(
435+
"Are you sure you want to reset the database? This would delete "
436+
"all your data! (y/[n]):",
437+
assume_yes=args.yes)
423438
if confirmation is True:
424439
with APP.app_context():
425440
handle_db_reset()
426441
elif args.seed:
427-
confirmation = confirm_action("Are you sure you want to seed the database? Seeding will delete all your "
428-
"existing data and replace it with seed data (y/[n]):")
442+
confirmation = confirm_action(
443+
"Are you sure you want to seed the database? Seeding will delete all your "
444+
"existing data and replace it with seed data (y/[n]):",
445+
assume_yes=args.yes)
429446
if confirmation is True:
430447
with APP.app_context():
431448
handle_db_seed()
432449
elif args.users_by_file is not None:
433450
# fileformat: suggested_username name <email>
434-
confirmation = confirm_action("Are you sure you want to add users to the database? (y/[n]):")
451+
confirmation = confirm_action(
452+
"Are you sure you want to add users to the database? (y/[n]):",
453+
assume_yes=args.yes)
435454
if confirmation is True:
436455
for line in args.users_by_file.readlines():
437456
info = line.split()
@@ -442,19 +461,22 @@ def main():
442461
add_user(emailid, username, password, fullname)
443462
elif args.default_operation:
444463
confirmation = confirm_action(
445-
"Are you sure you want to add users to the default TEMPLATE operation? (y/[n]):")
464+
"Are you sure you want to add users to the default TEMPLATE operation? (y/[n]):",
465+
assume_yes=args.yes)
446466
if confirmation is True:
447467
# adds all users as collaborator on the operation TEMPLATE if not added, command can be repeated
448468
add_all_users_default_operation(access_level='admin')
449469
elif args.add_all_to_all_operation:
450470
confirmation = confirm_action(
451-
"Are you sure you want to add users to the ALL operations? (y/[n]):")
471+
"Are you sure you want to add users to the ALL operations? (y/[n]):",
472+
assume_yes=args.yes)
452473
if confirmation is True:
453474
# adds all users to all Operations
454475
add_all_users_to_all_operations()
455476
elif args.delete_users_by_file:
456477
confirmation = confirm_action(
457-
"Are you sure you want to delete a user? (y/[n]):")
478+
"Are you sure you want to delete a user? (y/[n]):",
479+
assume_yes=args.yes)
458480
if confirmation is True:
459481
# deletes users from the db
460482
for email in args.delete_users_by_file.readlines():

tutorials/start_tutorial.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ trap cleanup EXIT
4141

4242
if [[ "$2" == "./tutorial_mscolab.py" ]]; then
4343
# ToDo mscolab needs a -Y and a stop
44-
python $PYTHONPATH/mslib/mscolab/mscolab.py db --seed
44+
python $PYTHONPATH/mslib/mscolab/mscolab.py db --seed --yes
4545
# sleep(2)
4646
python $PYTHONPATH/mslib/mscolab/mscolab.py start &
4747
# sleep(2)

0 commit comments

Comments
 (0)