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.
This commit is contained in:
parent
dc4235aafc
commit
08a534a60e
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user