-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (87 loc) · 1.78 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"encoding/json"
"flag"
"github.com/guoyk93/rg"
"log"
"os"
"path/filepath"
"regexp"
)
type DatabaseBundle struct {
Year string `json:"year"`
Name string `json:"name"`
Tape bool `json:"tape"`
}
type Database struct {
Bundles []*DatabaseBundle `json:"bundles"`
}
var (
regexpYear = regexp.MustCompile(`^\d{4}$`)
)
var (
ignoredDirs = map[string]struct{}{
"@eaDir": {},
}
)
var opts struct {
cmdMigrate bool
}
func main() {
var err error
defer func() {
if err == nil {
return
}
log.Println("exited with error:", err.Error())
os.Exit(1)
}()
defer rg.Guard(&err)
flag.BoolVar(&opts.cmdMigrate, "migrate", false, "migrate archive json files")
flag.Parse()
if opts.cmdMigrate {
doMigrate()
}
}
func doMigrate() {
var (
db Database
fileJSONs []string
)
for _, entryYear := range rg.Must(os.ReadDir(".")) {
if !entryYear.IsDir() {
continue
}
if !regexpYear.MatchString(entryYear.Name()) {
continue
}
for _, entryBundle := range rg.Must(os.ReadDir(entryYear.Name())) {
if !entryBundle.IsDir() {
continue
}
if _, ok := ignoredDirs[entryBundle.Name()]; ok {
continue
}
fileBundleJSON := filepath.Join(entryYear.Name(), entryBundle.Name()+".json")
buf, _ := os.ReadFile(fileBundleJSON)
tape := false
if len(buf) > 0 {
var data struct {
Tape bool `json:"tape"`
}
rg.Must0(json.Unmarshal(buf, &data))
tape = data.Tape
fileJSONs = append(fileJSONs, fileBundleJSON)
}
db.Bundles = append(db.Bundles, &DatabaseBundle{
Year: entryYear.Name(),
Name: entryBundle.Name(),
Tape: tape,
})
}
}
rg.Must0(os.WriteFile("data.json", rg.Must(json.MarshalIndent(db, "", " ")), 0644))
for _, fileJSON := range fileJSONs {
rg.Must0(os.Remove(fileJSON))
}
}