diff --git a/main.go b/main.go index fceb403..b0bad5f 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "net" "os" "os/exec" "path/filepath" @@ -42,6 +43,21 @@ func startServer() { } } +func checkServer() { + if gSocketProt == "unix" { + if _, err := os.Stat(gSocketPath); os.IsNotExist(err) { + startServer() + } else if _, err := net.Dial(gSocketProt, gSocketPath); err != nil { + os.Remove(gSocketPath) + startServer() + } + } else { + if _, err := net.Dial(gSocketProt, gSocketPath); err != nil { + startServer() + } + } +} + func main() { showDoc := flag.Bool( "doc", @@ -125,10 +141,7 @@ func main() { gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username)) serve() default: - // TODO: check if the socket is working - if _, err := os.Stat(gSocketPath); os.IsNotExist(err) { - startServer() - } + checkServer() gClientID = 1000 gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))