direttoforo/liquidsoap/spawn.go
2017-05-31 13:37:40 +02:00

57 lines
1,4 KiB
Go

package liquidsoap
import (
"os"
"os/exec"
"syscall"
)
// End describes how liquidsoap ended; successfully? With errors?
type End struct {
Retval int
Err error
}
// Success returns true if the process ended successfully
func (e *End) Success() bool {
if e.Err == nil && e.Retval == 0 {
return true
}
return false
}
// RunLiquidsoap will launch a new instance of liquidsoap and take control of it. It will also read output
// messages and send them to out chan. No magic will be done to liquidsoap config file, so if you want output
// messages, don't forget to set("log.stderr", true) in your liquidsoap file.
//
// RunLiquidsoap is an async function, which provides channels as feedback and needs a channel to ask for its
// termination
func RunLiquidsoap(configfile string, kill <-chan struct{}) (<-chan Output, <-chan End, error) {
out := make(chan Output)
exit := make(chan End, 1)
cmd := exec.Command("liquidsoap", "--enable-telnet", configfile)
cmd.Stderr = os.Stderr // connect liquidsoap err to process stderr
log, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
go Parse(log, out)
err = cmd.Start()
proc := cmd.Process
go func() {
<-kill
proc.Signal(syscall.SIGINT)
}()
go func() {
defer log.Close()
err = cmd.Wait()
close(out)
if err != nil {
exit <- End{Err: err}
} else {
exit <- End{}
}
close(exit)
}()
return out, exit, err
}