68 lines
1.0 KiB
Go
68 lines
1.0 KiB
Go
package hypr
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
func readSocketRaw(socket string, body io.Reader) (io.ReadCloser, error) {
|
|
conn, err := net.Dial("unix", socket)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, err := io.Copy(conn, body); err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
|
|
return conn, nil
|
|
}
|
|
|
|
func readSocketString(socket string, body io.Reader) (string, error) {
|
|
r, err := readSocketRaw(socket, body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer r.Close()
|
|
|
|
data, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(data), nil
|
|
}
|
|
|
|
func readSocket[T any](socket string, body io.Reader) (T, error) {
|
|
r, err := readSocketRaw(socket, body)
|
|
if err != nil {
|
|
return *new(T), err
|
|
}
|
|
defer r.Close()
|
|
|
|
value := new(T)
|
|
if err := json.NewDecoder(r).Decode(value); err != nil {
|
|
return *new(T), err
|
|
}
|
|
|
|
return *value, nil
|
|
}
|
|
|
|
func waitFor(condition func() bool) {
|
|
if condition() {
|
|
return
|
|
}
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
|
if condition() {
|
|
break
|
|
}
|
|
}
|
|
}
|