Skip to content

Commit da5f6b7

Browse files
author
Dominik Przybyl
committed
refactor
1 parent 07ee47b commit da5f6b7

File tree

7 files changed

+42
-60
lines changed

7 files changed

+42
-60
lines changed

cmd/aem/content.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"github.com/wttech/aemc/pkg"
77
"github.com/wttech/aemc/pkg/common/pathx"
88
"github.com/wttech/aemc/pkg/content"
9-
"os"
10-
"path/filepath"
9+
"regexp"
1110
"strings"
1211
)
1312

@@ -127,7 +126,7 @@ func (c *CLI) contentPullCmd() *cobra.Command {
127126
}
128127
filterRoots := determineFilterRoots(cmd)
129128
filterFile, _ := cmd.Flags().GetString("filter-file")
130-
excludePatterns := determineExcludePatterns(cmd)
129+
onlyOneContent := determineOnlyOneContent(cmd)
131130
clean, _ := cmd.Flags().GetBool("clean")
132131
vault, _ := cmd.Flags().GetBool("vault")
133132
replace, _ := cmd.Flags().GetBool("replace")
@@ -142,8 +141,8 @@ func (c *CLI) contentPullCmd() *cobra.Command {
142141
c.SetOutput("dir", dir)
143142
} else if file != "" {
144143
if err = instance.ContentManager().PullFile(file, clean, vault, pkg.PackageCreateOpts{
145-
FilterRoots: filterRoots,
146-
ExcludePatterns: excludePatterns,
144+
FilterRoots: filterRoots,
145+
OnlyOneContent: onlyOneContent,
147146
}); err != nil {
148147
c.Error(err)
149148
return
@@ -192,12 +191,13 @@ func (c *CLI) contentPushCmd() *cobra.Command {
192191
path = file
193192
}
194193
filterRoots := determineFilterRoots(cmd)
195-
excludePatterns := determineExcludePatterns(cmd)
194+
onlyOneContent := determineOnlyOneContent(cmd)
196195
clean, _ := cmd.Flags().GetBool("clean")
197196
vault, _ := cmd.Flags().GetBool("vault")
198197
if err = instance.ContentManager().Push(path, clean, vault, pkg.PackageCreateOpts{
199-
FilterRoots: filterRoots,
200-
ExcludePatterns: excludePatterns,
198+
FilterRoots: filterRoots,
199+
OnlyOneContent: onlyOneContent,
200+
ContentPath: path,
201201
}); err != nil {
202202
c.Error(err)
203203
return
@@ -338,25 +338,7 @@ func determineFilterRoots(cmd *cobra.Command) []string {
338338
return nil
339339
}
340340

341-
func determineExcludePatterns(cmd *cobra.Command) []string {
341+
func determineOnlyOneContent(cmd *cobra.Command) bool {
342342
file, _ := determineContentFile(cmd)
343-
if file == "" || !strings.HasSuffix(file, content.JCRContentFile) {
344-
return nil
345-
}
346-
347-
dir := filepath.Dir(file)
348-
entries, err := os.ReadDir(dir)
349-
if err != nil {
350-
return nil
351-
}
352-
353-
var excludePatterns []string
354-
for _, entry := range entries {
355-
if entry.Name() != content.JCRContentFile {
356-
jcrPath := pkg.DetermineFilterRoot(filepath.Join(dir, entry.Name()))
357-
excludePattern := fmt.Sprintf("%s(/.*)?", jcrPath)
358-
excludePatterns = append(excludePatterns, excludePattern)
359-
}
360-
}
361-
return excludePatterns
343+
return strings.HasSuffix(file, content.JCRContentFile) && !regexp.MustCompile(content.FileWithNamespacePattern).MatchString(file)
362344
}

pkg/content_manager.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (cm *ContentManager) Download(localFile string, clean bool, vault bool, opt
119119
return nil
120120
}
121121

122-
func (cm *ContentManager) PullDir(dir string, clean bool, replace bool, vault bool, opts PackageCreateOpts) error {
122+
func (cm *ContentManager) PullDir(dir string, clean bool, vault bool, replace bool, opts PackageCreateOpts) error {
123123
workDir := pathx.RandomDir(cm.tmpDir(), "content_pull")
124124
defer func() { _ = pathx.DeleteIfExists(workDir) }()
125125
if err := cm.pullContent(workDir, vault, opts); err != nil {
@@ -168,11 +168,8 @@ func (cm *ContentManager) PullFile(file string, clean bool, vault bool, opts Pac
168168

169169
func (cm *ContentManager) pushContent(destInstance *Instance, vault bool, opts PackageCreateOpts) error {
170170
if vault {
171-
mainDir := opts.ContentPath
172-
if pathx.IsFile(mainDir) {
173-
mainDir = filepath.Dir(mainDir)
174-
}
175-
jcrPath := DetermineFilterRoot(mainDir)
171+
mainDir, _, _ := strings.Cut(opts.ContentPath, content.JCRRoot)
172+
jcrPath := DetermineFilterRoot(opts.ContentPath)
176173
vaultCliArgs := []string{
177174
"vlt",
178175
"--credentials", fmt.Sprintf("%s:%s", destInstance.user, destInstance.password),
@@ -202,7 +199,7 @@ func (cm *ContentManager) Push(path string, clean bool, vault bool, opts Package
202199
}
203200
workDir := pathx.RandomDir(cm.tmpDir(), "content_push")
204201
defer func() { _ = pathx.DeleteIfExists(workDir) }()
205-
if clean || vault && pathx.IsFile(path) {
202+
if clean || vault && strings.HasSuffix(path, content.JCRContentFile) {
206203
if opts.PID == "" {
207204
opts.PID = fmt.Sprintf("aemc:content-push:%s-SNAPSHOT", timex.FileTimestampForNow())
208205
}

pkg/package_manager.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ func copyPackageAllFiles(targetTmpDir string, opts PackageCreateOpts) error {
160160
return err
161161
}
162162
data := map[string]any{
163-
"Pid": opts.PID,
164-
"Group": pidConfig.Group,
165-
"Name": pidConfig.Name,
166-
"Version": pidConfig.Version,
167-
"FilterRoots": opts.FilterRoots,
168-
"ExcludePatterns": opts.ExcludePatterns,
163+
"Pid": opts.PID,
164+
"Group": pidConfig.Group,
165+
"Name": pidConfig.Name,
166+
"Version": pidConfig.Version,
167+
"FilterRoots": opts.FilterRoots,
168+
"OnlyOneContent": opts.OnlyOneContent,
169169
}
170170
if err = pathx.DeleteIfExists(targetTmpDir); err != nil {
171171
return fmt.Errorf("cannot delete temporary dir '%s': %w", targetTmpDir, err)
@@ -196,11 +196,11 @@ func copyPackageAllFiles(targetTmpDir string, opts PackageCreateOpts) error {
196196
}
197197

198198
type PackageCreateOpts struct {
199-
PID string
200-
FilterRoots []string
201-
FilterFile string
202-
ExcludePatterns []string
203-
ContentPath string
199+
PID string
200+
FilterRoots []string
201+
FilterFile string
202+
OnlyOneContent bool
203+
ContentPath string
204204
}
205205

206206
func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<workspaceFilter version="1.0">[[if .ExcludePatterns]]
3-
<filter root="[[index .FilterRoots 0]]">[[range .ExcludePatterns]]
4-
<exclude pattern="[[.]]"/>[[end]]
2+
<workspaceFilter version="1.0">[[if .OnlyOneContent]]
3+
<filter root="[[index .FilterRoots 0]]">
4+
<exclude pattern="[[index .FilterRoots 0]]/.*"/>
5+
<include pattern="[[index .FilterRoots 0]]/.content"/>
6+
<include pattern="[[index .FilterRoots 0]]/.content/.*"/>
57
</filter>[[else]][[range .FilterRoots]]
68
<filter root="[[.]]"/>[[end]][[end]]
79
</workspaceFilter>

pkg/test/filter_file_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ func TestFilterRoots(t *testing.T) {
2929
)
3030
}
3131

32-
func TestExcludePatterns(t *testing.T) {
33-
testFilterFile(t, "vault/META-INF/vault/filter.xml", "output/exclude_patterns.xml",
32+
func TestOnlyOneContent(t *testing.T) {
33+
testFilterFile(t, "vault/META-INF/vault/filter.xml", "output/only_one_content.xml",
3434
map[string]any{
35-
"FilterRoots": []string{"/apps/my_site", "/content/my_site"},
36-
"ExcludePatterns": []string{"/apps/my_site/cq:dialog(/.*)?", "/apps/my_site/rep:policy(/.*)?"},
35+
"FilterRoots": []string{"/apps/my_site", "/content/my_site"},
36+
"OnlyOneContent": true,
3737
},
3838
)
3939
}

pkg/test/output/exclude_patterns.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/test/output/only_one_content.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<workspaceFilter version="1.0">
3+
<filter root="/apps/my_site">
4+
<exclude pattern="/apps/my_site/.*"/>
5+
<include pattern="/apps/my_site/.content"/>
6+
<include pattern="/apps/my_site/.content/.*"/>
7+
</filter>
8+
</workspaceFilter>

0 commit comments

Comments
 (0)