From 08a534a60ebb4a4d344ed1bc320121ca0c9e942e Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 15 Jan 2025 23:35:41 +0400 Subject: [PATCH] Removed a code for reading of a playlist file from a Playlist struct, let it be simpler. load() func became LoadFromPlaintext() to parse a plaintext content. Also fixed a wrong use of defer inside a switch, it simply doesn't work like that as it is scoped to a func, not a block. --- cmd/dwelling-radio/main.go | 19 ++++++++++++++----- internal/radio/playlist.go | 26 ++++++++------------------ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index 6f72a5a..2749420 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -49,7 +49,12 @@ func main() { currentSong := radio.Song{} lstnrs := radio.NewListenerCounter() - plylst, err := radio.NewPlaylist(path.Join(*workDirPath, "playlists", *playlistName), true) + data, err := os.ReadFile(path.Join(*workDirPath, "playlists", *playlistName)) + if err != nil { + log.Fatalln("Failed to read a playlist file: ", err) + } + + plylst, err := radio.NewPlaylistFromPlaintext(data, true) if err != nil { log.Fatalln(err) } @@ -127,11 +132,15 @@ func main() { } return case syscall.SIGHUP: - plylst.Lock() - defer plylst.Unlock() - if err := plylst.Reload(); err != nil { - log.Println(err) + data, err := os.ReadFile(path.Join(*workDirPath, "playlists", *playlistName)) + if err != nil { + log.Fatalln("Failed to read a playlist file:", err) } + plylst.Lock() + if err := plylst.LoadFromPlaintext(data); err != nil { + log.Println("Failed to load a playlist:", err) + } + plylst.Unlock() } } } diff --git a/internal/radio/playlist.go b/internal/radio/playlist.go index 2721b5a..1cda7af 100644 --- a/internal/radio/playlist.go +++ b/internal/radio/playlist.go @@ -1,7 +1,6 @@ package radio import ( - "os" "strings" "sync" @@ -11,17 +10,16 @@ import ( // Playlist holds a list of paths to a song files. type Playlist struct { sync.Mutex - filePath string playlist []string cur int repeat bool } // NewPlaylist returns an instance of a Playlist struct with a loaded playlist. -// Returns an error if failed to load a playlist file. -func NewPlaylist(filePath string, repeat bool) (*Playlist, error) { - p := &Playlist{filePath: filePath, repeat: repeat} - return p, p.load() +// Returns an error if failed to load a playlist. +func NewPlaylistFromPlaintext(data []byte, repeat bool) (*Playlist, error) { + p := &Playlist{repeat: repeat} + return p, p.LoadFromPlaintext(data) } // Next returns the next song to play. Returns an empty string if repeat is @@ -42,15 +40,11 @@ func (p *Playlist) Next() (song string) { return } -// Load reads a playlist file. -func (p *Playlist) load() error { - data, err := os.ReadFile(p.filePath) - if err != nil { - return errors.Wrap(err, "cannot open a playlist file") - } - +// Load accepts a byte array of plaintext data and parses it into an array of strings. +// Also resets a cur index to 0 to start over. +func (p *Playlist) LoadFromPlaintext(data []byte) error { if len(data) == 0 { - return errors.New("a playlist file is empty. Did not update") + return errors.New("an empty playlist array was passed") } p.playlist = strings.Split(string(data), "\n") @@ -58,7 +52,3 @@ func (p *Playlist) load() error { return nil } - -func (p *Playlist) Reload() error { - return p.load() -}