package main import ( "os" "path/filepath" "git.milar.in/milarin/adverr" "git.milar.in/milarin/slices" ) type Playlist struct { Name string `json:"name"` Songs []Song `json:"songs"` } type Song struct { Name string `json:"name"` File string `json:"file"` } func GetAllPlaylists() ([]Playlist, error) { playlistNames, err := GetAllPlaylistNames() if err != nil { return nil, err } playlists := make([]Playlist, 0, len(playlistNames)) for _, playlistName := range playlistNames { songs, err := GetSongsByPlaylist(playlistName) if err != nil { return nil, err } playlists = append(playlists, Playlist{ Name: playlistName, Songs: songs, }) } return playlists, nil } func GetAllPlaylistNames() ([]string, error) { entries, err := os.ReadDir(LibraryPath) if err != nil { return nil, err } directories := slices.Filter(entries, And(IsDir, Not(IsHidden))) return slices.Map(directories, FsEntry2Name), nil } func GetSongsByPlaylist(playlist string) ([]Song, error) { entries, err := os.ReadDir(filepath.Join(LibraryPath, playlist)) if err != nil { return nil, err } symlinks := slices.Filter(entries, IsSymlink) return slices.Map(symlinks, func(entry os.DirEntry) Song { return Song{ Name: FsEntry2Name(entry), File: filepath.Base(adverr.Must(os.Readlink(filepath.Join(LibraryPath, playlist, entry.Name())))), } }), nil }