diff --git a/swh/deposit/create_user.py b/swh/deposit/cli.py old mode 100755 new mode 100644 rename from swh/deposit/create_user.py rename to swh/deposit/cli.py --- a/swh/deposit/create_user.py +++ b/swh/deposit/cli.py @@ -1,6 +1,4 @@ -#!/usr/bin/env python3 - -# Copyright (C) 2017 The Software Heritage developers +# Copyright (C) 2017-2019 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -8,22 +6,43 @@ import click from swh.deposit.config import setup_django_for +from swh.deposit.models import DepositClient, DepositCollection + +CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) -@click.command( - help='Create a user with some needed information (password, collection)') + +@click.group(context_settings=CONTEXT_SETTINGS) @click.option('--platform', default='development', help='development or production platform') +@click.pass_context +def cli(ctx, platform): + setup_django_for(platform) + + +@cli.group('user') +@click.pass_context +def user(ctx): + """Manipulate user.""" + pass + + +@user.command('create') @click.option('--username', required=True, help="User's name") -@click.option('--password', required=True, help="Desired user's password.") +@click.option('--password', required=True, + help="Desired user's password (plain).") @click.option('--firstname', default='', help="User's first name") @click.option('--lastname', default='', help="User's last name") @click.option('--email', default='', help="User's email") @click.option('--collection', help="User's collection") -def main(platform, username, password, firstname, lastname, email, collection): - setup_django_for(platform) +def create_user(username, password, firstname, lastname, email, collection): + """Create a user with some needed information (password, collection) + + If the collection does not exist, the creation process is stopped. - from swh.deposit.models import DepositClient, DepositCollection + The password is stored encrypted using django's utilies. + + """ try: collection = DepositCollection.objects.get(name=collection) @@ -34,10 +53,10 @@ # user create/update try: user = DepositClient.objects.get(username=username) - print('User %s exists, updating information.' % user) + click.echo_via_pager('User %s exists, updating information.' % user) user.set_password(password) except DepositClient.DoesNotExist: - print('Create new user %s' % username) + click.echo_via_pager('Create new user %s' % username) user = DepositClient.objects.create_user( username=username, password=password) @@ -49,7 +68,11 @@ user.is_active = True user.save() - print('Information registered for user %s' % user) + click.echo_via_pager('Information registered for user %s' % user) + + +def main(): + return cli(auto_envvar_prefix='SWH_DEPOSIT') if __name__ == '__main__':