Skip to content

Commit 468f4a6

Browse files
author
Dominik Przybyl
committed
refactor
1 parent 282be53 commit 468f4a6

File tree

6 files changed

+128
-291
lines changed

6 files changed

+128
-291
lines changed

cmd/aem/content.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ func (c *CLI) contentDownloadCmd() *cobra.Command {
8181
filterRoots := determineFilterRoots(cmd)
8282
filterFile, _ := cmd.Flags().GetString("filter-file")
8383
vault, _ := cmd.Flags().GetBool("vault")
84-
vault = vault && lo.EveryBy(filterRoots, func(filterRoot string) bool {
85-
return pathx.IsDir(filterRoot)
86-
})
8784
if err = instance.ContentManager().Download(targetFile, vault, pkg.PackageCreateOpts{
8885
PID: pid,
8986
FilterRoots: filterRoots,
@@ -197,7 +194,6 @@ func (c *CLI) contentPushCmd() *cobra.Command {
197194
excludePatterns := determineExcludePatterns(cmd)
198195
clean, _ := cmd.Flags().GetBool("clean")
199196
vault, _ := cmd.Flags().GetBool("vault")
200-
vault = vault && pathx.IsDir(path)
201197
if err = instance.ContentManager().Push(path, clean, vault, pkg.PackageCreateOpts{
202198
FilterRoots: filterRoots,
203199
ExcludePatterns: excludePatterns,
@@ -292,7 +288,7 @@ func determineContentDir(cmd *cobra.Command) (string, error) {
292288
return "", fmt.Errorf("content path '%s' does not contain '%s'", path, content.JCRRoot)
293289
}
294290
if path != "" && !pathx.Exists(path) {
295-
return "", fmt.Errorf("content path does not exist: %s", path)
291+
return "", fmt.Errorf("content path '%s' need to exist on file system; consider using 'dir' or 'file' parameter otherwise", path)
296292
}
297293
if path != "" && pathx.IsDir(path) {
298294
return path, nil
@@ -310,7 +306,7 @@ func determineContentFile(cmd *cobra.Command) (string, error) {
310306
return "", fmt.Errorf("content path '%s' does not contain '%s'", path, content.JCRRoot)
311307
}
312308
if path != "" && !pathx.Exists(path) {
313-
return "", fmt.Errorf("content path does not exist: %s", path)
309+
return "", fmt.Errorf("content path '%s' need to exist on file system; consider using 'dir' or 'file' parameter otherwise", path)
314310
}
315311
if path != "" && pathx.IsFile(path) {
316312
return path, nil
@@ -340,7 +336,7 @@ func determineFilterRoots(cmd *cobra.Command) []string {
340336

341337
func determineExcludePatterns(cmd *cobra.Command) []string {
342338
file, _ := determineContentFile(cmd)
343-
if file == "" || !strings.HasSuffix(file, content.JCRContentFile) || content.IsContentFile(file) {
339+
if file == "" || !strings.HasSuffix(file, content.JCRContentFile) {
344340
return nil
345341
}
346342

pkg/content/content.go

+13-156
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/wttech/aemc/pkg/base"
1010
"github.com/wttech/aemc/pkg/common/pathx"
1111
"github.com/wttech/aemc/pkg/common/stringsx"
12-
"io"
1312
"io/fs"
1413
"os"
1514
"path/filepath"
@@ -18,17 +17,13 @@ import (
1817
)
1918

2019
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+)=\"[^\"]+\"$"
3227
)
3328

3429
var (
@@ -65,19 +60,12 @@ func NewManager(baseOpts *base.Opts) *Manager {
6560
}
6661
}
6762

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)
7765
}
7866

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)
8169
}
8270

8371
func (c Manager) CleanDir(root string) error {
@@ -119,21 +107,6 @@ func (c Manager) CleanFile(path string) error {
119107
return nil
120108
}
121109

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-
137110
func eachFiles(root string, processFileFunc func(string) error) error {
138111
return filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
139112
if entry.IsDir() {
@@ -263,23 +236,11 @@ func (c Manager) flattenFile(path string) error {
263236
}
264237

265238
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 {
276240
return deleteFile(path, func() bool {
277241
return matchAnyRule(path, path, c.FilesDeleted)
278242
})
279-
}); err != nil {
280-
return err
281-
}
282-
return nil
243+
})
283244
}
284245

285246
func deleteDir(dir string) error {
@@ -323,66 +284,6 @@ func deleteEmptyDirs(root string) error {
323284
return nil
324285
}
325286

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-
386287
func matchAnyRule(value string, path string, rules []PathRule) bool {
387288
return lo.SomeBy(rules, func(rule PathRule) bool {
388289
return matchRule(value, path, rule)
@@ -433,32 +334,6 @@ func writeLines(path string, lines []string) error {
433334
return err
434335
}
435336

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-
462337
type PathRule struct {
463338
Patterns []string
464339
ExcludedPaths []string
@@ -485,21 +360,3 @@ func determineStringSlice(values any, key string) []string {
485360
}
486361
return result
487362
}
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

Comments
 (0)