diff --git a/bitbotctl b/bitbotctl index 8fdb2817..a7fbeea0 100755 --- a/bitbotctl +++ b/bitbotctl @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import argparse, os +import argparse, os, sys directory = os.path.dirname(os.path.realpath(__file__)) @@ -10,14 +10,38 @@ arg_parser.add_argument("--database", "-d", help="Location of the sqlite3 database file", default=os.path.join(directory, "databases", "bot.db")) +arg_parser.add_argument("command") + args = arg_parser.parse_args() +def _die(s): + sys.stderr.write("%s\n" % s) + sys.exit(1) + +if args.command == "log": + arg_parser.add_argument("--level", "-l", help="Log level", + default="INFO") +else: + _die("Unknown command '%s'" % args.command) + +args = arg_parser.parse_args() + +sock_location = "%s.sock" % args.database +if not os.path.exists(sock_location): + _die("Failed to connect to BitBot instance") + import json, socket sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect("%s.sock" % args.database) -sock.send(b"0 version 0\n") -sock.send(b"1 log info\n") + +def _send(s): + sock.send(("%s\n" % s).encode("utf8")) + +_send("0 version 0") + +if args.command == "log": + _send("1 log %s" % args.level) read_buffer = b""