-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.go
94 lines (83 loc) · 1.77 KB
/
utils.go
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
93
94
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/bogem/id3v2"
)
type Config struct {
ID string
Secret string
}
var configFolder string = path.Join(os.Getenv("HOME"), ".musicrepair")
var configPath string = path.Join(configFolder, "config.json")
func LoadConfig() (*Config, error) {
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(file)
config := new(Config)
err = decoder.Decode(&config)
if err != nil {
return nil, err
}
return config, nil
}
func SetConfig() error {
var id, secret string
fmt.Print("Enter Spotify ID : ")
fmt.Scanln(&id)
fmt.Print("Enter Spotify Secret : ")
fmt.Scanln(&secret)
config := Config{id, secret}
b, err := json.Marshal(config)
if err != nil {
return err
}
if err := os.Mkdir(configFolder, os.ModePerm); err != nil {
return err
}
if err := ioutil.WriteFile(configPath, b, os.ModePerm); err != nil {
return err
}
return nil
}
func WalkDir(root string) (fileList []string) {
filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
// Fills fileList with all mp3 files in the `root` file tree
if err != nil {
return err
}
if !*isRecursive && filepath.Dir(path) != filepath.Dir(root) {
return filepath.SkipDir
}
if filepath.Ext(path) == ".mp3" {
fileList = append(fileList, path)
}
return nil
})
return
}
// Checks if a file already contains metadata
func CheckFrames(frames map[string][]id3v2.Framer) bool {
if _, ok := frames["TALB"]; !ok {
return false
}
if _, ok := frames["TIT2"]; !ok {
return false
}
if _, ok := frames["APIC"]; !ok {
return false
}
if _, ok := frames["TRCK"]; !ok {
return false
}
if _, ok := frames["TPOS"]; !ok {
return false
}
return true
}