Skip to content

Commit

Permalink
refactor content sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Przybyl committed Sep 12, 2024
1 parent 67fb491 commit d9fd145
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 305 deletions.
66 changes: 56 additions & 10 deletions cmd/aem/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/wttech/aemc/pkg"
"github.com/wttech/aemc/pkg/common/pathx"
"github.com/wttech/aemc/pkg/content"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -74,7 +76,7 @@ func (c *CLI) contentDownloadCmd() *cobra.Command {
}
pid, _ := cmd.Flags().GetString("pid")
targetFile, _ := cmd.Flags().GetString("target-file")
filterRoots, _ := cmd.Flags().GetStringSlice("filter-roots")
filterRoots := determineFilterRoots(cmd)
filterFile, _ := cmd.Flags().GetString("filter-file")
if err = instance.ContentManager().Download(targetFile, pkg.PackageCreateOpts{
PID: pid,
Expand Down Expand Up @@ -108,8 +110,6 @@ func (c *CLI) contentPullCmd() *cobra.Command {
c.Error(err)
return
}
clean, _ := cmd.Flags().GetBool("clean")
replace, _ := cmd.Flags().GetBool("replace")
dir, err := determineContentDir(cmd)
if err != nil {
c.Error(err)
Expand All @@ -120,21 +120,24 @@ func (c *CLI) contentPullCmd() *cobra.Command {
c.Error(err)
return
}
filterRoots := determineFilterRoots(cmd)
filterFile, _ := cmd.Flags().GetString("filter-file")
excludePatterns := determineExcludePatterns(cmd)
clean, _ := cmd.Flags().GetBool("clean")
replace, _ := cmd.Flags().GetBool("replace")
if dir != "" {
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,
}); err != nil {
c.Error(err)
return
}
c.SetOutput("dir", dir)
} else if file != "" {
if err = instance.ContentManager().PullFile(file, clean, pkg.PackageCreateOpts{
ContentFile: file,
FilterRoots: filterRoots,
ExcludePatterns: excludePatterns,
}); err != nil {
c.Error(err)
return
Expand Down Expand Up @@ -172,7 +175,7 @@ func (c *CLI) contentCopyCmd() *cobra.Command {
c.Error(err)
return
}
filterRoots, _ := cmd.Flags().GetStringSlice("filter-roots")
filterRoots := determineFilterRoots(cmd)
filterFile, _ := cmd.Flags().GetString("filter-file")
clean, _ := cmd.Flags().GetBool("clean")
if err = instance.ContentManager().Copy(targetInstance, clean, pkg.PackageCreateOpts{
Expand Down Expand Up @@ -221,7 +224,7 @@ func determineContentDir(cmd *cobra.Command) (string, error) {
return "", fmt.Errorf("content path '%s' does not contain '%s'", path, content.JCRRoot)
}
if path != "" && !pathx.Exists(path) {
return "", fmt.Errorf("content path does not exist: %s", path)
return "", fmt.Errorf("content path '%s' need to exist on file system; consider using 'dir' or 'file' parameter otherwise", path)
}
if path != "" && pathx.IsDir(path) {
return path, nil
Expand All @@ -239,10 +242,53 @@ func determineContentFile(cmd *cobra.Command) (string, error) {
return "", fmt.Errorf("content path '%s' does not contain '%s'", path, content.JCRRoot)
}
if path != "" && !pathx.Exists(path) {
return "", fmt.Errorf("content path does not exist: %s", path)
return "", fmt.Errorf("content path '%s' need to exist on file system; consider using 'dir' or 'file' parameter otherwise", path)
}
if path != "" && pathx.IsFile(path) {
return path, nil
}
return file, nil
}

func determineFilterRoots(cmd *cobra.Command) []string {
filterRoots, _ := cmd.Flags().GetStringSlice("filter-roots")
if len(filterRoots) > 0 {
return filterRoots
}
filterFile, _ := cmd.Flags().GetString("filter-file")
if filterFile != "" {
return nil
}
dir, _ := determineContentDir(cmd)
if dir != "" {
return []string{pkg.DetermineFilterRoot(dir)}
}
file, _ := determineContentFile(cmd)
if file != "" {
return []string{pkg.DetermineFilterRoot(file)}
}
return nil
}

func determineExcludePatterns(cmd *cobra.Command) []string {
file, _ := determineContentFile(cmd)
if file == "" || !strings.HasSuffix(file, content.JCRContentFile) {
return nil
}

dir := filepath.Dir(file)
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}

var excludePatterns []string
for _, entry := range entries {
if entry.Name() != content.JCRContentFile {
jcrPath := pkg.DetermineFilterRoot(filepath.Join(dir, entry.Name()))
excludePattern := fmt.Sprintf("%s(/.*)?", jcrPath)
excludePatterns = append(excludePatterns, excludePattern)
}
}
return excludePatterns
}
6 changes: 2 additions & 4 deletions cmd/aem/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,7 @@ func (c *CLI) pkgPathByFlags(cmd *cobra.Command) (string, error) {
path := c.aem.BaseOpts().TmpDir + "/package/" + fileName
if !pathx.Exists(path) {
log.Infof("downloading package from URL '%s' to file '%s'", url, path)
err := httpx.DownloadOnce(url, path)
if err != nil {
if err := httpx.DownloadOnce(url, path); err != nil {
return "", err
}
log.Infof("downloaded package from URL '%s' to file '%s'", url, path)
Expand Down Expand Up @@ -598,8 +597,7 @@ func (c *CLI) pkgUpdateCmd() *cobra.Command {
return
}
filterRoots, _ := cmd.Flags().GetStringSlice("filter-roots")
err = p.UpdateFilters(pkg.NewPackageFilters(filterRoots))
if err != nil {
if err = p.UpdateFilters(pkg.NewPackageFilters(filterRoots)); err != nil {
c.Error(err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/pathx/pathx.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ func DirAndFileName(path string) (string, string) {

func RandomDir(tmpDir string, prefix string) string {
suffix, _ := goutils.RandomNumeric(8)
return filepath.Join(tmpDir, prefix+"_"+suffix)
return Abs(filepath.Join(tmpDir, prefix+"_"+suffix))
}

func RandomFileName(tmpDir string, prefix string, extension string) string {
suffix, _ := goutils.RandomNumeric(8)
return filepath.Join(tmpDir, prefix+"_"+suffix+extension)
return Abs(filepath.Join(tmpDir, prefix+"_"+suffix+extension))
}
Loading

0 comments on commit d9fd145

Please sign in to comment.