if socket path is empty, try socket in $XDG_RUNTIME_DIR first

This commit is contained in:
Finn 2021-06-09 03:09:10 -07:00
parent f6e6cf4a14
commit 2a9e942b78
2 changed files with 29 additions and 5 deletions

View file

@ -36,9 +36,7 @@ var Path string
// Config is the variable the configuration gets loaded into // Config is the variable the configuration gets loaded into
// values shown here are used as defaults // values shown here are used as defaults
var Config = Configuration{ var Config = Configuration{}
SocketPath: "/var/run/signald/signald.sock",
}
func Load() error { func Load() error {
f, err := os.Open(Path) f, err := os.Open(Path)

View file

@ -31,8 +31,14 @@ import (
"gitlab.com/signald/signald-go/signald/client-protocol/v0" "gitlab.com/signald/signald-go/signald/client-protocol/v0"
) )
const (
defaultSocketPrefix = "/var/run"
socketSuffix = "/signald/signald.sock"
)
var ( var (
debugSignaldIO, _ = strconv.ParseBool(os.Getenv("DEBUG_SIGNALD_IO")) debugSignaldIO, _ = strconv.ParseBool(os.Getenv("DEBUG_SIGNALD_IO"))
xdgRuntimeDir = os.Getenv("XDG_RUNTIME_DIR")
) )
func init() { func init() {
@ -59,9 +65,29 @@ type UnexpectedError struct {
// Connect connects to the signad socket // Connect connects to the signad socket
func (s *Signald) Connect() error { func (s *Signald) Connect() error {
if s.SocketPath == "" { if s.SocketPath != "" {
s.SocketPath = "/var/run/signald/signald.sock" return s.connect()
} }
s.SocketPath = xdgRuntimeDir + socketSuffix
err := s.connect()
if err != nil {
_, ok := err.(net.Error)
if ok {
s.SocketPath = defaultSocketPrefix + socketSuffix
err = s.connect()
if err != nil {
return err
}
} else {
return err
}
}
return nil
}
func (s *Signald) connect() error {
socket, err := net.Dial("unix", s.SocketPath) socket, err := net.Dial("unix", s.SocketPath)
if err != nil { if err != nil {
return err return err