Skip to content

Commit

Permalink
refactor Vault-Cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Przybyl committed Sep 29, 2024
1 parent 93798fc commit 86ea33e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
41 changes: 3 additions & 38 deletions pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/wttech/aemc/pkg/common/pathx"
"github.com/wttech/aemc/pkg/content"
"github.com/wttech/aemc/pkg/pkg"
"net/url"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -48,16 +47,7 @@ func (cm *ContentManager) pullContent(instance *Instance, workDir string, vault
return err
}
filterFile := filepath.Join(workDir, pkg.MetaPath, pkg.VltDir, FilterXML)
vaultCliArgs := []string{
"vlt",
"--credentials", fmt.Sprintf("%s:%s", instance.user, instance.password),
"checkout",
"--force",
"--filter", filterFile,
fmt.Sprintf("%s/crx/server/crx.default", instance.http.baseURL),
workDir,
}
if err := cm.vaultCli.CommandShell(vaultCliArgs); err != nil {
if err := cm.vaultCli.PullContent(instance, workDir, filterFile); err != nil {
return err
}
if err := cm.contentManager.DeleteFiles(filepath.Join(workDir, content.JCRRoot)); err != nil {
Expand Down Expand Up @@ -162,14 +152,7 @@ func (cm *ContentManager) pushContent(instance *Instance, vault bool, opts Packa
if vault {
mainDir, _, _ := strings.Cut(opts.ContentPath, content.JCRRoot)
jcrPath := DetermineFilterRoot(opts.ContentPath)
vaultCliArgs := []string{
"vlt",
"--credentials", fmt.Sprintf("%s:%s", instance.user, instance.password),
"import",
fmt.Sprintf("%s/crx/-/jcr:root%s", instance.http.baseURL, jcrPath),
mainDir,
}
if err := cm.vaultCli.CommandShell(vaultCliArgs); err != nil {
if err := cm.vaultCli.PushContent(instance, mainDir, jcrPath); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -258,29 +241,11 @@ func (cm *ContentManager) copyByVaultCli(srcInstance *Instance, destInstance *In
return err
}
} else {
parsedURLSrc, err := url.Parse(srcInstance.http.baseURL)
if err != nil {
return err
}
parsedURLDest, err := url.Parse(destInstance.http.baseURL)
if err != nil {
return err
}
if rcpArgs == "" {
rcpArgs = "-b 100 -r -u"
}
for _, filterRoot := range opts.FilterRoots {
vaultCliArgs := []string{"vlt", "rcp"}
vaultCliArgs = append(vaultCliArgs, strings.Fields(rcpArgs)...)
vaultCliArgs = append(vaultCliArgs, []string{
fmt.Sprintf("%s://%s:%s@%s/crx/-/jcr:root%s",
parsedURLSrc.Scheme, srcInstance.user, srcInstance.password,
parsedURLSrc.Host, filterRoot),
fmt.Sprintf("%s://%s:%s@%s/crx/-/jcr:root%s",
parsedURLDest.Scheme, destInstance.user, destInstance.password,
parsedURLDest.Host, filterRoot),
}...)
if err = cm.vaultCli.CommandShell(vaultCliArgs); err != nil {
if err := cm.vaultCli.CopyContent(srcInstance, destInstance, strings.Fields(rcpArgs), filterRoot); err != nil {
return err
}
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/vault_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/wttech/aemc/pkg/common/httpx"
"github.com/wttech/aemc/pkg/common/osx"
"github.com/wttech/aemc/pkg/common/pathx"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -106,3 +107,49 @@ func (v VaultCli) CommandShell(args []string) error {
}
return nil
}

func (v VaultCli) PushContent(instance *Instance, mainDir string, jcrPath string) error {
vaultCliArgs := []string{
"vlt",
"--credentials", fmt.Sprintf("%s:%s", instance.user, instance.password),
"import",
fmt.Sprintf("%s/crx/-/jcr:root%s", instance.http.baseURL, jcrPath),
mainDir,
}
return v.CommandShell(vaultCliArgs)
}

func (v VaultCli) PullContent(instance *Instance, mainDir string, filterFile string) error {
vaultCliArgs := []string{
"vlt",
"--credentials", fmt.Sprintf("%s:%s", instance.user, instance.password),
"checkout",
"--force",
"--filter", filterFile,
fmt.Sprintf("%s/crx/server/crx.default", instance.http.baseURL),
mainDir,
}
return v.CommandShell(vaultCliArgs)
}

func (v VaultCli) CopyContent(srcInstance *Instance, destInstance *Instance, rcpArgs []string, jcrPath string) error {
parsedURLSrc, err := url.Parse(srcInstance.http.baseURL)
if err != nil {
return err
}
parsedURLDest, err := url.Parse(destInstance.http.baseURL)
if err != nil {
return err
}
vaultCliArgs := []string{"vlt", "rcp"}
vaultCliArgs = append(vaultCliArgs, rcpArgs...)
vaultCliArgs = append(vaultCliArgs, []string{
fmt.Sprintf("%s://%s:%s@%s/crx/-/jcr:root%s",
parsedURLSrc.Scheme, srcInstance.user, srcInstance.password,
parsedURLSrc.Host, jcrPath),
fmt.Sprintf("%s://%s:%s@%s/crx/-/jcr:root%s",
parsedURLDest.Scheme, destInstance.user, destInstance.password,
parsedURLDest.Host, jcrPath),
}...)
return v.CommandShell(vaultCliArgs)
}

0 comments on commit 86ea33e

Please sign in to comment.