mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 01:59:29 +02:00
147 lines
4.8 KiB
Go
147 lines
4.8 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package backends
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
"go.uploadedlobster.com/scotty/backends/dump"
|
|
"go.uploadedlobster.com/scotty/backends/funkwhale"
|
|
"go.uploadedlobster.com/scotty/backends/jspf"
|
|
"go.uploadedlobster.com/scotty/backends/listenbrainz"
|
|
"go.uploadedlobster.com/scotty/backends/maloja"
|
|
"go.uploadedlobster.com/scotty/backends/scrobblerlog"
|
|
"go.uploadedlobster.com/scotty/backends/spotify"
|
|
"go.uploadedlobster.com/scotty/backends/subsonic"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
)
|
|
|
|
type BackendInfo struct {
|
|
Name string
|
|
ExportCapabilities []Capability
|
|
ImportCapabilities []Capability
|
|
}
|
|
|
|
type Capability = string
|
|
|
|
func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
|
|
backendName, backend, err := resolveBackend(config)
|
|
var result T
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
implements, interfaceName := ImplementsInterface[T](&backend)
|
|
if implements {
|
|
result = backend.(T)
|
|
} else {
|
|
err = errors.New(
|
|
fmt.Sprintf("Backend %s does not implement %s", backendName, interfaceName))
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
func GetBackends() []BackendInfo {
|
|
backends := make([]BackendInfo, 0)
|
|
for name, backendFunc := range knownBackends {
|
|
backend := backendFunc()
|
|
info := BackendInfo{
|
|
Name: name,
|
|
ExportCapabilities: getExportCapabilities(backend),
|
|
ImportCapabilities: getImportCapabilities(backend),
|
|
}
|
|
backends = append(backends, info)
|
|
}
|
|
|
|
return backends
|
|
}
|
|
|
|
var knownBackends = map[string]func() models.Backend{
|
|
"dump": func() models.Backend { return &dump.DumpBackend{} },
|
|
"funkwhale": func() models.Backend { return &funkwhale.FunkwhaleApiBackend{} },
|
|
"jspf": func() models.Backend { return &jspf.JspfBackend{} },
|
|
"listenbrainz": func() models.Backend { return &listenbrainz.ListenBrainzApiBackend{} },
|
|
"maloja": func() models.Backend { return &maloja.MalojaApiBackend{} },
|
|
"scrobbler-log": func() models.Backend { return &scrobblerlog.ScrobblerLogBackend{} },
|
|
"spotify": func() models.Backend { return &spotify.SpotifyApiBackend{} },
|
|
"subsonic": func() models.Backend { return &subsonic.SubsonicApiBackend{} },
|
|
}
|
|
|
|
func resolveBackend(config *viper.Viper) (string, models.Backend, error) {
|
|
backendName := config.GetString("backend")
|
|
backendType := knownBackends[backendName]
|
|
if backendType == nil {
|
|
return backendName, nil, fmt.Errorf("Unknown backend %s", backendName)
|
|
}
|
|
return backendName, backendType().FromConfig(config), nil
|
|
}
|
|
|
|
func ImplementsInterface[T interface{}](backend *models.Backend) (bool, string) {
|
|
expectedInterface := reflect.TypeOf((*T)(nil)).Elem()
|
|
implements := backend != nil && reflect.TypeOf(*backend).Implements(expectedInterface)
|
|
return implements, expectedInterface.Name()
|
|
}
|
|
|
|
func getExportCapabilities(backend models.Backend) []string {
|
|
caps := make([]Capability, 0)
|
|
var name string
|
|
var found bool
|
|
name, found = checkCapability[models.ListensExport](backend, "export")
|
|
if found {
|
|
caps = append(caps, name)
|
|
}
|
|
name, found = checkCapability[models.LovesExport](backend, "export")
|
|
if found {
|
|
caps = append(caps, name)
|
|
}
|
|
return caps
|
|
}
|
|
|
|
func getImportCapabilities(backend models.Backend) []Capability {
|
|
caps := make([]Capability, 0)
|
|
var name string
|
|
var found bool
|
|
name, found = checkCapability[models.ListensImport](backend, "import")
|
|
if found {
|
|
caps = append(caps, name)
|
|
}
|
|
name, found = checkCapability[models.LovesImport](backend, "import")
|
|
if found {
|
|
caps = append(caps, name)
|
|
}
|
|
return caps
|
|
}
|
|
|
|
func checkCapability[T interface{}](backend models.Backend, suffix string) (string, bool) {
|
|
implements, name := ImplementsInterface[T](&backend)
|
|
if implements {
|
|
cap, found := strings.CutSuffix(strings.ToLower(name), suffix)
|
|
if found {
|
|
return cap, found
|
|
}
|
|
}
|
|
|
|
return "", false
|
|
}
|