From d99669b3aabe0ca9c7b84a723d3e27de80a21ec0 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 6 Feb 2021 18:57:51 +0100 Subject: [PATCH] add "silent" flag to openwebrx-admin --- owrx/users.py | 14 +++++++++----- owrxadmin/__main__.py | 21 +++++++++++++++++---- owrxadmin/commands.py | 5 ++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/owrx/users.py b/owrx/users.py index 7e4f6bc..935480f 100644 --- a/owrx/users.py +++ b/owrx/users.py @@ -151,15 +151,19 @@ class UserList(object): except Exception: logger.exception("error while writing users file %s", usersFile) + def _getUsername(self, user): + if isinstance(user, User): + return user.name + elif isinstance(user, str): + return user + else: + raise ValueError("invalid user type") + def addUser(self, user: User): self[user.name] = user def deleteUser(self, user): - if isinstance(user, User): - username = user.name - else: - username = user - del self[username] + del self[self._getUsername(user)] def __delitem__(self, key): if key not in self.users: diff --git a/owrxadmin/__main__.py b/owrxadmin/__main__.py index 1e3a653..6cbb5b0 100644 --- a/owrxadmin/__main__.py +++ b/owrxadmin/__main__.py @@ -2,6 +2,7 @@ from owrx.version import openwebrx_version from owrxadmin.commands import NewUser, DeleteUser import argparse import sys +import traceback def main(): @@ -10,13 +11,25 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument("command", help="One of the following commands: adduser, removeuser") parser.add_argument("--noninteractive", action="store_true", help="Don't ask for any user input (useful for automation)") + parser.add_argument("--silent", action="store_true", help="Ignore errors (useful for automation)") parser.add_argument("-u", "--user") args = parser.parse_args() if args.command == "adduser": - NewUser().run(args) + command = NewUser() elif args.command == "removeuser": - DeleteUser().run(args) + command = DeleteUser() else: - print("Unknown command: {command}".format(command=args.command)) - sys.exit(1) + if not args.silent: + print("Unknown command: {command}".format(command=args.command)) + sys.exit(1) + sys.exit(0) + + try: + command.run(args) + except Exception: + if not args.silent: + print("Error running command:") + traceback.print_exc() + sys.exit(1) + sys.exit(0) diff --git a/owrxadmin/commands.py b/owrxadmin/commands.py index 8be9d5a..9a631a6 100644 --- a/owrxadmin/commands.py +++ b/owrxadmin/commands.py @@ -27,6 +27,10 @@ class UserCommand(Command, metaclass=ABCMeta): class NewUser(UserCommand): def run(self, args): username = self.getUser(args) + userList = UserList() + # early test to bypass the password stuff if the user already exists + if username in userList: + raise KeyError("User {username} already exists".format(username=username)) if args.noninteractive: print("Generating password for user {username}...".format(username=username)) @@ -43,7 +47,6 @@ class NewUser(UserCommand): sys.exit(1) print("Creating user {username}...".format(username=username)) - userList = UserList() user = User(name=username, enabled=True, password=DefaultPasswordClass(password)) userList.addUser(user)