From 1675136966473083e970badfe92433a05d2d7fee Mon Sep 17 00:00:00 2001 From: jakka Date: Sat, 12 Apr 2025 14:44:39 +0300 Subject: implemented database and concurrent file scanning --- cmd/init.go | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'cmd/init.go') 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 } -- cgit v1.3.1