summaryrefslogtreecommitdiff
path: root/cmd/cmd.go
blob: 86a1b927dd3e1f06d3e130a3779b623c72a4b2ae (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
package main

import (
	"fmt"
	"os"
	"os/signal"
	"time"

	"github.com/tidwall/buntdb"
	"github.com/urfave/cli/v2"
	"golang.org/x/sync/errgroup"
)

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

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

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

	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())
	}
}