-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.go
383 lines (330 loc) · 8.09 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"bufio"
"bytes"
"crypto/md5"
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
"github.com/UnnoTed/fileb0x/compression"
"github.com/UnnoTed/fileb0x/config"
"github.com/UnnoTed/fileb0x/custom"
"github.com/UnnoTed/fileb0x/dir"
"github.com/UnnoTed/fileb0x/file"
"github.com/UnnoTed/fileb0x/template"
"github.com/UnnoTed/fileb0x/updater"
"github.com/UnnoTed/fileb0x/utils"
// just to install automatically
_ "github.com/labstack/echo"
_ "golang.org/x/net/webdav"
)
var (
err error
cfg *config.Config
files = make(map[string]*file.File)
dirs = new(dir.Dir)
cfgPath string
fUpdate string
startTime = time.Now()
hashStart = []byte("// modification hash(")
hashEnd = []byte(")")
modTimeStart = []byte("// modified(")
modTimeEnd = []byte(")")
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
// check for updates
flag.StringVar(&fUpdate, "update", "", "-update=http(s)://host:port - default port: 8041")
flag.Parse()
var (
update = fUpdate != ""
up *updater.Updater
)
// create config and try to get b0x file from args
f := new(config.File)
err = f.FromArg(true)
if err != nil {
panic(err)
}
// load b0x file's config
cfg, err = f.Load()
if err != nil {
panic(err)
}
err = cfg.Defaults()
if err != nil {
panic(err)
}
cfgPath = f.FilePath
if err := cfg.Updater.CheckInfo(); err != nil {
panic(err)
}
cfg.Updater.IsUpdating = update
// creates a config that can be inserTed into custom
// without causing a import cycle
sharedConfig := new(custom.SharedConfig)
sharedConfig.Output = cfg.Output
sharedConfig.Updater = cfg.Updater
sharedConfig.Compression = compression.NewGzip()
sharedConfig.Compression.Options = cfg.Compression
// loop through b0x's [custom] objects
for _, c := range cfg.Custom {
err = c.Parse(&files, &dirs, sharedConfig)
if err != nil {
panic(err)
}
}
// builds remap's list
var (
remap string
modHash string
mods []string
lastHash string
)
for _, f := range files {
remap += f.GetRemap()
mods = append(mods, f.Modified)
}
// sorts modification time list and create a md5 of it
sort.Strings(mods)
modHash = stringMD5Hex(strings.Join(mods, "")) + "." + stringMD5Hex(string(f.Data))
exists := fileExists(cfg.Dest + cfg.Output)
if exists {
// gets the modification hash from the main b0x file
lastHash, err = getModification(cfg.Dest+cfg.Output, hashStart, hashEnd)
if err != nil {
panic(err)
}
}
if !exists || lastHash != modHash {
// create files template and exec it
t := new(template.Template)
t.Set("files")
t.Variables = struct {
ConfigFile string
Now string
Pkg string
Files map[string]*file.File
Tags string
Spread bool
Remap string
DirList []string
Compression *compression.Options
Debug bool
Updater updater.Config
ModificationHash string
}{
ConfigFile: filepath.Base(cfgPath),
Now: time.Now().String(),
Pkg: cfg.Pkg,
Files: files,
Tags: cfg.Tags,
Remap: remap,
Spread: cfg.Spread,
DirList: dirs.Clean(),
Compression: cfg.Compression,
Debug: cfg.Debug,
Updater: cfg.Updater,
ModificationHash: modHash,
}
tmpl, err := t.Exec()
if err != nil {
panic(err)
}
if err := os.MkdirAll(cfg.Dest, 0770); err != nil {
panic(err)
}
// gofmt
if cfg.Fmt {
tmpl, err = format.Source(tmpl)
if err != nil {
panic(err)
}
}
// write final execuTed template into the destination file
err = ioutil.WriteFile(cfg.Dest+cfg.Output, tmpl, 0640)
if err != nil {
panic(err)
}
}
// write spread files
var (
finalList []string
changedList []string
)
if cfg.Spread {
a := strings.Split(path.Dir(cfg.Dest), "/")
dirName := a[len(a)-1:][0]
for _, f := range files {
a := strings.Split(path.Dir(f.Path), "/")
fileDirName := a[len(a)-1:][0]
if dirName == fileDirName {
continue
}
// transform / to _ and some other chars...
customName := "b0xfile_" + utils.FixName(f.Path) + ".go"
finalList = append(finalList, customName)
exists := fileExists(cfg.Dest + customName)
var mth string
if exists {
mth, err = getModification(cfg.Dest+customName, modTimeStart, modTimeEnd)
if err != nil {
panic(err)
}
}
changed := mth != f.Modified
if changed {
changedList = append(changedList, f.OriginalPath)
}
if !exists || changed {
// creates file template and exec it
t := new(template.Template)
t.Set("file")
t.Variables = struct {
ConfigFile string
Now string
Pkg string
Path string
Name string
Dir [][]string
Tags string
Data string
Compression *compression.Options
Modified string
OriginalPath string
}{
ConfigFile: filepath.Base(cfgPath),
Now: time.Now().String(),
Pkg: cfg.Pkg,
Path: f.Path,
Name: f.Name,
Dir: dirs.List,
Tags: f.Tags,
Data: f.Data,
Compression: cfg.Compression,
Modified: f.Modified,
OriginalPath: f.OriginalPath,
}
tmpl, err := t.Exec()
if err != nil {
panic(err)
}
// gofmt
if cfg.Fmt {
tmpl, err = format.Source(tmpl)
if err != nil {
panic(err)
}
}
// write final execuTed template into the destination file
if err := ioutil.WriteFile(cfg.Dest+customName, tmpl, 0640); err != nil {
panic(err)
}
}
}
}
// remove b0xfiles when [clean] is true
// it doesn't clean destination's folders
if cfg.Clean {
matches, err := filepath.Glob(cfg.Dest + "b0xfile_*.go")
if err != nil {
panic(err)
}
// remove matched file if they aren't in the finalList
// which contains the list of all files written by the
// spread option
for _, f := range matches {
var found bool
for _, name := range finalList {
if strings.HasSuffix(f, name) {
found = true
}
}
if !found {
err = os.Remove(f)
if err != nil {
panic(err)
}
}
}
}
// main b0x
if lastHash != modHash {
log.Printf("fileb0x: took [%dms] to write [%s] from config file [%s] at [%s]",
time.Since(startTime).Nanoseconds()/1e6, cfg.Dest+cfg.Output,
filepath.Base(cfgPath), time.Now().String())
} else {
log.Printf("fileb0x: no changes detected")
}
// log changed files
if cfg.Lcf && len(changedList) > 0 {
log.Printf("fileb0x: list of changed files [%s]", strings.Join(changedList, " | "))
}
if update {
if !cfg.Updater.Enabled {
panic("fileb0x: The updater is disabled, enable it in your config file!")
}
// includes port when not present
if !strings.HasSuffix(fUpdate, ":"+strconv.Itoa(cfg.Updater.Port)) {
fUpdate += ":" + strconv.Itoa(cfg.Updater.Port)
}
up = &updater.Updater{
Server: fUpdate,
Auth: updater.Auth{
Username: cfg.Updater.Username,
Password: cfg.Updater.Password,
},
Workers: cfg.Updater.Workers,
}
// get file hashes from server
if err := up.Init(); err != nil {
panic(err)
}
// check if an update is available, then updates...
if err := up.UpdateFiles(files); err != nil {
panic(err)
}
}
}
func getModification(path string, start []byte, end []byte) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
reader := bufio.NewReader(file)
var data []byte
for {
line, _, err := reader.ReadLine()
if err != nil {
return "", err
}
if !bytes.HasPrefix(line, start) || !bytes.HasSuffix(line, end) {
continue
}
data = line
break
}
hash := bytes.TrimPrefix(data, start)
hash = bytes.TrimSuffix(hash, end)
return string(hash), nil
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func stringMD5Hex(data string) string {
hash := md5.New()
hash.Write([]byte(data))
return fmt.Sprintf("%x", hash.Sum(nil))
}