From 064d2f0341a671b98190f22fa5978cbd548d4f97 Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 13 Oct 2021 15:26:42 -0800 Subject: [PATCH] Use a separate socket path to publish so pomo can publish and listen on different sockets --- pkg/internal/config.go | 2 ++ pkg/internal/server.go | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/pkg/internal/config.go b/pkg/internal/config.go index 04d3a44..d76fe4b 100644 --- a/pkg/internal/config.go +++ b/pkg/internal/config.go @@ -27,6 +27,8 @@ type Config struct { // PublishJson pushes socket updates as a JSON // encoded status message instead of string formatted PublishJson bool `json:"publishJson"` + // If Publish is true, provide a socket path to publish to + PublishSocketPath string `json:"publishSocketPath"` } type ColorMap struct { diff --git a/pkg/internal/server.go b/pkg/internal/server.go index 03e7443..5866a73 100644 --- a/pkg/internal/server.go +++ b/pkg/internal/server.go @@ -12,12 +12,12 @@ import ( // Server listens on a Unix domain socket // for Pomo status requests type Server struct { - listener net.Listener - runner *TaskRunner - running bool - publish bool - publishJson bool - socketPath string + listener net.Listener + runner *TaskRunner + running bool + publish bool + publishJson bool + publishSocketPath string } func (s *Server) listen() { @@ -38,7 +38,7 @@ func (s *Server) listen() { func (s *Server) push() { ticker := time.NewTicker(1 * time.Second) for s.running { - conn, err := net.Dial("unix", s.socketPath) + conn, err := net.Dial("unix", s.publishSocketPath) if err != nil { <-ticker.C continue @@ -59,9 +59,9 @@ func (s *Server) Start() { s.running = true if s.publish { go s.push() - } else { - go s.listen() } + + go s.listen() } func (s *Server) Stop() { @@ -72,13 +72,6 @@ func (s *Server) Stop() { } func NewServer(runner *TaskRunner, config *Config) (*Server, error) { - if config.Publish { - return &Server{ - runner: runner, - publish: true, - publishJson: config.PublishJson, - socketPath: config.SocketPath}, nil - } //check if socket file exists if _, err := os.Stat(config.SocketPath); err == nil { _, err := net.Dial("unix", config.SocketPath) @@ -94,7 +87,16 @@ func NewServer(runner *TaskRunner, config *Config) (*Server, error) { if err != nil { return nil, err } - return &Server{listener: listener, runner: runner}, nil + + server := &Server{ + listener: listener, + runner: runner, + publish: config.Publish, + publishJson: config.PublishJson, + publishSocketPath: config.PublishSocketPath, + } + + return server, nil } // Client makes requests to a listening