fix: use proper exit codes on failure in CLI

This commit is contained in:
Maarten de Waard 2022-10-13 12:23:02 +02:00
parent f6e0e4b608
commit 1b18b3d06e
No known key found for this signature in database
GPG key ID: 1D3E893A657CC8DA
2 changed files with 16 additions and 29 deletions

View file

@ -4,6 +4,7 @@ Hydra for OIDC sessions and MariaDB for application and role specifications.
The application provides also several command line options to interact with The application provides also several command line options to interact with
the user entries in the database(s)""" the user entries in the database(s)"""
import sys
import click import click
import hydra_client import hydra_client
@ -68,7 +69,7 @@ def create_app(slug, name, external_url = None):
if app_obj: if app_obj:
current_app.logger.info(f"App definition: {name} ({slug}) already exists in database") current_app.logger.info(f"App definition: {name} ({slug}) already exists in database")
return sys.exit(1)
if (external_url): if (external_url):
obj.external = True obj.external = True
@ -102,13 +103,13 @@ def delete_app(slug):
if not app_obj: if not app_obj:
current_app.logger.info("Not found") current_app.logger.info("Not found")
return sys.exit(1)
app_status = app_obj.get_status() app_status = app_obj.get_status()
if app_status.installed and not app_obj.external: if app_status.installed and not app_obj.external:
current_app.logger.info("Can not delete installed application, run" current_app.logger.info("Can not delete installed application, run"
" 'uninstall' first") " 'uninstall' first")
return sys.exit(1)
app_obj.delete() app_obj.delete()
current_app.logger.info("Success.") current_app.logger.info("Success.")
@ -127,11 +128,10 @@ def uninstall_app(slug):
if not app_obj: if not app_obj:
current_app.logger.info("Not found") current_app.logger.info("Not found")
return sys.exit(1)
app_obj.uninstall() app_obj.uninstall()
current_app.logger.info("Success.") current_app.logger.info("Success.")
return
@app_cli.command("status") @app_cli.command("status")
@click.argument("slug") @click.argument("slug")
@ -145,7 +145,7 @@ def status_app(slug):
if not app: if not app:
current_app.logger.error(f"App {slug} does not exist") current_app.logger.error(f"App {slug} does not exist")
return sys.exit(1)
current_app.logger.info(app.get_status()) current_app.logger.info(app.get_status())
@ -161,12 +161,12 @@ def install_app(slug):
if not app: if not app:
current_app.logger.error(f"App {slug} does not exist") current_app.logger.error(f"App {slug} does not exist")
return sys.exit(1)
if app.external: if app.external:
current_app.logger.info( current_app.logger.info(
f"App {slug} is an external app and can not be provisioned automatically") f"App {slug} is an external app and can not be provisioned automatically")
return sys.exit(1)
current_status = app.get_status() current_status = app.get_status()
if not current_status.installed: if not current_status.installed:
@ -188,7 +188,7 @@ def roles_app(slug):
if not app: if not app:
current_app.logger.error(f"App {slug} does not exist") current_app.logger.error(f"App {slug} does not exist")
return sys.exit(1)
current_app.logger.info("Roles: ") current_app.logger.info("Roles: ")
for role in app.roles: for role in app.roles:
@ -217,16 +217,16 @@ def setrole(email, app_slug, role):
if role not in ("admin", "user"): if role not in ("admin", "user"):
print("At this point only the roles 'admin' and 'user' are accepted") print("At this point only the roles 'admin' and 'user' are accepted")
return sys.exit(1)
if not user: if not user:
print("User not found. Abort") print("User not found. Abort")
return sys.exit(1)
app_obj = db.session.query(App).filter(App.slug == app_slug).first() app_obj = db.session.query(App).filter(App.slug == app_slug).first()
if not app_obj: if not app_obj:
print("App not found. Abort.") print("App not found. Abort.")
return sys.exit(1)
role_obj = ( role_obj = (
db.session.query(AppRole) db.session.query(AppRole)
@ -291,7 +291,7 @@ def update_user(email, field, value):
user = KratosUser.find_by_email(KRATOS_ADMIN, email) user = KratosUser.find_by_email(KRATOS_ADMIN, email)
if not user: if not user:
current_app.logger.error(f"User with email {email} not found.") current_app.logger.error(f"User with email {email} not found.")
return sys.exit(1)
if field == "name": if field == "name":
user.name = value user.name = value
@ -313,7 +313,7 @@ def delete_user(email):
user = KratosUser.find_by_email(KRATOS_ADMIN, email) user = KratosUser.find_by_email(KRATOS_ADMIN, email)
if not user: if not user:
current_app.logger.error(f"User with email {email} not found.") current_app.logger.error(f"User with email {email} not found.")
return sys.exit(1)
user.delete() user.delete()
@ -344,8 +344,6 @@ def setpassword_user(email, password):
"""Set a password for an account """Set a password for an account
:param email: email address of account to set a password for :param email: email address of account to set a password for
:param password: password to be set :param password: password to be set
:return: true on success, false if not set (too weak)
:rtype: boolean
:raise: exception if unexepted error happens :raise: exception if unexepted error happens
""" """
@ -362,7 +360,7 @@ def setpassword_user(email, password):
kratos_user = KratosUser.find_by_email(KRATOS_ADMIN, email) kratos_user = KratosUser.find_by_email(KRATOS_ADMIN, email)
if kratos_user is None: if kratos_user is None:
current_app.logger.error(f"User with email '{email}' not found") current_app.logger.error(f"User with email '{email}' not found")
return False sys.exit(1)
# Get a recovery URL # Get a recovery URL
url = kratos_user.get_recovery_link() url = kratos_user.get_recovery_link()
@ -372,15 +370,13 @@ def setpassword_user(email, password):
except Exception as error: # pylint: disable=broad-except except Exception as error: # pylint: disable=broad-except
current_app.logger.error(f"Error while setting password: {error}") current_app.logger.error(f"Error while setting password: {error}")
return False sys.exit(1)
if result: if result:
current_app.logger.info("Success setting password") current_app.logger.info("Success setting password")
else: else:
current_app.logger.error("Failed to set password. Password too weak?") current_app.logger.error("Failed to set password. Password too weak?")
return result
@user_cli.command("list") @user_cli.command("list")
def list_user(): def list_user():

View file

@ -1,9 +0,0 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>stackspin/renovate-config:default"
],
"assignees": [
"luka"
]
}