mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-29 21:27:05 +02:00
Dynamic per-backend configuration options
This commit is contained in:
parent
ae5f1c5f26
commit
c9fa21be73
14 changed files with 262 additions and 17 deletions
|
@ -63,7 +63,7 @@ func (l BackendList) Swap(i, j int) {
|
|||
type Capability = string
|
||||
|
||||
func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
|
||||
backendName, backend, err := resolveBackend(config)
|
||||
backendName, backend, err := backendWithConfig(config)
|
||||
var result T
|
||||
if err != nil {
|
||||
return result, err
|
||||
|
@ -78,6 +78,14 @@ func ResolveBackend[T interface{}](config *viper.Viper) (T, error) {
|
|||
return result, err
|
||||
}
|
||||
|
||||
func BackendByName(backendName string) (models.Backend, error) {
|
||||
backendType := knownBackends[backendName]
|
||||
if backendType == nil {
|
||||
return nil, fmt.Errorf("unknown backend %s", backendName)
|
||||
}
|
||||
return backendType(), nil
|
||||
}
|
||||
|
||||
func GetBackends() BackendList {
|
||||
backends := make(BackendList, 0)
|
||||
for name, backendFunc := range knownBackends {
|
||||
|
@ -107,13 +115,13 @@ var knownBackends = map[string]func() models.Backend{
|
|||
"subsonic": func() models.Backend { return &subsonic.SubsonicApiBackend{} },
|
||||
}
|
||||
|
||||
func resolveBackend(config *viper.Viper) (string, models.Backend, error) {
|
||||
func backendWithConfig(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)
|
||||
backend, err := BackendByName(backendName)
|
||||
if err != nil {
|
||||
return backendName, nil, err
|
||||
}
|
||||
return backendName, backendType().FromConfig(config), nil
|
||||
return backendName, backend.FromConfig(config), nil
|
||||
}
|
||||
|
||||
func ImplementsInterface[T interface{}](backend *models.Backend) (bool, string) {
|
||||
|
|
|
@ -36,6 +36,18 @@ type DeezerApiBackend struct {
|
|||
|
||||
func (b *DeezerApiBackend) Name() string { return "deezer" }
|
||||
|
||||
func (b *DeezerApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "client-id",
|
||||
Label: "Client ID",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "client-secret",
|
||||
Label: "Client secret",
|
||||
Type: models.Secret,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *DeezerApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.clientId = config.GetString("client-id")
|
||||
b.clientSecret = config.GetString("client-secret")
|
||||
|
|
|
@ -25,6 +25,8 @@ type DumpBackend struct{}
|
|||
|
||||
func (b *DumpBackend) Name() string { return "dump" }
|
||||
|
||||
func (b *DumpBackend) Options() *[]models.BackendOption { return nil }
|
||||
|
||||
func (b *DumpBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
return b
|
||||
}
|
||||
|
|
|
@ -33,6 +33,22 @@ type FunkwhaleApiBackend struct {
|
|||
|
||||
func (b *FunkwhaleApiBackend) Name() string { return "funkwhale" }
|
||||
|
||||
func (b *FunkwhaleApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "server-url",
|
||||
Label: "Server URL",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "username",
|
||||
Label: "User name",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "token",
|
||||
Label: "Access token",
|
||||
Type: models.Secret,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *FunkwhaleApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.client = NewClient(
|
||||
config.GetString("server-url"),
|
||||
|
|
|
@ -36,6 +36,26 @@ type JSPFBackend struct {
|
|||
|
||||
func (b *JSPFBackend) Name() string { return "jspf" }
|
||||
|
||||
func (b *JSPFBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "file-path",
|
||||
Label: "File path",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "title",
|
||||
Label: "Playlist title",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "username",
|
||||
Label: "User name",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "identifier",
|
||||
Label: "Unique playlist identifier",
|
||||
Type: models.String,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *JSPFBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.filePath = config.GetString("file-path")
|
||||
b.title = config.GetString("title")
|
||||
|
|
|
@ -43,6 +43,22 @@ type LastfmApiBackend struct {
|
|||
|
||||
func (b *LastfmApiBackend) Name() string { return "lastfm" }
|
||||
|
||||
func (b *LastfmApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "username",
|
||||
Label: "User name",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "client-id",
|
||||
Label: "Client ID",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "client-secret",
|
||||
Label: "Client secret",
|
||||
Type: models.Secret,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *LastfmApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
clientId := config.GetString("client-id")
|
||||
clientSecret := config.GetString("client-secret")
|
||||
|
|
|
@ -34,6 +34,18 @@ type ListenBrainzApiBackend struct {
|
|||
|
||||
func (b *ListenBrainzApiBackend) Name() string { return "listenbrainz" }
|
||||
|
||||
func (b *ListenBrainzApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "username",
|
||||
Label: "User name",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "token",
|
||||
Label: "Access token",
|
||||
Type: models.Secret,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *ListenBrainzApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.client = NewClient(config.GetString("token"))
|
||||
b.client.MaxResults = MaxItemsPerGet
|
||||
|
|
|
@ -33,6 +33,22 @@ type MalojaApiBackend struct {
|
|||
|
||||
func (b *MalojaApiBackend) Name() string { return "maloja" }
|
||||
|
||||
func (b *MalojaApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "server-url",
|
||||
Label: "Server URL",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "token",
|
||||
Label: "Access token",
|
||||
Type: models.Secret,
|
||||
}, {
|
||||
Name: "nofix",
|
||||
Label: "Disable auto correction of submitted listens",
|
||||
Type: models.Bool,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.client = NewClient(
|
||||
config.GetString("server-url"),
|
||||
|
|
|
@ -36,6 +36,22 @@ type ScrobblerLogBackend struct {
|
|||
|
||||
func (b *ScrobblerLogBackend) Name() string { return "scrobbler-log" }
|
||||
|
||||
func (b *ScrobblerLogBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "file-path",
|
||||
Label: "File path",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "include-skipped",
|
||||
Label: "Include skipped listens",
|
||||
Type: models.Bool,
|
||||
}, {
|
||||
Name: "append",
|
||||
Label: "Append to file",
|
||||
Type: models.Bool,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *ScrobblerLogBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.filePath = config.GetString("file-path")
|
||||
b.includeSkipped = config.GetBool("include-skipped")
|
||||
|
|
|
@ -39,6 +39,18 @@ type SpotifyApiBackend struct {
|
|||
|
||||
func (b *SpotifyApiBackend) Name() string { return "spotify" }
|
||||
|
||||
func (b *SpotifyApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "client-id",
|
||||
Label: "Client ID",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "client-secret",
|
||||
Label: "Client secret",
|
||||
Type: models.Secret,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *SpotifyApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.clientId = config.GetString("client-id")
|
||||
b.clientSecret = config.GetString("client-secret")
|
||||
|
|
|
@ -34,6 +34,22 @@ type SubsonicApiBackend struct {
|
|||
|
||||
func (b *SubsonicApiBackend) Name() string { return "subsonic" }
|
||||
|
||||
func (b *SubsonicApiBackend) Options() *[]models.BackendOption {
|
||||
return &[]models.BackendOption{{
|
||||
Name: "server-url",
|
||||
Label: "Server URL",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "username",
|
||||
Label: "User name",
|
||||
Type: models.String,
|
||||
}, {
|
||||
Name: "token",
|
||||
Label: "Access token",
|
||||
Type: models.Secret,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *SubsonicApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.client = subsonic.Client{
|
||||
Client: &http.Client{},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue