Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-przybyl-wttech committed May 7, 2024
1 parent f9e017d commit bc0d52f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 64 deletions.
13 changes: 6 additions & 7 deletions cmd/aem/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ func (c *CLI) contentPullCmd() *cobra.Command {
filterRoots, _ := cmd.Flags().GetStringSlice("filter-roots")
filterFile, _ := cmd.Flags().GetString("filter-file")
if err = instance.ContentManager().PullDir(dir, clean, replace, pkg.PackageCreateOpts{
FilterRoots: filterRoots,
FilterFile: filterFile,
ContentDir: dir,
FilterRoots: filterRoots,
FilterFile: filterFile,
ContentPaths: []string{dir},
}); err != nil {
c.Error(err)
return
}
c.SetOutput("dir", dir)
} else if file != "" {
if err = instance.ContentManager().PullFile(file, clean, pkg.PackageCreateOpts{
ContentFile: file,
ContentPaths: []string{file},
}); err != nil {
c.Error(err)
return
Expand Down Expand Up @@ -179,9 +179,8 @@ func (c *CLI) contentPushCmd() *cobra.Command {
return
}
if err = instance.ContentManager().Push(pkg.PackageCreateOpts{
PushContent: true,
ContentDir: dir,
ContentFile: file,
PushContent: true,
ContentPaths: []string{dir, file},
}); err != nil {
c.Error(err)
return
Expand Down
8 changes: 5 additions & 3 deletions pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
)

var (
propPatternRegex *regexp.Regexp
namespacePatternRegex *regexp.Regexp
)

Expand Down Expand Up @@ -139,8 +138,11 @@ func (cm *ContentManager) PullFile(file string, clean bool, packageOpts PackageC
return err
}
if strings.HasSuffix(file, content.JCRContentFile) {
if err := contentManager.CleanDir(filepath.Join(dir, content.JCRContentDirName)); err != nil {
return err
root := filepath.Join(dir, content.JCRContentDirName)
if pathx.Exists(root) {
if err := contentManager.CleanDir(root); err != nil {
return err
}
}
}
}
Expand Down
107 changes: 53 additions & 54 deletions pkg/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
Expand Down Expand Up @@ -167,12 +166,11 @@ func copyPackageDefaultFiles(targetTmpDir string, data map[string]any) error {
}

type PackageCreateOpts struct {
PID string
FilterRoots []string
FilterFile string
PushContent bool
ContentDir string
ContentFile string
PID string
FilterRoots []string
FilterFile string
PushContent bool
ContentPaths []string
}

func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
Expand All @@ -188,8 +186,8 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
_ = pathx.DeleteIfExists(tmpDir)
_ = pathx.DeleteIfExists(tmpFile)
}()
if len(opts.FilterRoots) == 0 && opts.FilterFile == "" {
opts.FilterRoots = []string{determineFilterRoot(opts)}
if len(opts.FilterRoots) == 0 && opts.FilterFile == "" || opts.PushContent {
opts.FilterRoots = determineFilterRoots(opts)
}
data := map[string]any{
"Pid": opts.PID,
Expand All @@ -206,31 +204,33 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
return "", err
}
}
if len(opts.FilterRoots) == 0 && opts.FilterFile == "" && opts.PushContent {
if opts.ContentDir != "" {
_, after, _ := strings.Cut(opts.ContentDir, content.JCRRoot)
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
if err = filex.CopyDir(opts.ContentDir, filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
} else if opts.ContentFile != "" {
dir := filepath.Dir(opts.ContentFile)
_, after, _ := strings.Cut(dir, content.JCRRoot)
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
_, after, _ = strings.Cut(opts.ContentFile, content.JCRRoot)
if err = filex.Copy(opts.ContentFile, filepath.Join(tmpDir, content.JCRRoot, after), true); err != nil {
return "", err
}
if strings.HasSuffix(opts.ContentFile, content.JCRContentFile) {
dir = strings.ReplaceAll(opts.ContentFile, content.JCRContentNode, content.JCRContentDirName)
_, after, _ = strings.Cut(dir, content.JCRRoot)
if pathx.Exists(dir) {
if err = filex.CopyDir(dir, filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
if opts.PushContent {
for _, contentPath := range opts.ContentPaths {
if pathx.IsDir(contentPath) {
_, after, _ := strings.Cut(contentPath, content.JCRRoot)
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
if err = filex.CopyDir(contentPath, filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
} else if pathx.IsFile(contentPath) {
dir := filepath.Dir(contentPath)
_, after, _ := strings.Cut(dir, content.JCRRoot)
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
_, after, _ = strings.Cut(contentPath, content.JCRRoot)
if err = filex.Copy(contentPath, filepath.Join(tmpDir, content.JCRRoot, after), true); err != nil {
return "", err
}
if strings.HasSuffix(contentPath, content.JCRContentFile) {
dir = strings.ReplaceAll(contentPath, content.JCRContentNode, content.JCRContentDirName)
_, after, _ = strings.Cut(dir, content.JCRRoot)
if pathx.Exists(dir) {
if err = filex.CopyDir(dir, filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
return "", err
}
}
}
}
Expand Down Expand Up @@ -259,28 +259,27 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
return status.Path, nil
}

func determineFilterRoot(opts PackageCreateOpts) string {
if opts.ContentDir != "" {
return strings.Split(opts.ContentDir, content.JCRRoot)[1]
}

if opts.ContentFile != "" {
contentFile := strings.Split(opts.ContentFile, content.JCRRoot)[1]
if content.IsContentFile(opts.ContentFile) {
return strings.ReplaceAll(contentFile, content.JCRContentFile, content.JCRContentNode)
} else if strings.HasSuffix(contentFile, content.JCRContentFile) {
re := regexp.MustCompile(NamespacePattern)
contentFile = re.ReplaceAllString(contentFile, "$1:")
return filepath.Dir(contentFile)
} else if strings.HasSuffix(contentFile, content.JCRContentFileSuffix) {
re := regexp.MustCompile(NamespacePattern)
contentFile = re.ReplaceAllString(contentFile, "$1:")
return strings.ReplaceAll(contentFile, content.JCRContentFileSuffix, "")
func determineFilterRoots(opts PackageCreateOpts) []string {
var filterRoots []string
for _, contentPath := range opts.ContentPaths {
if pathx.IsDir(contentPath) {
filterRoots = append(filterRoots, strings.Split(contentPath, content.JCRRoot)[1])
} else if pathx.IsFile(contentPath) {
contentFile := strings.Split(contentPath, content.JCRRoot)[1]
if content.IsContentFile(contentPath) {
filterRoots = append(filterRoots, strings.ReplaceAll(contentFile, content.JCRContentFile, content.JCRContentNode))
} else if strings.HasSuffix(contentFile, content.JCRContentFile) {
contentFile = namespacePatternRegex.ReplaceAllString(contentFile, "$1:")
filterRoots = append(filterRoots, filepath.Dir(contentFile))
} else if strings.HasSuffix(contentFile, content.JCRContentFileSuffix) {
contentFile = namespacePatternRegex.ReplaceAllString(contentFile, "$1:")
filterRoots = append(filterRoots, strings.ReplaceAll(contentFile, content.JCRContentFileSuffix, ""))
} else {
filterRoots = append(filterRoots, contentFile)
}
}
return contentFile
}

return ""
return filterRoots
}

func (pm *PackageManager) Copy(remotePath string, destInstance *Instance) error {
Expand Down

0 comments on commit bc0d52f

Please sign in to comment.