handle .connect() failure in bitbotctl

This commit is contained in:
jesopo 2019-11-11 11:52:35 +00:00
parent 9460238917
commit dc52200af8

View file

@ -16,9 +16,9 @@ arg_parser.add_argument("command")
args, unknown = arg_parser.parse_known_args() args, unknown = arg_parser.parse_known_args()
def _die(s): def _die(code: int, s: str):
sys.stderr.write("%s\n" % s) sys.stderr.write("%s\n" % s)
sys.exit(1) sys.exit(code)
SIMPLE = ["rehash", "reload", "stop"] SIMPLE = ["rehash", "reload", "stop"]
if args.command == "log": if args.command == "log":
@ -27,13 +27,10 @@ if args.command == "log":
elif args.command in SIMPLE: elif args.command in SIMPLE:
pass pass
else: else:
_die("Unknown command '%s'" % args.command) _die(1, "Unknown command '%s'" % args.command)
args = arg_parser.parse_args() args = arg_parser.parse_args()
if not os.path.exists(args.socket):
_die("Failed to connect to BitBot instance")
import json, socket, signal import json, socket, signal
def _sigint(_1, _2): def _sigint(_1, _2):
@ -41,9 +38,12 @@ def _sigint(_1, _2):
sys.exit(0) sys.exit(0)
signal.signal(signal.SIGINT, _sigint) signal.signal(signal.SIGINT, _sigint)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(args.socket)
try:
sock.connect(args.socket)
except ConnectionRefusedError:
_die(2, "Failed to connect to BitBot instance. Is bitbotd running?")
def _send(s): def _send(s):
sock.send(("%s\n" % s).encode("utf8")) sock.send(("%s\n" % s).encode("utf8"))