Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/gob"
|
|
"os"
|
|
|
|
"github.com/navidrome/navidrome/core"
|
|
"github.com/navidrome/navidrome/db"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
"github.com/navidrome/navidrome/scanner"
|
|
"github.com/navidrome/navidrome/utils/pl"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
fullScan bool
|
|
subprocess bool
|
|
targets []string
|
|
)
|
|
|
|
func init() {
|
|
scanCmd.Flags().BoolVarP(&fullScan, "full", "f", false, "check all subfolders, ignoring timestamps")
|
|
scanCmd.Flags().BoolVarP(&subprocess, "subprocess", "", false, "run as subprocess (internal use)")
|
|
scanCmd.Flags().StringArrayVarP(&targets, "target", "t", []string{}, "list of libraryID:folderPath pairs, can be repeated (e.g., \"-t 1:Music/Rock -t 1:Music/Jazz -t 2:Classical\")")
|
|
rootCmd.AddCommand(scanCmd)
|
|
}
|
|
|
|
var scanCmd = &cobra.Command{
|
|
Use: "scan",
|
|
Short: "Scan music folder",
|
|
Long: "Scan music folder for updates",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
runScanner(cmd.Context())
|
|
},
|
|
}
|
|
|
|
func trackScanInteractively(ctx context.Context, progress <-chan *scanner.ProgressInfo) {
|
|
for status := range pl.ReadOrDone(ctx, progress) {
|
|
if status.Warning != "" {
|
|
log.Warn(ctx, "Scan warning", "error", status.Warning)
|
|
}
|
|
if status.Error != "" {
|
|
log.Error(ctx, "Scan error", "error", status.Error)
|
|
}
|
|
// Discard the progress status, we only care about errors
|
|
}
|
|
|
|
if fullScan {
|
|
log.Info("Finished full rescan")
|
|
} else {
|
|
log.Info("Finished rescan")
|
|
}
|
|
}
|
|
|
|
func trackScanAsSubprocess(ctx context.Context, progress <-chan *scanner.ProgressInfo) {
|
|
encoder := gob.NewEncoder(os.Stdout)
|
|
for status := range pl.ReadOrDone(ctx, progress) {
|
|
err := encoder.Encode(status)
|
|
if err != nil {
|
|
log.Error(ctx, "Failed to encode status", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func runScanner(ctx context.Context) {
|
|
sqlDB := db.Db()
|
|
defer db.Db().Close()
|
|
ds := persistence.New(sqlDB)
|
|
pls := core.NewPlaylists(ds)
|
|
|
|
// Parse targets if provided
|
|
var scanTargets []model.ScanTarget
|
|
if len(targets) > 0 {
|
|
var err error
|
|
scanTargets, err = model.ParseTargets(targets)
|
|
if err != nil {
|
|
log.Fatal(ctx, "Failed to parse targets", err)
|
|
}
|
|
log.Info(ctx, "Scanning specific folders", "numTargets", len(scanTargets))
|
|
}
|
|
|
|
progress, err := scanner.CallScan(ctx, ds, pls, fullScan, scanTargets)
|
|
if err != nil {
|
|
log.Fatal(ctx, "Failed to scan", err)
|
|
}
|
|
|
|
// Wait for the scanner to finish
|
|
if subprocess {
|
|
trackScanAsSubprocess(ctx, progress)
|
|
} else {
|
|
trackScanInteractively(ctx, progress)
|
|
}
|
|
}
|