Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -playlists-relative option #537

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/gonic/gonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {
flag.Var(&confMusicPaths, "music-path", "path to music")

confPlaylistsPath := flag.String("playlists-path", "", "path to your list of new or existing m3u playlists that gonic can manage")
confPlaylistsRelative := flag.Bool("playlists-relative", false, "make song file paths relative in the m3u playlists gonic generates")

confDBPath := flag.String("db-path", "gonic.db", "path to database (optional)")

Expand Down Expand Up @@ -147,6 +148,7 @@ func main() {
DBPath: *confDBPath,
OriginalMusicPath: confMusicPaths[0].path,
PlaylistsPath: *confPlaylistsPath,
PlaylistsRelative: *confPlaylistsRelative,
PodcastsPath: *confPodcastPath,
})
if err != nil {
Expand Down Expand Up @@ -215,7 +217,7 @@ func main() {
listenbrainzClient := listenbrainz.NewClient()
lastfmClient := lastfm.NewClient(lastfmClientKeySecretFunc)

playlistStore, err := playlist.NewStore(*confPlaylistsPath)
playlistStore, err := playlist.NewStore(*confPlaylistsPath, *confPlaylistsRelative)
if err != nil {
log.Panicf("error creating playlists store: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type MigrationContext struct {
DBPath string
OriginalMusicPath string
PlaylistsPath string
PlaylistsRelative bool
PodcastsPath string
}

Expand Down Expand Up @@ -507,7 +508,7 @@ func migratePlaylistsToM3U(tx *gorm.DB, ctx MigrationContext) error {
return ""
}

store, err := playlist.NewStore(ctx.PlaylistsPath)
store, err := playlist.NewStore(ctx.PlaylistsPath, ctx.PlaylistsRelative)
if err != nil {
return fmt.Errorf("create playlists store: %w", err)
}
Expand Down
21 changes: 19 additions & 2 deletions playlist/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -37,10 +38,11 @@ type Playlist struct {

type Store struct {
basePath string
relative bool
mu sync.Mutex
}

func NewStore(basePath string) (*Store, error) {
func NewStore(basePath string, relative bool) (*Store, error) {
if basePath == "" {
return nil, ErrInvalidBasePath
}
Expand All @@ -50,6 +52,7 @@ func NewStore(basePath string) (*Store, error) {

return &Store{
basePath: basePath,
relative: relative,
}, nil
}

Expand Down Expand Up @@ -126,6 +129,12 @@ func (s *Store) Read(relPath string) (*Playlist, error) {
if strings.HasPrefix(line, "#") {
continue
}

// file path might be relative if using -playlists-relative
if !filepath.IsAbs(line) {
line = filepath.Join(filepath.Dir(absPath), line)
}

playlist.Items = append(playlist.Items, line)
}

Expand Down Expand Up @@ -178,9 +187,17 @@ func (s *Store) Write(relPath string, playlist *Playlist) error {
fmt.Fprintln(file, encodeAttr(attrCommment, playlist.Comment))
fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic)))
for _, line := range playlist.Items {
if s.relative {
// transform to path relative to playlist's dir
relativePath, err := filepath.Rel(filepath.Dir(absPath), line)
if err == nil {
line = relativePath
} else {
log.Printf("Warning: could not make playlist entry path's relative - %v\n", err)
}
}
fmt.Fprintln(file, line)
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions playlist/playlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestPlaylist(t *testing.T) {
t.Parallel()

tmp := t.TempDir()
store, err := playlist.NewStore(tmp)
store, err := playlist.NewStore(tmp, false)
require.NoError(t, err)

playlistIDs, err := store.List()
Expand All @@ -32,9 +32,9 @@ Example comment
It has multiple lines 👍
`,
Items: []string{
"item 1.flac",
"item 2.flac",
"item 3.flac",
"/item 1.flac",
"/item 2.flac",
"/item 3.flac",
},
IsPublic: true,
}
Expand Down
Loading