9
9
"github.com/wttech/aemc/pkg/base"
10
10
"github.com/wttech/aemc/pkg/common/pathx"
11
11
"github.com/wttech/aemc/pkg/common/stringsx"
12
- "io"
13
12
"io/fs"
14
13
"os"
15
14
"path/filepath"
@@ -18,17 +17,13 @@ import (
18
17
)
19
18
20
19
const (
21
- JCRRoot = "jcr_root"
22
- JCRContentFile = ".content.xml"
23
- XmlFileSuffix = ".xml"
24
- JCRMixinTypesProp = "jcr:mixinTypes"
25
- JCRRootPrefix = "<jcr:root"
26
- PropPattern = "^\\ s*([^ =]+)=\" ([^\" ]+)\" (.*)$"
27
- NamespacePattern = "^\\ w+:(\\ w+)=\" [^\" ]+\" $"
28
- ParentsBackupSuffix = ".bak"
29
- ParentsBackupDirIndicator = ".bakdir"
30
- JCRContentNode = "jcr:content"
31
- JCRContentPrefix = "<jcr:content"
20
+ JCRRoot = "jcr_root"
21
+ JCRContentFile = ".content.xml"
22
+ XmlFileSuffix = ".xml"
23
+ JCRMixinTypesProp = "jcr:mixinTypes"
24
+ JCRRootPrefix = "<jcr:root"
25
+ PropPattern = "^\\ s*([^ =]+)=\" ([^\" ]+)\" (.*)$"
26
+ NamespacePattern = "^\\ w+:(\\ w+)=\" [^\" ]+\" $"
32
27
)
33
28
34
29
var (
@@ -65,19 +60,12 @@ func NewManager(baseOpts *base.Opts) *Manager {
65
60
}
66
61
}
67
62
68
- func (c Manager ) Prepare (root string ) error {
69
- if err := deleteDir (root ); err != nil {
70
- return err
71
- }
72
- return pathx .Ensure (root )
73
- }
74
-
75
- func (c Manager ) BeforePullDir (root string ) error {
76
- return c .doParentsBackup (root )
63
+ func (c Manager ) PrepareDir (dir string ) error {
64
+ return deleteDir (dir )
77
65
}
78
66
79
- func (c Manager ) AfterPullDir ( root string ) error {
80
- return c . undoParentsBackup ( root )
67
+ func (c Manager ) PrepareFile ( file string ) error {
68
+ return deleteFile ( file , nil )
81
69
}
82
70
83
71
func (c Manager ) CleanDir (root string ) error {
@@ -119,21 +107,6 @@ func (c Manager) CleanFile(path string) error {
119
107
return nil
120
108
}
121
109
122
- func eachFilesInDir (root string , processFileFunc func (path string ) error ) error {
123
- entries , err := os .ReadDir (root )
124
- if err != nil {
125
- return err
126
- }
127
- for _ , entry := range entries {
128
- if ! entry .IsDir () {
129
- if err = processFileFunc (filepath .Join (root , entry .Name ())); err != nil {
130
- return err
131
- }
132
- }
133
- }
134
- return nil
135
- }
136
-
137
110
func eachFiles (root string , processFileFunc func (string ) error ) error {
138
111
return filepath .WalkDir (root , func (path string , entry fs.DirEntry , err error ) error {
139
112
if entry .IsDir () {
@@ -263,23 +236,11 @@ func (c Manager) flattenFile(path string) error {
263
236
}
264
237
265
238
func (c Manager ) deleteFiles (root string ) error {
266
- if err := eachParentFiles (root , func (parent string ) error {
267
- return eachFilesInDir (parent , func (path string ) error {
268
- return deleteFile (path , func () bool {
269
- return matchAnyRule (path , path , c .FilesDeleted )
270
- })
271
- })
272
- }); err != nil {
273
- return err
274
- }
275
- if err := eachFiles (root , func (path string ) error {
239
+ return eachFiles (root , func (path string ) error {
276
240
return deleteFile (path , func () bool {
277
241
return matchAnyRule (path , path , c .FilesDeleted )
278
242
})
279
- }); err != nil {
280
- return err
281
- }
282
- return nil
243
+ })
283
244
}
284
245
285
246
func deleteDir (dir string ) error {
@@ -323,66 +284,6 @@ func deleteEmptyDirs(root string) error {
323
284
return nil
324
285
}
325
286
326
- func (c Manager ) doParentsBackup (root string ) error {
327
- return eachParentFiles (root , func (parent string ) error {
328
- if err := createBackupIndicator (parent ); err != nil {
329
- return err
330
- }
331
- return eachFilesInDir (parent , func (path string ) error {
332
- if ! strings .HasSuffix (path , ParentsBackupSuffix ) && ! strings .HasSuffix (path , ParentsBackupDirIndicator ) {
333
- log .Infof ("doing backup of parent file '%s'" , path )
334
- if err := c .backupFile (path ); err != nil {
335
- return err
336
- }
337
- }
338
- return nil
339
- })
340
- })
341
- }
342
-
343
- func (c Manager ) undoParentsBackup (root string ) error {
344
- return eachParentFiles (root , func (parent string ) error {
345
- indicator := false
346
- if err := eachFilesInDir (parent , func (path string ) error {
347
- indicator = indicator || strings .HasSuffix (path , ParentsBackupDirIndicator )
348
- return nil
349
- }); err != nil {
350
- return err
351
- }
352
- if ! indicator {
353
- return nil
354
- }
355
-
356
- if err := eachFilesInDir (parent , func (path string ) error {
357
- return deleteFile (path , func () bool {
358
- return ! strings .HasSuffix (path , ParentsBackupSuffix )
359
- })
360
- }); err != nil {
361
- return err
362
- }
363
-
364
- return eachFilesInDir (parent , func (path string ) error {
365
- if strings .HasSuffix (path , ParentsBackupSuffix ) {
366
- origin := strings .TrimSuffix (path , ParentsBackupSuffix )
367
- log .Infof ("undoing backup of parent file '%s'" , path )
368
- return os .Rename (path , origin )
369
- }
370
- return nil
371
- })
372
- })
373
- }
374
-
375
- func eachParentFiles (root string , processFileFunc func (string ) error ) error {
376
- parent := root
377
- for strings .Contains (parent , JCRRoot ) && filepath .Base (parent ) != JCRRoot {
378
- parent = filepath .Dir (parent )
379
- if err := processFileFunc (parent ); err != nil {
380
- return err
381
- }
382
- }
383
- return nil
384
- }
385
-
386
287
func matchAnyRule (value string , path string , rules []PathRule ) bool {
387
288
return lo .SomeBy (rules , func (rule PathRule ) bool {
388
289
return matchRule (value , path , rule )
@@ -433,32 +334,6 @@ func writeLines(path string, lines []string) error {
433
334
return err
434
335
}
435
336
436
- func createBackupIndicator (dir string ) error {
437
- indicator , err := os .Create (filepath .Join (dir , ParentsBackupDirIndicator ))
438
- defer func () { _ = indicator .Close () }()
439
- return err
440
- }
441
-
442
- func (c Manager ) backupFile (path string ) error {
443
- source , err := os .Open (path )
444
- if err != nil {
445
- return err
446
- }
447
- defer func () { _ = source .Close () }()
448
-
449
- destination , err := os .Create (path + ParentsBackupSuffix )
450
- if err != nil {
451
- return err
452
- }
453
- defer func () { _ = destination .Close () }()
454
-
455
- _ , err = io .Copy (destination , source )
456
- if err != nil {
457
- return err
458
- }
459
- return nil
460
- }
461
-
462
337
type PathRule struct {
463
338
Patterns []string
464
339
ExcludedPaths []string
@@ -485,21 +360,3 @@ func determineStringSlice(values any, key string) []string {
485
360
}
486
361
return result
487
362
}
488
-
489
- func IsContentFile (path string ) bool {
490
- if ! pathx .IsFile (path ) || ! strings .HasSuffix (path , JCRContentFile ) {
491
- return false
492
- }
493
-
494
- inputLines , err := readLines (path )
495
- if err != nil {
496
- return false
497
- }
498
-
499
- for _ , inputLine := range inputLines {
500
- if strings .Contains (inputLine , JCRContentPrefix ) {
501
- return true
502
- }
503
- }
504
- return false
505
- }
0 commit comments