From 210e0e95fbd543dbe4b8b55280962d50e859375f Mon Sep 17 00:00:00 2001 From: "Max P." Date: Mon, 24 Mar 2025 14:15:28 +0100 Subject: [PATCH] Refactors command sending and adds CLI support Introduces argument parsing for command-line interaction, allowing users to specify commands dynamically. Refactors the command sending function to include detailed error handling and support for configurable socket paths via external configuration. --- send_cmd.py | 56 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/send_cmd.py b/send_cmd.py index 39505ec..7a5181a 100755 --- a/send_cmd.py +++ b/send_cmd.py @@ -1,17 +1,53 @@ #!/usr/bin/env python3 import socket import sys +import argparse +from configuration import read_configurations -SOCKET_PATH = "/tmp/voice.sock" +CONFIGURATION = read_configurations() -def send_cmd(cmd): - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client: - client.connect(SOCKET_PATH) - client.sendall(cmd.encode()) +def send_cmd(cmd: str, socket_path: str): + """ + Sends a command to a Unix domain socket server. + + This function creates a Unix domain socket, connects to the server + specified by the socket_path, and sends the provided command as a + UTF-8 encoded string. + + Args: + cmd (str): The command to send to the server. + socket_path (str): The path to the Unix domain socket. + + Raises: + FileNotFoundError: If the socket file specified by socket_path does not exist. + ConnectionRefusedError: If the connection to the server is refused. + OSError: For other socket-related errors. + """ + try: + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client: + client.connect(socket_path) + client.sendall(cmd.encode()) + except FileNotFoundError: + print(f"Error: The socket file '{socket_path}' does not exist.", file=sys.stderr) + except ConnectionRefusedError: + print(f"Error: Connection to the server at '{socket_path}' was refused.", file=sys.stderr) + except OSError as e: + print(f"Socket error: {e}", file=sys.stderr) + +def main(): + parser = argparse.ArgumentParser( + description="Send a command to a Unix domain socket server." + ) + parser.add_argument( + "command", + choices=["start", "stop", "toggle"], + nargs="?", + default="toggle", + help="The command to send to the server (default: toggle).", + ) + args = parser.parse_args() + + send_cmd(args.command, CONFIGURATION["socket_path"]) if __name__ == "__main__": - # Default: toggle - cmd = "toggle" - if len(sys.argv) == 2 and sys.argv[1] in ["start", "stop", "toggle"]: - cmd = sys.argv[1] - send_cmd(cmd) + main()