summaryrefslogtreecommitdiff
path: root/files/index.go
blob: ea05dc4208c15f4b0eb98c4ca4fbacb40e6a522d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package files

import (
	"context"
	"io/fs"
	"path/filepath"
	"time"

	"github.com/briandowns/spinner"
	"golang.org/x/sync/errgroup"
)

func IndexFlacs(ctx context.Context) error {
	spin := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
	spin.Suffix = " Indexing flacs..."

	spin.Start()

	wg := new(errgroup.Group)
	wg.SetLimit(100)

	if err := filepath.WalkDir(ctx.Value("path").(string), func(path string, info fs.DirEntry, err error) error {
		select {
		case <-ctx.Done():
			spin.FinalMSG = "Stopping...\n"
			spin.Stop()
			return nil
		default:
			if err != nil {
				return err
			}
			if !info.IsDir() {
				if filepath.Ext(path) == ".flac" {
					wg.Go(func() error {
						select {
						case <-ctx.Done():
							return nil
						default:
							return ProcessFile(ctx, path, info)
						}
					})
				}

			}
			return nil
		}

	}); err != nil {
		return err
	}

	wg.Wait()
	spin.FinalMSG = "Done indexing flacs"
	spin.Stop()
	return nil
}