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{}
|
currentSong := radio.Song{}
|
||||||
lstnrs := radio.NewListenerCounter()
|
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 {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
@ -127,11 +132,15 @@ func main() {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
case syscall.SIGHUP:
|
case syscall.SIGHUP:
|
||||||
plylst.Lock()
|
data, err := os.ReadFile(path.Join(*workDirPath, "playlists", *playlistName))
|
||||||
defer plylst.Unlock()
|
if err != nil {
|
||||||
if err := plylst.Reload(); err != nil {
|
log.Fatalln("Failed to read a playlist file:", err)
|
||||||
log.Println(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
|
package radio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -11,17 +10,16 @@ import (
|
|||||||
// Playlist holds a list of paths to a song files.
|
// Playlist holds a list of paths to a song files.
|
||||||
type Playlist struct {
|
type Playlist struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
filePath string
|
|
||||||
playlist []string
|
playlist []string
|
||||||
cur int
|
cur int
|
||||||
repeat bool
|
repeat bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPlaylist returns an instance of a Playlist struct with a loaded playlist.
|
// NewPlaylist returns an instance of a Playlist struct with a loaded playlist.
|
||||||
// Returns an error if failed to load a playlist file.
|
// Returns an error if failed to load a playlist.
|
||||||
func NewPlaylist(filePath string, repeat bool) (*Playlist, error) {
|
func NewPlaylistFromPlaintext(data []byte, repeat bool) (*Playlist, error) {
|
||||||
p := &Playlist{filePath: filePath, repeat: repeat}
|
p := &Playlist{repeat: repeat}
|
||||||
return p, p.load()
|
return p, p.LoadFromPlaintext(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next returns the next song to play. Returns an empty string if repeat is
|
// 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load reads a playlist file.
|
// Load accepts a byte array of plaintext data and parses it into an array of strings.
|
||||||
func (p *Playlist) load() error {
|
// Also resets a cur index to 0 to start over.
|
||||||
data, err := os.ReadFile(p.filePath)
|
func (p *Playlist) LoadFromPlaintext(data []byte) error {
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "cannot open a playlist file")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data) == 0 {
|
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")
|
p.playlist = strings.Split(string(data), "\n")
|
||||||
@ -58,7 +52,3 @@ func (p *Playlist) load() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Playlist) Reload() error {
|
|
||||||
return p.load()
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user