55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
package radio
|
|
|
|
import (
|
|
"os/exec"
|
|
"syscall"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
var ErrLiquidsoapNotRunning = errors.New("liquidsoap is not running")
|
|
|
|
type Liquidsoap struct {
|
|
command *exec.Cmd
|
|
}
|
|
|
|
func NewLiquidsoap(liquidsoapPath, scriptPath string) (*Liquidsoap, error) {
|
|
if _, err := exec.LookPath(liquidsoapPath); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out, err := exec.Command(liquidsoapPath, "--verbose", "-c", scriptPath).CombinedOutput()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "script cannot be validated")
|
|
}
|
|
|
|
if len(out) > 0 {
|
|
return nil, errors.Errorf("script validation failed: %s", string(out))
|
|
}
|
|
|
|
cmd := exec.Command(liquidsoapPath, scriptPath)
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Liquidsoap{
|
|
command: cmd}, nil
|
|
}
|
|
|
|
func (l *Liquidsoap) Stop() error {
|
|
if l.command.Process == nil && l.command.ProcessState != nil {
|
|
return ErrLiquidsoapNotRunning
|
|
}
|
|
|
|
if err := l.command.Process.Signal(syscall.SIGINT); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.command.Wait(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|