Improve LoadableSource implementation
This commit is contained in:
parent
eb2eeee3c7
commit
207a4562e6
2
app.go
2
app.go
@ -73,7 +73,7 @@ func (app *App) loadCustomSources() error {
|
||||
if newss, ok := sources.LoadableSources[csrc.Source]; !ok {
|
||||
return fmt.Errorf("Unable to load source #%d: %q: no such source registered", id, csrc.Source)
|
||||
} else {
|
||||
src, err := newss(csrc.KV)
|
||||
src, err := newss.LoadSource(csrc.KV)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to load source #%d (%s): %w", id, csrc.Source, err)
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type CustomSource struct {
|
||||
Source string `json:"src"`
|
||||
KV map[string]string `json:"kv"`
|
||||
KV map[string]interface{} `json:"kv"`
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package sources
|
||||
|
||||
import ()
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
var (
|
||||
LoadableSources = map[string]LoadaleSource{}
|
||||
@ -19,4 +21,17 @@ type PlayingSource interface {
|
||||
CurrentlyPlaying() string
|
||||
}
|
||||
|
||||
type LoadaleSource func(map[string]string) (SoundSource, error)
|
||||
type LoadaleSource struct {
|
||||
LoadSource func(map[string]interface{}) (SoundSource, error)
|
||||
Description string
|
||||
SourceDefinition interface{}
|
||||
}
|
||||
|
||||
func Unmarshal(in map[string]interface{}, out interface{}) error {
|
||||
jin, err := json.Marshal(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(jin, out)
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/DexterLB/mpvipc"
|
||||
@ -18,31 +17,23 @@ import (
|
||||
type MPVSource struct {
|
||||
process *exec.Cmd
|
||||
ipcSocketDir string
|
||||
Name string
|
||||
Options []string
|
||||
File string
|
||||
Name string `json:"name"`
|
||||
Options []string `json:"opts"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
sources.LoadableSources["mpv"] = NewMPVSource
|
||||
sources.LoadableSources["mpv"] = sources.LoadaleSource{
|
||||
LoadSource: NewMPVSource,
|
||||
Description: "Play any file, stream or URL through mpv",
|
||||
SourceDefinition: &MPVSource{},
|
||||
}
|
||||
}
|
||||
|
||||
func NewMPVSource(kv map[string]string) (sources.SoundSource, error) {
|
||||
func NewMPVSource(kv map[string]interface{}) (sources.SoundSource, error) {
|
||||
var s MPVSource
|
||||
|
||||
if name, ok := kv["name"]; ok {
|
||||
s.Name = name
|
||||
}
|
||||
|
||||
if opts, ok := kv["opts"]; ok {
|
||||
s.Options = strings.Split(opts, " ")
|
||||
}
|
||||
|
||||
if file, ok := kv["file"]; ok {
|
||||
s.File = file
|
||||
}
|
||||
|
||||
return &s, nil
|
||||
err := sources.Unmarshal(kv, &s)
|
||||
return &s, err
|
||||
}
|
||||
|
||||
func (s *MPVSource) GetName() string {
|
||||
|
Loading…
Reference in New Issue
Block a user