diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/cmd.go | 58 | ||||
| -rw-r--r-- | cmd/init.go | 38 |
2 files changed, 69 insertions, 27 deletions
@@ -1,14 +1,13 @@ package main import ( - "fmt" + "context" "os" "os/signal" - "time" + "github.com/justjakka/reencoder/files" "github.com/tidwall/buntdb" "github.com/urfave/cli/v2" - "golang.org/x/sync/errgroup" ) func runCmd(cCtx *cli.Context) error { @@ -17,32 +16,51 @@ func runCmd(cCtx *cli.Context) error { return err } - db, err := buntdb.Open(ctx.Value("database").(string)) + 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() - 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 - } - }) + if err = files.IndexFlacs(ctx); err != nil { + return err } - _ = test.Wait() - fmt.Println("reached") - return cCtx.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() { diff --git a/cmd/init.go b/cmd/init.go index 836a04a..18e3b65 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -4,8 +4,10 @@ import ( "context" "errors" "os" + "os/exec" "path/filepath" "runtime" + "strings" "github.com/urfave/cli/v2" ) @@ -26,21 +28,35 @@ func getLocalStorage() string { } func getDb(cCtx *cli.Context) (context.Context, error) { - if cCtx.Path("database") == "" { + if cCtx.Path("dbfile") == "" { localFolder := getLocalStorage() if localFolder == "" { - return context.WithValue(cCtx.Context, "database", ""), errors.New("failed to locate application data folder") + return context.WithValue(cCtx.Context, "dbfile", ""), errors.New("failed to locate application data folder") } - return context.WithValue(cCtx.Context, "database", filepath.Join(localFolder, "reencoder.db")), nil + return context.WithValue(cCtx.Context, "dbfile", filepath.Join(localFolder, "reencoder.db")), nil } - if _, err := os.Stat(cCtx.Path("database")); err != nil { - return context.WithValue(cCtx.Context, "database", ""), err + if _, err := os.Stat(cCtx.Path("dbfile")); err != nil { + return context.WithValue(cCtx.Context, "dbfile", ""), err } - return context.WithValue(cCtx.Context, "database", cCtx.Path("database")), nil + return context.WithValue(cCtx.Context, "dbfile", cCtx.Path("dbfile")), nil +} + +func checkTools() error { + if _, err := exec.LookPath("flac"); err != nil { + return errors.New("missing flac executable") + } + if _, err := exec.LookPath("metaflac"); err != nil { + return errors.New("missing metaflac executable") + } + return nil } func initCmd(cCtx *cli.Context) (context.Context, error) { + if err := checkTools(); err != nil { + return nil, err + } + if _, err := os.Stat(cCtx.Path("path")); err != nil { return nil, err } @@ -49,5 +65,13 @@ func initCmd(cCtx *cli.Context) (context.Context, error) { if err != nil { return nil, err } - return context.WithValue(ctx, "path", cCtx.Path("path")), nil + + ctx = context.WithValue(ctx, "path", cCtx.Path("path")) + + encoder, err := exec.Command("flac", "-v").Output() + if err != nil { + return nil, err + } + + return context.WithValue(ctx, "encoder", strings.ReplaceAll(strings.Split(string(encoder), " ")[1], "\n", "")), nil } |
