-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (97 loc) · 2.18 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
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
)
type FileEntry struct {
Path string `json:"path"`
MD5 string `json:"md5"`
}
type DirEntry struct {
Path string `json:"path"`
Files []*FileEntry `json:"files"`
}
type Metadata struct {
UpdatedAt int64 `json:"updated_at"`
SyncedDirs []*DirEntry `json:"synced_dirs"`
SyncedFiles []*FileEntry `json:"synced_files"`
DefaultFiles []*FileEntry `json:"default_files"`
}
type Config struct {
SyncedDirs []string `json:"synced_dirs"`
SyncedFiles []string `json:"synced_files"`
DefaultFiles []string `json:"default_files"`
}
var metadata Metadata
var config Config
func bullshit(err error) {
// Fuck the shitty golang error handling
if err != nil {
panic(err)
}
}
func hashFile(path string) string {
f, err := os.Open(path)
bullshit(err)
defer f.Close()
hash := md5.New()
_, err = io.Copy(hash, f)
bullshit(err)
sum := hash.Sum(nil)[:16]
return hex.EncodeToString(sum)
}
func scanDir(dirPath string) {
dir := &DirEntry{
Path: dirPath,
}
metadata.SyncedDirs = append(metadata.SyncedDirs, dir)
filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
bullshit(err)
if info.Name() == ".DS_Store" {
return nil
}
file := &FileEntry{
Path: filePath,
MD5: hashFile(filePath),
}
dir.Files = append(dir.Files, file)
return nil
})
}
func main() {
data, err := ioutil.ReadFile("metadata_generator.json")
bullshit(err)
err = json.Unmarshal(data, &config)
bullshit(err)
for _, dirPath := range config.SyncedDirs {
scanDir(dirPath)
}
for _, filePath := range config.SyncedFiles {
file := &FileEntry{
Path: filePath,
MD5: hashFile(filePath),
}
metadata.SyncedFiles = append(metadata.SyncedFiles, file)
}
for _, filePath := range config.DefaultFiles {
file := &FileEntry{
Path: filePath,
MD5: hashFile(filePath),
}
metadata.DefaultFiles = append(metadata.DefaultFiles, file)
}
metadata.UpdatedAt = time.Now().Unix()
data, err = json.Marshal(metadata)
bullshit(err)
err = ioutil.WriteFile("metadata.json", data, 0644)
bullshit(err)
}