Skip to content

Commit

Permalink
Merge pull request #200 from wttech/pkg-copy
Browse files Browse the repository at this point in the history
added package copy
  • Loading branch information
krystian-panek-vmltech authored Oct 4, 2023
2 parents 1b33e12 + 9dd1cbf commit 63b92ad
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cmd/aem/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *CLI) pkgCmd() *cobra.Command {
cmd.AddCommand(c.pkgDownloadCmd())
cmd.AddCommand(c.pkgBuildCmd())
cmd.AddCommand(c.pkgFindCmd())
cmd.AddCommand(c.pkgCopyCmd())
return cmd
}

Expand Down Expand Up @@ -669,3 +670,76 @@ func (c *CLI) pkgDownloadCmd() *cobra.Command {
pkgDefineDownloadFlags(cmd)
return cmd
}

func (c *CLI) pkgCopyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "copy",
Aliases: []string{"cp"},
Short: "Copy package to another instance",
Run: func(cmd *cobra.Command, args []string) {
instance, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
targetInstance, err := determineTargetInstance(cmd, c.aem.InstanceManager())
if err != nil {
c.Error(err)
return
}
p, err := pkgPIDByFlags(cmd, *instance)
if err != nil {
c.Error(err)
return
}
force, _ := cmd.Flags().GetBool("force")
changed := false
if force {
err = p.Copy(targetInstance)
changed = true
} else {
changed, err = p.CopyWithChanged(targetInstance)
}
if err != nil {
c.Error(err)
return
}
if err := c.aem.InstanceManager().AwaitStartedOne(*targetInstance); err != nil {
c.Error(err)
return
}
c.SetOutput("package", p)
c.SetOutput("instance", instance)
c.SetOutput("targetInstance", targetInstance)
if changed {
c.Changed("package copied")
} else {
c.Ok("package already copied (up-to-date)")
}
},
}
cmd.Flags().StringP("instance-target-url", "u", "", "Destination instance URL")
cmd.Flags().StringP("instance-target-id", "i", "", "Destination instance ID")
cmd.MarkFlagsOneRequired("instance-target-url", "instance-target-id")
cmd.Flags().String("pid", "", "ID (group:name:version)'")
cmd.Flags().String("path", "", "Remote repository path")
cmd.MarkFlagsOneRequired("pid", "path")
cmd.Flags().BoolP("force", "f", false, "Copy even when already copied")
return cmd
}

func determineTargetInstance(cmd *cobra.Command, instanceManager *pkg.InstanceManager) (*pkg.Instance, error) {
var instance *pkg.Instance
url, _ := cmd.Flags().GetString("instance-target-url")
if url != "" {
instance, _ = instanceManager.NewByIDAndURL("remote_adhoc_target", url)
}
id, _ := cmd.Flags().GetString("instance-target-id")
if id != "" {
instance = instanceManager.NewByID(id)
}
if instance == nil {
return nil, fmt.Errorf("missing 'instance-target-url' or 'instance-target-id'")
}
return instance, nil
}
29 changes: 29 additions & 0 deletions pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,32 @@ func (p Package) DownloadWithChanged(localFile string) (bool, error) {
}
return false, nil
}

func (p Package) Copy(destInstance *Instance) error {
state, err := p.State()
if err != nil {
return err
}
if !state.Exists {
return fmt.Errorf("%s > package '%s' cannot be copied as it does not exist", p.manager.instance.ID(), p.PID.String())
}
return p.manager.Copy(state.Data.Path, destInstance)
}

func (p Package) CopyWithChanged(destInstance *Instance) (bool, error) {
state, err := p.State()
if err != nil {
return false, err
}
if !state.Exists {
return false, fmt.Errorf("%s > package '%s' cannot be copied as it does not exist", p.manager.instance.ID(), p.PID.String())
}
destItem, err := destInstance.PackageManager().Find(state.PID)
if err != nil {
return false, err
}
if destItem != nil {
return false, nil
}
return true, p.manager.Copy(state.Data.Path, destInstance)
}
18 changes: 18 additions & 0 deletions pkg/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
return status.Path, nil
}

func (pm *PackageManager) Copy(remotePath string, destInstance *Instance) error {
var localPath = pathx.RandomFileName(pm.tmpDir(), "pkg_copy", ".zip")
defer func() {
_ = pathx.DeleteIfExists(localPath)
}()
if err := pm.Download(remotePath, localPath); err != nil {
return err
}
destRemotePath, err := destInstance.PackageManager().Upload(localPath)
if err != nil {
return err
}
if err := destInstance.PackageManager().Install(destRemotePath); err != nil {
return err
}
return nil
}

func (pm *PackageManager) tmpDir() string {
return pm.instance.manager.aem.baseOpts.TmpDir
}
Expand Down

0 comments on commit 63b92ad

Please sign in to comment.