From 4838efdb1d5a746432a30ef0b86b090aab52fa7a Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Sat, 4 Mar 2023 10:56:45 +0100 Subject: ipc: change protocol to JSON In overhauling the IPC, it has become necessary to switch to a more extendable message format, to ensure more complex commands can be sent. Messages have the following basic structure and must not contain linebreaks, as these are used to delimit separate messages from one another. {"arguments": ["mailto:moritz@poldrack.dev"]} The responses have the following structure: {"error": "epic fail"} If the IPC request was successful, "error" will be empty. {"error": ""} Signed-off-by: Moritz Poldrack Signed-off-by: Robin Jarry --- lib/ipc/send.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'lib/ipc/send.go') diff --git a/lib/ipc/send.go b/lib/ipc/send.go index 5cc97cc0..522e944a 100644 --- a/lib/ipc/send.go +++ b/lib/ipc/send.go @@ -10,14 +10,20 @@ import ( "github.com/kyoh86/xdg" ) -func ConnectAndExec(msg string) error { +func ConnectAndExec(args []string) error { sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock") conn, err := net.Dial("unix", sockpath) if err != nil { return err } defer conn.Close() - _, err = conn.Write([]byte(msg + "\n")) + + req, err := (&Request{Arguments: args}).Encode() + if err != nil { + return fmt.Errorf("failed to encode request: %w", err) + } + + _, err = conn.Write(append(req, '\n')) if err != nil { return fmt.Errorf("failed to send message: %w", err) } @@ -25,7 +31,17 @@ func ConnectAndExec(msg string) error { if !scanner.Scan() { return errors.New("No response from server") } - result := scanner.Text() - fmt.Println(result) + resp, err := DecodeResponse(scanner.Bytes()) + if err != nil { + return err + } + + // TODO: handle this in a more elegant manner + if resp.Error == "" { + fmt.Println("result: success") + } else { + fmt.Println("result: ", resp.Error) + } + return nil } -- cgit