switch to subparsers

This commit is contained in:
Jakob Ketterl 2021-02-12 18:34:28 +01:00
parent 3f3f5eacfe
commit ad0ca114f5
3 changed files with 40 additions and 35 deletions

View File

@ -14,7 +14,7 @@ case "$1" in
chown "${OWRX_USER}". ${OWRX_DATADIR}
# create initial openwebrx user
openwebrx-admin adduser --noninteractive --silent --user admin
openwebrx-admin --noninteractive --silent adduser admin
;;
*)
echo "postinst called with unknown argument '$1'" 1>&2

View File

@ -9,34 +9,49 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(level
def main():
print("OpenWebRX admin version {version}".format(version=openwebrx_version))
parser = argparse.ArgumentParser()
parser.add_argument("command", help="""One of the following commands:
adduser, removeuser, listusers, resetpassword, enableuser, disableuser""")
subparsers = parser.add_subparsers(title="Commands", dest="command")
adduser_parser = subparsers.add_parser("adduser", help="Add a new user")
adduser_parser.add_argument("user", help="Username to be added")
adduser_parser.set_defaults(cls=NewUser)
removeuser_parser = subparsers.add_parser("removeuser", help="Remove an existing user")
removeuser_parser.add_argument("user", help="Username to be remvoed")
removeuser_parser.set_defaults(cls=DeleteUser)
resetpassword_parser = subparsers.add_parser("resetpassword", help="Reset a user's password")
resetpassword_parser.add_argument("user", help="Username to be remvoed")
resetpassword_parser.set_defaults(cls=ResetPassword)
listusers_parser = subparsers.add_parser("listusers", help="List enabled users")
listusers_parser.add_argument("-a", "--all", action="store_true", help="Show all users (including disabled ones)")
listusers_parser.set_defaults(cls=ListUsers)
disableuser_parser = subparsers.add_parser("disableuser", help="Disable a user")
disableuser_parser.add_argument("user", help="Username to be disabled")
disableuser_parser.set_defaults(cls=DisableUser)
enableuser_parser = subparsers.add_parser("enableuser", help="Enable a user")
enableuser_parser.add_argument("user", help="Username to be enabled")
enableuser_parser.set_defaults(cls=EnableUser)
parser.add_argument("-v", "--version", action="store_true", help="Show the software version")
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", help="User name to perform action upon")
parser.add_argument("-a", "--all", action="store_true", help="Show all users")
args = parser.parse_args()
if args.command == "adduser":
command = NewUser()
elif args.command == "removeuser":
command = DeleteUser()
elif args.command == "resetpassword":
command = ResetPassword()
elif args.command == "listusers":
command = ListUsers()
elif args.command == "disableuser":
command = DisableUser()
elif args.command == "enableuser":
command = EnableUser()
if args.version:
print("OpenWebRX Admin CLI version {version}".format(version=openwebrx_version))
sys.exit(0)
if hasattr(args, "cls"):
command = args.cls()
else:
if not args.silent:
print("Unknown command: {command}".format(command=args.command))
parser.print_help()
sys.exit(1)
sys.exit(0)

View File

@ -13,16 +13,6 @@ class Command(ABC):
class UserCommand(Command, metaclass=ABCMeta):
def getUser(self, args):
if args.user:
return args.user
else:
if args.noninteractive:
print("ERROR: User name not specified")
sys.exit(1)
else:
return input("Please enter the user name: ")
def getPassword(self, args, username):
if args.noninteractive:
print("Generating password for user {username}...".format(username=username))
@ -47,7 +37,7 @@ class UserCommand(Command, metaclass=ABCMeta):
class NewUser(UserCommand):
def run(self, args):
username = self.getUser(args)
username = args.user
userList = UserList()
# early test to bypass the password stuff if the user already exists
if username in userList:
@ -62,7 +52,7 @@ class NewUser(UserCommand):
class DeleteUser(UserCommand):
def run(self, args):
username = self.getUser(args)
username = args.user
print("Deleting user {username}...".format(username=username))
userList = UserList()
userList.deleteUser(username)
@ -70,7 +60,7 @@ class DeleteUser(UserCommand):
class ResetPassword(UserCommand):
def run(self, args):
username = self.getUser(args)
username = args.user
password, generated = self.getPassword(args, username)
userList = UserList()
userList[username].setPassword(DefaultPasswordClass(password), must_change_password=generated)
@ -81,7 +71,7 @@ class ResetPassword(UserCommand):
class DisableUser(UserCommand):
def run(self, args):
username = self.getUser(args)
username = args.user
userList = UserList()
userList[username].disable()
userList.store()
@ -89,7 +79,7 @@ class DisableUser(UserCommand):
class EnableUser(UserCommand):
def run(self, args):
username = self.getUser(args)
username = args.user
userList = UserList()
userList[username].enable()
userList.store()