add "silent" flag to openwebrx-admin

This commit is contained in:
Jakob Ketterl 2021-02-06 18:57:51 +01:00
parent e548d6a5de
commit d99669b3aa
3 changed files with 30 additions and 10 deletions

View File

@ -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:

View File

@ -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:
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)

View File

@ -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)