mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-29 21:27:05 +02:00
Use config.ServiceConfig across API
This commit is contained in:
parent
091b3c2f49
commit
9c363cc06d
27 changed files with 137 additions and 99 deletions
|
@ -22,7 +22,6 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/deezer"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/dump"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
|
||||
|
@ -33,6 +32,7 @@ import (
|
|||
"go.uploadedlobster.com/scotty/internal/backends/scrobblerlog"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/spotify"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/subsonic"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
|
@ -62,8 +62,8 @@ func (l BackendList) Swap(i, j int) {
|
|||
|
||||
type Capability = string
|
||||
|
||||
func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
|
||||
backendName, backend, err := backendWithConfig(config)
|
||||
func ResolveBackend[T interface{}](config *config.ServiceConfig) (T, error) {
|
||||
backend, err := backendWithConfig(config)
|
||||
var result T
|
||||
if err != nil {
|
||||
return result, err
|
||||
|
@ -72,7 +72,7 @@ func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
|
|||
if implements {
|
||||
result = backend.(T)
|
||||
} else {
|
||||
err = fmt.Errorf("backend %s does not implement %s", backendName, interfaceName)
|
||||
err = fmt.Errorf("backend %s does not implement %s", config.Backend, interfaceName)
|
||||
}
|
||||
|
||||
return result, err
|
||||
|
@ -81,7 +81,7 @@ func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
|
|||
func BackendByName(backendName string) (models.Backend, error) {
|
||||
backendType := knownBackends[backendName]
|
||||
if backendType == nil {
|
||||
return nil, fmt.Errorf("unknown backend %s", backendName)
|
||||
return nil, fmt.Errorf("unknown backend \"%s\"", backendName)
|
||||
}
|
||||
return backendType(), nil
|
||||
}
|
||||
|
@ -115,13 +115,12 @@ var knownBackends = map[string]func() models.Backend{
|
|||
"subsonic": func() models.Backend { return &subsonic.SubsonicApiBackend{} },
|
||||
}
|
||||
|
||||
func backendWithConfig(config *viper.Viper) (string, models.Backend, error) {
|
||||
backendName := config.GetString("backend")
|
||||
backend, err := BackendByName(backendName)
|
||||
func backendWithConfig(config *config.ServiceConfig) (models.Backend, error) {
|
||||
backend, err := BackendByName(config.Backend)
|
||||
if err != nil {
|
||||
return backendName, nil, err
|
||||
return nil, err
|
||||
}
|
||||
return backendName, backend.FromConfig(config), nil
|
||||
return backend.FromConfig(config), nil
|
||||
}
|
||||
|
||||
func ImplementsInterface[T interface{}](backend *models.Backend) (bool, string) {
|
||||
|
|
|
@ -34,28 +34,32 @@ import (
|
|||
"go.uploadedlobster.com/scotty/internal/backends/scrobblerlog"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/spotify"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/subsonic"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
func TestResolveBackend(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("backend", "dump")
|
||||
backend, err := backends.ResolveBackend[models.ListensImport](config)
|
||||
c := viper.New()
|
||||
c.Set("backend", "dump")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend, err := backends.ResolveBackend[models.ListensImport](&service)
|
||||
assert.NoError(t, err)
|
||||
assert.IsType(t, &dump.DumpBackend{}, backend)
|
||||
}
|
||||
|
||||
func TestResolveBackendUnknown(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("backend", "foo")
|
||||
_, err := backends.ResolveBackend[models.ListensImport](config)
|
||||
assert.EqualError(t, err, "unknown backend foo")
|
||||
c := viper.New()
|
||||
c.Set("backend", "foo")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
_, err := backends.ResolveBackend[models.ListensImport](&service)
|
||||
assert.EqualError(t, err, "unknown backend \"foo\"")
|
||||
}
|
||||
|
||||
func TestResolveBackendInvalidInterface(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("backend", "dump")
|
||||
_, err := backends.ResolveBackend[models.ListensExport](config)
|
||||
c := viper.New()
|
||||
c.Set("backend", "dump")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
_, err := backends.ResolveBackend[models.ListensExport](&service)
|
||||
assert.EqualError(t, err, "backend dump does not implement ListensExport")
|
||||
}
|
||||
|
||||
|
|
|
@ -22,8 +22,8 @@ import (
|
|||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/auth"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
@ -48,7 +48,7 @@ func (b *DeezerApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *DeezerApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *DeezerApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.clientId = config.GetString("client-id")
|
||||
b.clientSecret = config.GetString("client-secret")
|
||||
return b
|
||||
|
|
|
@ -25,13 +25,15 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/deezer"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("client-id", "someclientid")
|
||||
config.Set("client-secret", "someclientsecret")
|
||||
backend := (&deezer.DeezerApiBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("client-id", "someclientid")
|
||||
c.Set("client-secret", "someclientsecret")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&deezer.DeezerApiBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &deezer.DeezerApiBackend{}, backend)
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
|
|||
package dump
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
|
@ -27,7 +27,7 @@ func (b *DumpBackend) Name() string { return "dump" }
|
|||
|
||||
func (b *DumpBackend) Options() *[]models.BackendOption { return nil }
|
||||
|
||||
func (b *DumpBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *DumpBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
return b
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ import (
|
|||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
|
@ -49,7 +49,7 @@ func (b *FunkwhaleApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *FunkwhaleApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *FunkwhaleApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.client = NewClient(
|
||||
config.GetString("server-url"),
|
||||
config.GetString("token"),
|
||||
|
|
|
@ -24,13 +24,15 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("token", "thetoken")
|
||||
backend := (&funkwhale.FunkwhaleApiBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("token", "thetoken")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&funkwhale.FunkwhaleApiBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &funkwhale.FunkwhaleApiBackend{}, backend)
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"go.uploadedlobster.com/scotty/pkg/jspf"
|
||||
)
|
||||
|
@ -56,7 +56,7 @@ func (b *JSPFBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *JSPFBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *JSPFBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.filePath = config.GetString("file-path")
|
||||
b.title = config.GetString("title")
|
||||
b.creator = config.GetString("username")
|
||||
|
|
|
@ -22,15 +22,17 @@ import (
|
|||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/scrobblerlog"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/jspf"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("file-path", "/foo/bar.jspf")
|
||||
config.Set("title", "My Playlist")
|
||||
config.Set("username", "outsidecontext")
|
||||
config.Set("identifier", "http://example.com/playlist1")
|
||||
backend := (&scrobblerlog.ScrobblerLogBackend{}).FromConfig(config)
|
||||
assert.IsType(t, &scrobblerlog.ScrobblerLogBackend{}, backend)
|
||||
c := viper.New()
|
||||
c.Set("file-path", "/foo/bar.jspf")
|
||||
c.Set("title", "My Playlist")
|
||||
c.Set("username", "outsidecontext")
|
||||
c.Set("identifier", "http://example.com/playlist1")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&jspf.JSPFBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &jspf.JSPFBackend{}, backend)
|
||||
}
|
||||
|
|
|
@ -23,8 +23,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/shkh/lastfm-go/lastfm"
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/auth"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
@ -59,7 +59,7 @@ func (b *LastfmApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *LastfmApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *LastfmApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
clientId := config.GetString("client-id")
|
||||
clientSecret := config.GetString("client-secret")
|
||||
b.client = lastfm.New(clientId, clientSecret)
|
||||
|
|
|
@ -21,7 +21,7 @@ import (
|
|||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"go.uploadedlobster.com/scotty/internal/version"
|
||||
)
|
||||
|
@ -46,7 +46,7 @@ func (b *ListenBrainzApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *ListenBrainzApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *ListenBrainzApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.client = NewClient(config.GetString("token"))
|
||||
b.client.MaxResults = MaxItemsPerGet
|
||||
b.username = config.GetString("username")
|
||||
|
|
|
@ -24,13 +24,15 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("token", "thetoken")
|
||||
backend := (&listenbrainz.ListenBrainzApiBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("token", "thetoken")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&listenbrainz.ListenBrainzApiBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &listenbrainz.ListenBrainzApiBackend{}, backend)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
|
@ -49,7 +49,7 @@ func (b *MalojaApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *MalojaApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.client = NewClient(
|
||||
config.GetString("server-url"),
|
||||
config.GetString("token"),
|
||||
|
|
|
@ -23,12 +23,14 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/maloja"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("token", "thetoken")
|
||||
backend := (&maloja.MalojaApiBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("token", "thetoken")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&maloja.MalojaApiBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &maloja.MalojaApiBackend{}, backend)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ import (
|
|||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
|
@ -52,7 +52,7 @@ func (b *ScrobblerLogBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *ScrobblerLogBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *ScrobblerLogBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.filePath = config.GetString("file-path")
|
||||
b.includeSkipped = config.GetBool("include-skipped")
|
||||
b.append = true
|
||||
|
|
|
@ -22,11 +22,13 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/scrobblerlog"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("token", "thetoken")
|
||||
backend := (&scrobblerlog.ScrobblerLogBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("token", "thetoken")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&scrobblerlog.ScrobblerLogBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &scrobblerlog.ScrobblerLogBackend{}, backend)
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/auth"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/spotify"
|
||||
|
@ -51,7 +51,7 @@ func (b *SpotifyApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *SpotifyApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *SpotifyApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.clientId = config.GetString("client-id")
|
||||
b.clientSecret = config.GetString("client-secret")
|
||||
return b
|
||||
|
|
|
@ -27,13 +27,15 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/spotify"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("client-id", "someclientid")
|
||||
config.Set("client-secret", "someclientsecret")
|
||||
backend := (&spotify.SpotifyApiBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("client-id", "someclientid")
|
||||
c.Set("client-secret", "someclientsecret")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&spotify.SpotifyApiBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &spotify.SpotifyApiBackend{}, backend)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/delucks/go-subsonic"
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"go.uploadedlobster.com/scotty/internal/version"
|
||||
)
|
||||
|
@ -50,7 +50,7 @@ func (b *SubsonicApiBackend) Options() *[]models.BackendOption {
|
|||
}}
|
||||
}
|
||||
|
||||
func (b *SubsonicApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *SubsonicApiBackend) FromConfig(config *config.ServiceConfig) models.Backend {
|
||||
b.client = subsonic.Client{
|
||||
Client: &http.Client{},
|
||||
BaseUrl: config.GetString("server-url"),
|
||||
|
|
|
@ -24,13 +24,15 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/subsonic"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("server-url", "https://subsonic.example.com")
|
||||
config.Set("token", "thetoken")
|
||||
backend := (&subsonic.SubsonicApiBackend{}).FromConfig(config)
|
||||
c := viper.New()
|
||||
c.Set("server-url", "https://subsonic.example.com")
|
||||
c.Set("token", "thetoken")
|
||||
service := config.NewServiceConfig("test", c)
|
||||
backend := (&subsonic.SubsonicApiBackend{}).FromConfig(&service)
|
||||
assert.IsType(t, &subsonic.SubsonicApiBackend{}, backend)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue