From 2addd23ad6bc4db76f64ee0123b0c7cf297d951e Mon Sep 17 00:00:00 2001 From: Gustav Behm Date: Thu, 28 Mar 2024 21:31:52 +0100 Subject: [PATCH] Add a status subcommand --- setup.cfg | 2 +- src/action/cli.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index 338a5da..a142d5e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = action -version = 0.0.1 +version = 0.0.2 [options] packages = find_namespace: diff --git a/src/action/cli.py b/src/action/cli.py index ce06223..7f630c4 100644 --- a/src/action/cli.py +++ b/src/action/cli.py @@ -42,6 +42,10 @@ def parse_args(): trigger_cmd = subparsers.add_parser("trigger") add_action_arg(trigger_cmd) + status_cmd = subparsers.add_parser("status") + add_action_arg(status_cmd) + status_cmd.add_argument("-p", "--print-path", action="store_true") + return parser.parse_args() class Action: @@ -81,9 +85,18 @@ class Action: def datagram_received(self, data, addr): j = json.loads(data.decode()) - logger.info(f"action {self.action.name} triggered: {j}") - self.action.trigger() + logger.debug(f"received message: {j}") + if j.get("action") != self.action.name: + logger.warn(f"received unexpected message: {j}") + return + + timestamp = j.get("timestamp") + if not j.get("dry_run"): + logger.info(f"action {self.action.name} triggered: {timestamp}") + self.action.trigger() + else: + logger.info(f"action {self.action.name} dry run: {timestamp}") def mk_socket_path(args): socket_path = args.socket @@ -118,15 +131,30 @@ def bind(args): asyncio.run(main()) sys.exit(ec.status) -def trigger(args): +def trigger(args, dry_run=None): msg = { "action": args.action, "timestamp": datetime.datetime.utcnow().astimezone().isoformat(timespec='seconds'), } + + if dry_run is not None: + msg["dry_run"] = dry_run + s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) s.sendto(json.dumps(msg).encode("UTF-8"), mk_socket_path(args)) s.close() +def status(args): + if args.print_path: + print(mk_socket_path(args)) + return + + try: + trigger(args, dry_run=True) + sys.exit(0) + except ConnectionRefusedError: + sys.exit(1) + def main(): args = parse_args() if args.log_file is not None: @@ -138,3 +166,5 @@ def main(): bind(args) elif args.cmd == "trigger": trigger(args) + elif args.cmd == "status": + status(args) -- 2.45.2