Implement "backends" command

This commit is contained in:
Philipp Wolfer 2023-11-10 08:43:17 +01:00
parent 153b1a0def
commit f9ac0328a7
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
3 changed files with 229 additions and 11 deletions

View file

@ -25,6 +25,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/spf13/viper"
@ -55,35 +56,105 @@ type ImportResult struct {
LastTimestamp time.Time
}
func resolveBackend(config *viper.Viper) (string, Backend, error) {
backendName := config.GetString("backend")
// fmt.Printf("requested backend %s\n", backendName)
backendType := knownBackends[backendName]
if backendType == nil {
return backendName, nil, errors.New(fmt.Sprintf("Unknown backend %s", backendName))
}
return backendName, backendType().FromConfig(config), nil
type BackendInfo struct {
Name string
ExportCapabilities []Capability
ImportCapabilities []Capability
}
type Capability = string
func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
expectedInterface := reflect.TypeOf((*T)(nil)).Elem()
backendName, backend, err := resolveBackend(config)
var result T
if err != nil {
return result, err
}
if backend != nil && reflect.TypeOf(backend).Implements(expectedInterface) {
implements, interfaceName := implementsInterface[T](backend)
if implements {
result = backend.(T)
} else {
err = errors.New(
fmt.Sprintf("Backend %s does not implement %s", backendName, expectedInterface.String()))
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() Backend{
"dump": func() Backend { return &DumpBackend{} },
"listenbrainz-api": func() Backend { return &ListenBrainzApiBackend{} },
"maloja-api": func() Backend { return &MalojaApiBackend{} },
"scrobbler-log": func() Backend { return &ScrobblerLogBackend{} },
}
func resolveBackend(config *viper.Viper) (string, 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 Backend) (bool, string) {
expectedInterface := reflect.TypeOf((*T)(nil)).Elem()
implements := backend != nil && reflect.TypeOf(backend).Implements(expectedInterface)
return implements, expectedInterface.Name()
}
func getExportCapabilities(backend Backend) []string {
caps := make([]Capability, 0)
var name string
var found bool
name, found = checkCapability[ListenExport](backend, "export")
if found {
caps = append(caps, name)
}
name, found = checkCapability[LovesExport](backend, "export")
if found {
caps = append(caps, name)
}
return caps
}
func getImportCapabilities(backend Backend) []Capability {
caps := make([]Capability, 0)
var name string
var found bool
name, found = checkCapability[ListenImport](backend, "import")
if found {
caps = append(caps, name)
}
name, found = checkCapability[LovesImport](backend, "import")
if found {
caps = append(caps, name)
}
return caps
}
func checkCapability[T interface{}](backend 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
}