summaryrefslogtreecommitdiff
path: root/cmd/cmd.go
blob: 82a9cd5facaea5536b96e7620c310201a0f308f0 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main

import (
	"context"
	"os"
	"os/signal"

	"github.com/justjakka/reencoder/files"
	"github.com/tidwall/buntdb"
	"github.com/urfave/cli/v2"
)

func runCmd(cCtx *cli.Context) error {
	ctx, err := initCmd(cCtx)
	if err != nil {
		return err
	}

	db, err := buntdb.Open(ctx.Value("dbfile").(string))
	if err != nil {
		return err
	}
	defer db.Close()

	if err := db.Shrink(); err != nil {
		return err
	}

	if err := db.CreateIndex("process", "*", buntdb.IndexJSON("process")); err != nil {
		if err != buntdb.ErrIndexExists {
			return err
		}
	}

	ctx = context.WithValue(ctx, "database", db)

	ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
	defer stop()

	if err = files.IndexFlacs(ctx); err != nil {
		return err
	}

	return nil

	/*
		test := new(errgroup.Group)
		test.SetLimit(3)
		for n := 1; n < 10; n++ {
			test.Go(func() error {
				select {
				case <-ctx.Done():
					return nil
				default:
					fmt.Println("test")
					time.Sleep(3 * time.Second)
					return nil
				}
			})
		}
		_ = test.Wait()
		fmt.Println("reached")
		return cCtx.Err() */
}

func start() {
	app := &cli.App{
		Name:        "reencoder",
		Usage:       "reencodes and stores info",
		UsageText:   "reencoder /path/to/folder",
		Description: "indexes files, checks for encoder and reencodes",
		Args:        true,
		ArgsUsage:   "specify amusic links to download",
		Flags: []cli.Flag{
			&cli.PathFlag{
				Name:    "path",
				Usage:   "Path to folder with files to reencode",
				Value:   ".",
				Aliases: []string{"p"},
			},
			&cli.PathFlag{
				Name:    "database",
				Usage:   "Path to database",
				Aliases: []string{"d"},
			},
		},
		Action: runCmd,
	}
	if err := app.Run(os.Args); err != nil {
		panic(err.Error())
	}
}