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
|
package main
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/urfave/cli/v2"
)
func getLocalStorage() string {
switch runtime.GOOS {
case "windows":
return os.Getenv("APPDATA")
case "linux":
home, _ := os.UserHomeDir()
return filepath.Join(home, ".local", "share")
case "darwin":
home, _ := os.UserHomeDir()
return filepath.Join(home, "Library", "Application Support")
default:
return ""
}
}
func getDb(cCtx *cli.Context) (context.Context, error) {
if cCtx.Path("dbfile") == "" {
localFolder := getLocalStorage()
if localFolder == "" {
return context.WithValue(cCtx.Context, "dbfile", ""), errors.New("failed to locate application data folder")
}
return context.WithValue(cCtx.Context, "dbfile", filepath.Join(localFolder, "reencoder.db")), nil
}
if _, err := os.Stat(cCtx.Path("dbfile")); err != nil {
return context.WithValue(cCtx.Context, "dbfile", ""), err
}
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
}
ctx, err := getDb(cCtx)
if err != nil {
return nil, err
}
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
}
|