2022-09-19 01:32:23 +04:00
|
|
|
package radio
|
2022-08-29 07:20:36 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-09-19 01:32:23 +04:00
|
|
|
var ErrLiquidsoapNotRunning = errors.New("liquidsoap is not running")
|
2022-08-29 07:20:36 +04:00
|
|
|
|
|
|
|
type Liquidsoap struct {
|
|
|
|
command *exec.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLiquidsoap(liquidsoapPath, scriptPath string) (*Liquidsoap, error) {
|
2022-08-29 22:20:05 +04:00
|
|
|
if _, err := exec.LookPath(liquidsoapPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-29 23:10:39 +04:00
|
|
|
out, err := exec.Command(liquidsoapPath, "--verbose", "-c", scriptPath).CombinedOutput()
|
|
|
|
if err != nil {
|
2022-09-19 01:32:23 +04:00
|
|
|
return nil, errors.Wrap(err, "script cannot be validated")
|
2022-08-29 23:10:39 +04:00
|
|
|
}
|
2022-08-29 07:20:36 +04:00
|
|
|
|
2022-08-29 23:10:39 +04:00
|
|
|
if len(out) > 0 {
|
|
|
|
return nil, errors.Errorf("script validation failed: %s", string(out))
|
2022-08-29 07:20:36 +04:00
|
|
|
}
|
|
|
|
|
2022-08-29 23:10:39 +04:00
|
|
|
cmd := exec.Command(liquidsoapPath, scriptPath)
|
2022-08-29 07:20:36 +04:00
|
|
|
|
2022-08-29 23:10:39 +04:00
|
|
|
if err := cmd.Start(); err != nil {
|
2022-08-29 22:20:05 +04:00
|
|
|
return nil, err
|
2022-08-29 07:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Liquidsoap{
|
|
|
|
command: cmd}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Liquidsoap) Stop() error {
|
2022-08-29 23:12:47 +04:00
|
|
|
if l.command.Process == nil && l.command.ProcessState != nil {
|
2022-09-19 01:32:23 +04:00
|
|
|
return ErrLiquidsoapNotRunning
|
2022-08-29 07:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.command.Process.Signal(syscall.SIGINT); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.command.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|