From 2d37d05726621be328312ad1a79f5f62b1882ba9 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 21 May 2023 20:22:33 +0200 Subject: allow YAML parsing of the spotify config --- .gitignore | 1 + config.go | 2 +- player.go | 2 +- spotify.go | 27 +++++++++++++++++---------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index f816994..b4823f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ wrms uploads +config.yml diff --git a/config.go b/config.go index 049145a..02becb9 100644 --- a/config.go +++ b/config.go @@ -16,7 +16,7 @@ type Config struct { LocalMusicDir string `yaml:"music-dir"` UploadDir string `yaml:"upload-dir"` Admin uuid.UUID `yaml:"admin"` - spotify *spotifyConfig + Spotify *SpotifyConfig `yaml:"spotify` HasUpload bool } diff --git a/player.go b/player.go index a3232dd..f8f6272 100644 --- a/player.go +++ b/player.go @@ -23,7 +23,7 @@ func NewPlayer(wrms *Wrms, backends []string) Player { for _, backend := range backends { switch backend { case "spotify": - b, err = NewSpotify(wrms.Config.spotify) + b, err = NewSpotify(wrms.Config.Spotify) case "youtube": b = NewYoutubeBackend() case "dummy": diff --git a/spotify.go b/spotify.go index 0e4974a..d075930 100644 --- a/spotify.go +++ b/spotify.go @@ -25,30 +25,37 @@ const ( deviceName = "wrms" ) -type spotifyConfig struct { - username string - password string +type SpotifyConfig struct { + Username string `yaml:"username"` + Password string `yaml:"password"` } type SpotifyBackend struct { session *core.Session } -func NewSpotify(config *spotifyConfig) (*SpotifyBackend, error) { +func NewSpotify(config *SpotifyConfig) (*SpotifyBackend, error) { spotify := SpotifyBackend{} - if config != nil { - config = &spotifyConfig{} + if config == nil { + config = &SpotifyConfig{} } - config.username = os.Getenv("WRMS_SPOTIFY_USER") - config.password = os.Getenv("WRMS_SPOTIFY_PASSWORD") + usernameEnv := os.Getenv("WRMS_SPOTIFY_USER") + if usernameEnv != "" { + config.Username = usernameEnv + } + + passwordEnv := os.Getenv("WRMS_SPOTIFY_PASSWORD") + if passwordEnv != "" { + config.Password = passwordEnv + } - if config.username == "" || config.password == "" { + if config.Username == "" || config.Password == "" { return nil, errors.New("User and password for spotify must be provided") } var err error - spotify.session, err = librespot.Login(config.username, config.password, deviceName) + spotify.session, err = librespot.Login(config.Username, config.Password, deviceName) if err != nil { return nil, err } -- cgit v1.2.3