diff options
author | Moritz Poldrack <git@moritz.sh> | 2023-03-04 10:56:44 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-07 00:12:40 +0100 |
commit | 3dbf33bb4c8988851eeed0292fcdc170eb0ee6c7 (patch) | |
tree | eaa8d90c69aa457b7c1f4ceee9732b78fa9dd2f5 /lib/ipc/send.go | |
parent | 70dfd1bc40b633f83010ab1e5721a613f96efb87 (diff) | |
download | aerc-3dbf33bb4c8988851eeed0292fcdc170eb0ee6c7.tar.gz |
socket: refactor existing code
There are several //TODO comments in the socket package, these should be
fixed before expanding it.
Put send logic into it's own file and rename receiver code.
Fix the rather inelegant error handling when shutting down the server.
Make sure to close sockets.
Signed-off-by: Moritz Poldrack <git@moritz.sh>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/ipc/send.go')
-rw-r--r-- | lib/ipc/send.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/ipc/send.go b/lib/ipc/send.go new file mode 100644 index 00000000..5cc97cc0 --- /dev/null +++ b/lib/ipc/send.go @@ -0,0 +1,31 @@ +package ipc + +import ( + "bufio" + "errors" + "fmt" + "net" + "path" + + "github.com/kyoh86/xdg" +) + +func ConnectAndExec(msg 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")) + if err != nil { + return fmt.Errorf("failed to send message: %w", err) + } + scanner := bufio.NewScanner(conn) + if !scanner.Scan() { + return errors.New("No response from server") + } + result := scanner.Text() + fmt.Println(result) + return nil +} |