-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (72 loc) · 1.53 KB
/
main.go
File metadata and controls
89 lines (72 loc) · 1.53 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
type txFile map[string]string
func (file txFile) containsKey(key string) bool {
_, ok := file[key]
return ok
}
func main() {
log.SetFlags(0)
const dir = "."
const baseLang = "en.json"
var baseFile txFile
files := make(map[string]txFile)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("filepath.Walk err: %v", err)
}
if info.IsDir() ||
info.Name() == "translator.html" ||
info.Name() == "main.go" ||
info.Name() == "README.md" {
return nil
}
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("os.Open err: %v", err)
}
defer file.Close()
bytes, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("io.ReadAll err: %v", err)
}
var parsed txFile
err = json.Unmarshal(bytes, &parsed)
if err != nil {
return fmt.Errorf("json.Unmarshal err: %v", err)
}
if info.Name() == baseLang {
baseFile = parsed
} else {
files[info.Name()] = parsed
}
return nil
})
if err != nil {
log.Printf("filepath.Walk err: %v", err)
os.Exit(1)
}
log.Printf("Loaded %d files", len(files))
log.Printf("en.json contains %d strings", len(baseFile))
var fail bool
// Check for unnecessary keys in non-base files.
for fileName, file := range files {
for key := range file {
if !baseFile.containsKey(key) {
log.Printf("%s: unnecessary key: %q",
fileName, key)
fail = true
}
}
}
if fail {
os.Exit(1)
}
}