github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/copy.go (about)

     1  package generic
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/spec"
     7  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     8  	"github.com/jfrog/jfrog-client-go/artifactory/services"
     9  	"github.com/jfrog/jfrog-client-go/utils/log"
    10  )
    11  
    12  type CopyCommand struct {
    13  	GenericCommand
    14  }
    15  
    16  func NewCopyCommand() *CopyCommand {
    17  	return &CopyCommand{GenericCommand: *NewGenericCommand()}
    18  }
    19  
    20  func (cc *CopyCommand) CommandName() string {
    21  	return "rt_copy"
    22  }
    23  
    24  // Copies the artifacts using the specified move pattern.
    25  func (cc *CopyCommand) Run() error {
    26  	// Create Service Manager:
    27  	servicesManager, err := utils.CreateServiceManager(cc.rtDetails, cc.dryRun)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	var errorOccurred = false
    33  	// Copy Loop:
    34  	for i := 0; i < len(cc.spec.Files); i++ {
    35  
    36  		copyParams, err := getCopyParams(cc.spec.Get(i))
    37  		if err != nil {
    38  			errorOccurred = true
    39  			log.Error(err)
    40  			continue
    41  		}
    42  
    43  		partialSuccess, partialFailed, err := servicesManager.Copy(copyParams)
    44  		success := cc.result.SuccessCount() + partialSuccess
    45  		cc.result.SetSuccessCount(success)
    46  		failed := cc.result.FailCount() + partialFailed
    47  		cc.result.SetFailCount(failed)
    48  		if err != nil {
    49  			errorOccurred = true
    50  			log.Error(err)
    51  			continue
    52  		}
    53  	}
    54  	if errorOccurred {
    55  		return errors.New("Copy finished with errors, please review the logs.")
    56  	}
    57  	return err
    58  }
    59  
    60  func getCopyParams(f *spec.File) (copyParams services.MoveCopyParams, err error) {
    61  	copyParams = services.NewMoveCopyParams()
    62  	copyParams.ArtifactoryCommonParams = f.ToArtifactoryCommonParams()
    63  	copyParams.Recursive, err = f.IsRecursive(true)
    64  	if err != nil {
    65  		return
    66  	}
    67  
    68  	copyParams.Flat, err = f.IsFlat(false)
    69  	if err != nil {
    70  		return
    71  	}
    72  	return
    73  }