mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-29 13:27:03 +02:00
Implement "backends" command
This commit is contained in:
parent
153b1a0def
commit
f9ac0328a7
3 changed files with 229 additions and 11 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue