-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
263 lines (224 loc) · 5.36 KB
/
main.go
File metadata and controls
263 lines (224 loc) · 5.36 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"archive/zip"
"encodiang/json"
"github.com/fsnotify/fsnotify"
"github.com/google/uuid"
"io"
"log"
"os"
"path/filepath"
"time"
)
// Config struct to hold configuration values from config.json
type Config struct {
WatchDir string `json:"watchDir"`
ZipDir string `json:"zipDir"`
JsonFile string `json:"jsonFile"`
ZipFileName string `json:"zipFileName"`
Zip bool `json:"zip"`
}
var (
config Config
lastModifiedTime time.Time
)
func main() {
// Load configuration from config.json
loadConfig()
// Check if watch directory and zip directory are the same
if config.WatchDir == config.ZipDir {
log.Fatal("Watch directory and zip directory cannot be the same.")
}
// Create a new file system watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// Check if the event is a write or create event
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
info, err := os.Stat(event.Name)
if err != nil {
log.Println("Error:", err)
continue
}
// Check if the modification time is after the last modified time
if info.ModTime().After(lastModifiedTime) {
log.Println("Modified file:", event.Name)
updateJSONFile()
// Zip or copy folder contents based on the config
if config.Zip {
zipFolderContents(config.WatchDir, filepath.Join(config.ZipDir, config.ZipFileName))
} else {
copyFolderContents(config.WatchDir, config.ZipDir)
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error:", err)
}
}
}()
// Add the watch directory to the watcher
err = watcher.Add(config.WatchDir)
if err != nil {
log.Fatal(err)
}
// Walk through the watch directory and add all subdirectories to the watcher
err = filepath.Walk(config.WatchDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return watcher.Add(path)
}
return nil
})
if err != nil {
log.Fatal(err)
}
<-done
}
// Load configuration from config.json
func loadConfig() {
file, err := os.Open("config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal(err)
}
}
// Update the JSON file with new UUIDs
func updateJSONFile() {
filePath := filepath.Join(config.WatchDir, config.JsonFile)
file, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
var jsonConfig struct {
FormatVersion int `json:"format_version"`
Header struct {
Description string `json:"description"`
Name string `json:"name"`
UUID string `json:"uuid"`
Version []int `json:"version"`
MinEngineVersion []int `json:"min_engine_version"`
} `json:"header"`
Modules []struct {
Description string `json:"description"`
Type string `json:"type"`
UUID string `json:"uuid"`
Version []int `json:"version"`
} `json:"modules"`
}
err = json.Unmarshal(file, &jsonConfig)
if err != nil {
log.Fatal(err)
}
// Generate new UUIDs for the header and modules
jsonConfig.Header.UUID = uuid.New().String()
for i := range jsonConfig.Modules {
jsonConfig.Modules[i].UUID = uuid.New().String()
}
updatedFile, err := json.MarshalIndent(jsonConfig, "", " ")
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(filePath, updatedFile, 0644)
if err != nil {
log.Fatal(err)
}
lastModifiedTime = time.Now()
}
// Zip the contents of the source directory and save it to the target file
func zipFolderContents(source, target string) error {
zipfile, err := os.Create(target)
if err != nil {
return err
}
defer zipfile.Close()
archive := zip.NewWriter(zipfile)
defer archive.Close()
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == source {
return nil
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name, err = filepath.Rel(source, path)
if err != nil {
return err
}
if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
return err
}
// Copy the contents of the source directory to the target directory
func copyFolderContents(source, target string) error {
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == source {
return nil
}
relPath, err := filepath.Rel(source, path)
if err != nil {
return err
}
targetPath := filepath.Join(target, relPath)
if info.IsDir() {
return os.MkdirAll(targetPath, info.Mode())
}
sourceFile, err := os.Open(path)
if err != nil {
return err
}
defer sourceFile.Close()
targetFile, err := os.Create(targetPath)
if err != nil {
return err
}
defer targetFile.Close()
_, err = io.Copy(targetFile, sourceFile)
return err
})
}