Skip to content

Commit

Permalink
added download content using Vault-Cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-przybyl-wttech committed Aug 25, 2024
1 parent 8b5e818 commit 282be53
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
7 changes: 6 additions & 1 deletion cmd/aem/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func (c *CLI) contentDownloadCmd() *cobra.Command {
targetFile, _ := cmd.Flags().GetString("target-file")
filterRoots := determineFilterRoots(cmd)
filterFile, _ := cmd.Flags().GetString("filter-file")
if err = instance.ContentManager().Download(targetFile, pkg.PackageCreateOpts{
vault, _ := cmd.Flags().GetBool("vault")
vault = vault && lo.EveryBy(filterRoots, func(filterRoot string) bool {
return pathx.IsDir(filterRoot)
})
if err = instance.ContentManager().Download(targetFile, vault, pkg.PackageCreateOpts{
PID: pid,
FilterRoots: filterRoots,
FilterFile: filterFile,
Expand All @@ -98,6 +102,7 @@ func (c *CLI) contentDownloadCmd() *cobra.Command {
cmd.Flags().StringSliceP("filter-roots", "r", []string{}, "Vault filter root paths")
cmd.Flags().StringP("filter-file", "f", "", "Vault filter file path")
cmd.MarkFlagsOneRequired("filter-roots", "filter-file")
cmd.Flags().Bool("vault", false, "Use Vault-Cli to download content")
return cmd
}

Expand Down
70 changes: 56 additions & 14 deletions pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,62 @@ func (cm *ContentManager) tmpDir() string {
return cm.instance.manager.aem.baseOpts.TmpDir
}

func (cm *ContentManager) Download(localFile string, opts PackageCreateOpts) error {
func (cm *ContentManager) Download(localFile string, vault bool, opts PackageCreateOpts) error {
workDir := pathx.RandomDir(cm.tmpDir(), "content_download")
defer func() { _ = pathx.DeleteIfExists(workDir) }()
if opts.PID == "" {
opts.PID = fmt.Sprintf("aemc:content-download:%s-SNAPSHOT", timex.FileTimestampForNow())
}
remotePath, err := cm.pkgMgr().Create(opts)
if err != nil {
return err
}
defer func() { _ = cm.pkgMgr().Delete(remotePath) }()
if err := cm.pkgMgr().Build(remotePath); err != nil {
return err
}
if err := cm.pkgMgr().Download(remotePath, localFile); err != nil {
return err
if vault {
pidConfig, err := pkg.ParsePID(opts.PID)
if err != nil {
return err
}
data := map[string]any{
"Pid": opts.PID,
"Group": pidConfig.Group,
"Name": pidConfig.Name,
"Version": pidConfig.Version,
"FilterRoots": opts.FilterRoots,
"ExcludePatterns": opts.ExcludePatterns,
}
if err = copyPackageDefaultFiles(workDir, data); err != nil {
return err
}
filterFile, err := cm.determineFilterFile(workDir, opts)
if err != nil {
return err
}
if err = os.Rename(filterFile, filepath.Join(workDir, pkg.MetaPath, pkg.VltDir, FilterXML)); err != nil {
return err
}
vaultCliArgs := []string{
"vlt",
"--credentials", fmt.Sprintf("%s:%s", cm.instance.user, cm.instance.password),
"checkout",
"--force",
"--filter", filterFile,
fmt.Sprintf("%s/crx/server/crx.default", cm.instance.http.baseURL),
workDir,
}
if err = cm.vaultCli().CommandShell(vaultCliArgs); err != nil {
return err
}
if err = content.Zip(workDir, localFile); err != nil {
return err
}
} else {
remotePath, err := cm.pkgMgr().Create(opts)
if err != nil {
return err
}
defer func() { _ = cm.pkgMgr().Delete(remotePath) }()
if err := cm.pkgMgr().Build(remotePath); err != nil {
return err
}
if err := cm.pkgMgr().Download(remotePath, localFile); err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -91,7 +133,7 @@ func (cm *ContentManager) PullDir(dir string, clean bool, vault bool, replace bo
} else {
pkgFile := pathx.RandomFileName(cm.tmpDir(), "content_pull", ".zip")
defer func() { _ = pathx.DeleteIfExists(pkgFile) }()
if err := cm.Download(pkgFile, opts); err != nil {
if err := cm.Download(pkgFile, false, opts); err != nil {
return err
}
if err := content.Unzip(pkgFile, workDir); err != nil {
Expand Down Expand Up @@ -148,7 +190,7 @@ func (cm *ContentManager) PullFile(file string, clean bool, vault bool, opts Pac
} else {
pkgFile := pathx.RandomFileName(cm.tmpDir(), "content_pull", ".zip")
defer func() { _ = pathx.DeleteIfExists(pkgFile) }()
if err := cm.Download(pkgFile, opts); err != nil {
if err := cm.Download(pkgFile, false, opts); err != nil {
return err
}
if err := content.Unzip(pkgFile, workDir); err != nil {
Expand Down Expand Up @@ -268,7 +310,7 @@ func (cm *ContentManager) Copy(destInstance *Instance, clean bool, vault bool, o
return err
}
} else {
if err := cm.Download(pkgFile, opts); err != nil {
if err := cm.Download(pkgFile, false, opts); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
return "", err
}
if opts.FilterFile != "" {
if err = filex.Copy(opts.FilterFile, filepath.Join(tmpDir, "META-INF", "vault", FilterXML), true); err != nil {
if err = filex.Copy(opts.FilterFile, filepath.Join(tmpDir, pkg.MetaPath, pkg.VltDir, FilterXML), true); err != nil {
return "", err
}
}
Expand Down

0 comments on commit 282be53

Please sign in to comment.