| 1 |
package ipc |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"net" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 10 |
) |
| 11 |
|
| 12 |
func ConnectAndExec(args []string) (*Response, error) { |
| 13 |
sockpath := xdg.RuntimePath("aerc.sock") |
| 14 |
conn, err := net.Dial("unix", sockpath) |
| 15 |
if err != nil { |
| 16 |
return nil, err |
| 17 |
} |
| 18 |
defer conn.Close() |
| 19 |
|
| 20 |
req, err := (&Request{Arguments: args}).Encode() |
| 21 |
if err != nil { |
| 22 |
return nil, fmt.Errorf("failed to encode request: %w", err) |
| 23 |
} |
| 24 |
|
| 25 |
_, err = conn.Write(append(req, '\n')) |
| 26 |
if err != nil { |
| 27 |
return nil, fmt.Errorf("failed to send message: %w", err) |
| 28 |
} |
| 29 |
scanner := bufio.NewScanner(conn) |
| 30 |
if !scanner.Scan() { |
| 31 |
return nil, errors.New("No response from server") |
| 32 |
} |
| 33 |
resp, err := DecodeResponse(scanner.Bytes()) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
|
| 38 |
return resp, nil |
| 39 |
} |