From 1b18b3d06e79b2f59a170f129a9eea70d08e9e3a Mon Sep 17 00:00:00 2001
From: Maarten de Waard <maarten@greenhost.nl>
Date: Thu, 13 Oct 2022 12:23:02 +0200
Subject: [PATCH] fix: use proper exit codes on failure in CLI

---
 backend/cliapp/cliapp/cli.py | 36 ++++++++++++++++--------------------
 backend/renovate.json        |  9 ---------
 2 files changed, 16 insertions(+), 29 deletions(-)
 delete mode 100644 backend/renovate.json

diff --git a/backend/cliapp/cliapp/cli.py b/backend/cliapp/cliapp/cli.py
index 3355dc6a..3d2fedc6 100644
--- a/backend/cliapp/cliapp/cli.py
+++ b/backend/cliapp/cliapp/cli.py
@@ -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 user entries in the database(s)"""
 
+import sys
 
 import click
 import hydra_client
@@ -68,7 +69,7 @@ def create_app(slug, name, external_url = None):
 
     if app_obj:
         current_app.logger.info(f"App definition: {name} ({slug}) already exists in database")
-        return
+        sys.exit(1)
 
     if (external_url):
         obj.external = True
@@ -102,13 +103,13 @@ def delete_app(slug):
 
     if not app_obj:
         current_app.logger.info("Not found")
-        return
+        sys.exit(1)
 
     app_status = app_obj.get_status()
     if app_status.installed and not app_obj.external:
         current_app.logger.info("Can not delete installed application, run"
             " 'uninstall' first")
-        return
+        sys.exit(1)
 
     app_obj.delete()
     current_app.logger.info("Success.")
@@ -127,11 +128,10 @@ def uninstall_app(slug):
 
     if not app_obj:
         current_app.logger.info("Not found")
-        return
+        sys.exit(1)
 
     app_obj.uninstall()
     current_app.logger.info("Success.")
-    return
 
 @app_cli.command("status")
 @click.argument("slug")
@@ -145,7 +145,7 @@ def status_app(slug):
 
     if not app:
         current_app.logger.error(f"App {slug} does not exist")
-        return
+        sys.exit(1)
 
     current_app.logger.info(app.get_status())
 
@@ -161,12 +161,12 @@ def install_app(slug):
 
     if not app:
         current_app.logger.error(f"App {slug} does not exist")
-        return
+        sys.exit(1)
 
     if app.external:
         current_app.logger.info(
             f"App {slug} is an external app and can not be provisioned automatically")
-        return
+        sys.exit(1)
 
     current_status = app.get_status()
     if not current_status.installed:
@@ -188,7 +188,7 @@ def roles_app(slug):
 
     if not app:
         current_app.logger.error(f"App {slug} does not exist")
-        return
+        sys.exit(1)
 
     current_app.logger.info("Roles: ")
     for role in app.roles:
@@ -217,16 +217,16 @@ def setrole(email, app_slug, role):
 
     if role not in ("admin", "user"):
         print("At this point only the roles 'admin' and 'user' are accepted")
-        return
+        sys.exit(1)
 
     if not user:
         print("User not found. Abort")
-        return
+        sys.exit(1)
 
     app_obj = db.session.query(App).filter(App.slug == app_slug).first()
     if not app_obj:
         print("App not found. Abort.")
-        return
+        sys.exit(1)
 
     role_obj = (
         db.session.query(AppRole)
@@ -291,7 +291,7 @@ def update_user(email, field, value):
     user = KratosUser.find_by_email(KRATOS_ADMIN, email)
     if not user:
         current_app.logger.error(f"User with email {email} not found.")
-        return
+        sys.exit(1)
 
     if field == "name":
         user.name = value
@@ -313,7 +313,7 @@ def delete_user(email):
     user = KratosUser.find_by_email(KRATOS_ADMIN, email)
     if not user:
         current_app.logger.error(f"User with email {email} not found.")
-        return
+        sys.exit(1)
     user.delete()
 
 
@@ -344,8 +344,6 @@ def setpassword_user(email, password):
     """Set a password for an account
     :param email:    email address of account to set a password for
     :param password:  password to be set
-    :return:         true on success, false if not set (too weak)
-    :rtype: boolean
     :raise:          exception if unexepted error happens
     """
 
@@ -362,7 +360,7 @@ def setpassword_user(email, password):
         kratos_user = KratosUser.find_by_email(KRATOS_ADMIN, email)
         if kratos_user is None:
             current_app.logger.error(f"User with email '{email}' not found")
-            return False
+            sys.exit(1)
 
         # Get a recovery URL
         url = kratos_user.get_recovery_link()
@@ -372,15 +370,13 @@ def setpassword_user(email, password):
 
     except Exception as error:  # pylint: disable=broad-except
         current_app.logger.error(f"Error while setting password: {error}")
-        return False
+        sys.exit(1)
 
     if result:
         current_app.logger.info("Success setting password")
     else:
         current_app.logger.error("Failed to set password. Password too weak?")
 
-    return result
-
 
 @user_cli.command("list")
 def list_user():
diff --git a/backend/renovate.json b/backend/renovate.json
deleted file mode 100644
index 72770327..00000000
--- a/backend/renovate.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
-    "$schema": "https://docs.renovatebot.com/renovate-schema.json",
-    "extends": [
-        "local>stackspin/renovate-config:default"
-    ],
-    "assignees": [
-        "luka"
-    ]
-}
-- 
GitLab