diff --git a/cmd/pls.go b/cmd/pls.go index 5d6a05b60..9b94c9e8f 100644 --- a/cmd/pls.go +++ b/cmd/pls.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "encoding/csv" "encoding/json" "errors" @@ -48,7 +49,7 @@ var ( Short: "Export playlists", Long: "Export Navidrome playlists to M3U files", Run: func(cmd *cobra.Command, args []string) { - runExporter() + runExporter(cmd.Context()) }, } @@ -56,13 +57,13 @@ var ( Use: "list", Short: "List playlists", Run: func(cmd *cobra.Command, args []string) { - runList() + runList(cmd.Context()) }, } ) -func runExporter() { - ds, ctx := getContext() +func runExporter(ctx context.Context) { + ds, ctx := getAdminContext(ctx) playlist, err := ds.Playlist(ctx).GetWithTracks(playlistID, true, false) if err != nil && !errors.Is(err, model.ErrNotFound) { log.Fatal("Error retrieving playlist", "name", playlistID, err) @@ -94,12 +95,12 @@ func runExporter() { } } -func runList() { +func runList(ctx context.Context) { if outputFormat != "csv" && outputFormat != "json" { log.Fatal("Invalid output format. Must be one of csv, json", "format", outputFormat) } - ds, ctx := getContext() + ds, ctx := getAdminContext(ctx) options := model.QueryOptions{Sort: "owner_name"} if userID != "" { diff --git a/cmd/user.go b/cmd/user.go index f7947551c..1abf157b7 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "encoding/csv" "encoding/json" "errors" @@ -86,7 +87,7 @@ var ( Aliases: []string{"c"}, Short: "Create a new user", Run: func(cmd *cobra.Command, args []string) { - runCreateUser() + runCreateUser(cmd.Context()) }, } @@ -95,7 +96,7 @@ var ( Aliases: []string{"d"}, Short: "Deletes an existing user", Run: func(cmd *cobra.Command, args []string) { - runDeleteUser() + runDeleteUser(cmd.Context()) }, } @@ -105,7 +106,7 @@ var ( Short: "Edit a user", Long: "Edit the password, admin status, and/or library access", Run: func(cmd *cobra.Command, args []string) { - runUserEdit() + runUserEdit(cmd.Context()) }, } @@ -113,7 +114,7 @@ var ( Use: "list", Short: "List users", Run: func(cmd *cobra.Command, args []string) { - runUserList() + runUserList(cmd.Context()) }, } ) @@ -161,7 +162,7 @@ func libraryError(libraries model.Libraries) error { return fmt.Errorf("not all available libraries found. Requested ids: %v, Found libraries: %v", libraryIds, ids) } -func runCreateUser() { +func runCreateUser(ctx context.Context) { password := promptPassword() if password == "" { log.Fatal("Empty password provided, user creation cancelled") @@ -179,7 +180,7 @@ func runCreateUser() { user.Name = userID } - ds, ctx := getContext() + ds, ctx := getAdminContext(ctx) err := ds.WithTx(func(tx model.DataStore) error { existingUser, err := tx.User(ctx).FindByUsername(userID) @@ -228,8 +229,8 @@ func runCreateUser() { log.Info(ctx, "Successfully created user", "id", user.ID, "username", user.UserName) } -func runDeleteUser() { - ds, ctx := getContext() +func runDeleteUser(ctx context.Context) { + ds, ctx := getAdminContext(ctx) var err error var user *model.User @@ -259,8 +260,8 @@ func runDeleteUser() { log.Info(ctx, "Deleted user", "username", user.UserName) } -func runUserEdit() { - ds, ctx := getContext() +func runUserEdit(ctx context.Context) { + ds, ctx := getAdminContext(ctx) var err error var user *model.User @@ -385,12 +386,12 @@ type displayUser struct { Libraries []displayLibrary `json:"libraries"` } -func runUserList() { +func runUserList(ctx context.Context) { if outputFormat != "csv" && outputFormat != "json" { log.Fatal("Invalid output format. Must be one of csv, json", "format", outputFormat) } - ds, ctx := getContext() + ds, ctx := getAdminContext(ctx) users, err := ds.User(ctx).ReadAll() if err != nil { diff --git a/cmd/utils.go b/cmd/utils.go index 2ddc8e30f..817a391d0 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -11,10 +11,10 @@ import ( "github.com/navidrome/navidrome/persistence" ) -func getContext() (model.DataStore, context.Context) { +func getAdminContext(ctx context.Context) (model.DataStore, context.Context) { sqlDB := db.Db() ds := persistence.New(sqlDB) - return ds, auth.WithAdminUser(context.Background(), ds) + return ds, auth.WithAdminUser(ctx, ds) } func getUser(ctx context.Context, id string, ds model.DataStore) (*model.User, error) {