diff --git a/backend/configure.py b/backend/configure.py index ee26cac..e027805 100755 --- a/backend/configure.py +++ b/backend/configure.py @@ -8,32 +8,41 @@ import dotenv from django.db import transaction, IntegrityError -def yesno(prompt, default=False): - if not sys.stdin.isatty(): - return default - yes = {'yes', 'y', 'ye'} - no = {'no', 'n'} +class CmdCtx: - if default: - yes.add('') - else: - no.add('') + def __init__(self, args): + self.args = args - hint = ' [Y/n] ' if default else ' [y/N] ' - - while True: - choice = input(prompt + hint).lower() - if choice in yes: + def yesno(self, prompt, default=False): + if not sys.stdin.isatty() or self.args.noninteractive: + return default + elif self.args.yes: return True - elif choice in no: + elif self.args.no: return False + yes = {'yes', 'y', 'ye'} + no = {'no', 'n'} + + if default: + yes.add('') else: - print('Please respond with "yes" or "no"') + no.add('') + + hint = ' [Y/n] ' if default else ' [y/N] ' + + while True: + choice = input(prompt + hint).lower() + if choice in yes: + return True + elif choice in no: + return False + else: + print('Please respond with "yes" or "no"') -def configure(): +def configure(ctx): if not os.path.exists('.env'): - if not yesno("the .env file does not exist, do you want to create it?", default=True): + if not ctx.yesno("the .env file does not exist, do you want to create it?", default=True): print('Aborting') exit(0) if not os.path.exists('.env.dist'): @@ -56,7 +65,7 @@ def configure(): current_hosts = os.getenv('ALLOWED_HOSTS') print('Current ALLOWED_HOSTS: {}'.format(current_hosts)) - if yesno("Do you want to add ALLOWED_HOSTS?"): + if ctx.yesno("Do you want to add ALLOWED_HOSTS?"): hosts = input("Enter a comma-separated list of allowed hosts: ") joined_hosts = current_hosts + ',' + hosts if current_hosts else hosts dotenv.set_key('.env', 'ALLOWED_HOSTS', joined_hosts) @@ -67,20 +76,21 @@ def configure(): django.setup() if not os.path.exists('db.sqlite3'): - if not yesno("No database found, do you want to create one?", default=True): + if not ctx.yesno("No database found, do you want to create one?", default=True): print('Aborting') exit(0) from django.core.management import call_command call_command('migrate') - if yesno("Do you want to create a superuser?"): + if ctx.yesno("Do you want to create a superuser?"): from django.core.management import call_command call_command('createsuperuser') call_command('collectstatic', '--no-input') - if yesno("Do you want to import all categories, properties and tags contained in this repository?", default=True): + if ctx.yesno("Do you want to import all categories, properties and tags contained in this repository?", + default=True): from hostadmin.serializers import CategorySerializer, PropertySerializer, TagSerializer from hostadmin.models import ImportedIdentifierSets from hashlib import sha256 @@ -196,6 +206,7 @@ def main(): parser = ArgumentParser(description='Toolshed Server Configuration') parser.add_argument('--yes', '-y', help='Answer yes to all questions', action='store_true') parser.add_argument('--no', '-n', help='Answer no to all questions', action='store_true') + parser.add_argument('--noninteractive', '-x', help="Run in noninteractive mode", action='store_true') parser.add_argument('cmd', help='Command', default='configure', nargs='?') args = parser.parse_args() @@ -203,8 +214,10 @@ def main(): print('Error: --yes and --no are mutually exclusive') exit(1) + ctx = CmdCtx(args) + if args.cmd == 'configure': - configure() + configure(ctx) elif args.cmd == 'reset': reset() elif args.cmd == 'testdata':