mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-24 21:47:55 +02:00
Moved specifc backends into separate packages
This commit is contained in:
parent
dfaf21b234
commit
48c8843f91
17 changed files with 127 additions and 98 deletions
141
backends/backends.go
Normal file
141
backends/backends.go
Normal file
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
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/listenbrainz"
|
||||
"go.uploadedlobster.com/scotty/backends/maloja"
|
||||
"go.uploadedlobster.com/scotty/backends/scrobblerlog"
|
||||
"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-api": func() models.Backend { return &funkwhale.FunkwhaleApiBackend{} },
|
||||
"listenbrainz-api": func() models.Backend { return &listenbrainz.ListenBrainzApiBackend{} },
|
||||
"maloja-api": func() models.Backend { return &maloja.MalojaApiBackend{} },
|
||||
"scrobbler-log": func() models.Backend { return &scrobblerlog.ScrobblerLogBackend{} },
|
||||
}
|
||||
|
||||
func resolveBackend(config *viper.Viper) (string, models.Backend, error) {
|
||||
backendName := config.GetString("backend")
|
||||
backendType := knownBackends[backendName]
|
||||
if backendType == nil {
|
||||
return backendName, nil, errors.New(fmt.Sprintf("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.ListenExport](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.ListenImport](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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue