openwebrx-clone/owrxadmin/__main__.py

70 lines
2.8 KiB
Python
Raw Normal View History

2021-02-06 17:04:32 +00:00
from owrx.version import openwebrx_version
from owrxadmin.commands import NewUser, DeleteUser, ResetPassword, ListUsers, DisableUser, EnableUser, HasUser
2021-02-06 17:04:32 +00:00
import argparse
import sys
2021-02-06 17:57:51 +00:00
import traceback
2021-02-12 16:00:51 +00:00
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2021-02-06 17:04:32 +00:00
2021-02-06 15:43:54 +00:00
def main():
2021-02-06 17:04:32 +00:00
parser = argparse.ArgumentParser()
2021-02-12 17:34:28 +00:00
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)
hasuser_parser = subparsers.add_parser("hasuser", help="Test if a user exists")
hasuser_parser.add_argument("user", help="Username to be checked")
hasuser_parser.set_defaults(cls=HasUser)
2021-02-12 17:34:28 +00:00
parser.add_argument("-v", "--version", action="store_true", help="Show the software version")
2021-02-06 18:03:28 +00:00
parser.add_argument(
"--noninteractive", action="store_true", help="Don't ask for any user input (useful for automation)"
)
2021-02-06 17:57:51 +00:00
parser.add_argument("--silent", action="store_true", help="Ignore errors (useful for automation)")
2021-02-06 17:04:32 +00:00
args = parser.parse_args()
2021-02-12 17:34:28 +00:00
if args.version:
print("OpenWebRX Admin CLI version {version}".format(version=openwebrx_version))
sys.exit(0)
if hasattr(args, "cls"):
command = args.cls()
2021-02-06 17:04:32 +00:00
else:
2021-02-06 17:57:51 +00:00
if not args.silent:
2021-02-12 17:34:28 +00:00
parser.print_help()
2021-02-06 17:57:51 +00:00
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)