add --remove-server/-R cli arg

This commit is contained in:
jesopo 2019-07-12 09:30:22 +01:00
parent c083d4845a
commit 89c12e845a
2 changed files with 19 additions and 0 deletions

View file

@ -21,6 +21,10 @@ class Servers(Table):
realname])
return self.database.execute_fetchone(
"SELECT server_id FROM servers ORDER BY server_id DESC LIMIT 1")[0]
def by_alias(self, alias: str) -> typing.Optional[int]:
ids = self.database.execute_fetchone(
"SELECT server_id FROM servers WHERE alias=?", [alias])
return ids[0] if ids else None
def get_all(self):
return self.database.execute_fetchall(
"SELECT server_id, alias FROM servers")
@ -45,6 +49,8 @@ class Servers(Table):
raise ValueError("Unknown column on servers table '%s'" % column)
self.database.execute(
"UPDATE servers SET %s=? WHERE server_id=?" % column, [value, id])
def delete(self, id: int):
self.database.execute("DELETE FROM servers WHERE server_id=?", [id])
class Channels(Table):
def add(self, server_id: int, name: str):

View file

@ -46,6 +46,9 @@ arg_parser.add_argument("--module-args", "-M",
arg_parser.add_argument("--startup-disconnects", "-D",
help="Tolerate failed connections on startup", action="store_true")
arg_parser.add_argument("--remove-server", "-R",
help="Remove a server by it's alias")
args = arg_parser.parse_args()
if args.version:
@ -63,6 +66,16 @@ log.info("Starting BitBot %s (Python v%s)",
database = Database.Database(log, args.database)
if args.remove_server:
alias = args.remove_server
id = database.servers.by_alias(alias)
if not id == None:
database.servers.delete(id)
print("Deleted server '%s'" % alias)
else:
sys.stderr.write("Unknown server '%s'\n" % alias)
sys.exit(0)
if args.add_server:
print("Adding a new server")
utils.cli.add_server(database)