github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/move.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 MoveCommand struct {
    13  	GenericCommand
    14  }
    15  
    16  func NewMoveCommand() *MoveCommand {
    17  	return &MoveCommand{GenericCommand: *NewGenericCommand()}
    18  }
    19  
    20  // Moves the artifacts using the specified move pattern.
    21  func (mc *MoveCommand) Run() error {
    22  	// Create Service Manager:
    23  	servicesManager, err := utils.CreateServiceManager(mc.rtDetails, mc.DryRun())
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	var errorOccurred = false
    29  	// Move Loop:
    30  	for i := 0; i < len(mc.Spec().Files); i++ {
    31  
    32  		moveParams, err := getMoveParams(mc.Spec().Get(i))
    33  		if err != nil {
    34  			errorOccurred = true
    35  			log.Error(err)
    36  			continue
    37  		}
    38  
    39  		partialSuccess, partialFailed, err := servicesManager.Move(moveParams)
    40  		success := mc.result.SuccessCount() + partialSuccess
    41  		mc.result.SetSuccessCount(success)
    42  		failed := mc.result.FailCount() + partialFailed
    43  		mc.result.SetFailCount(failed)
    44  		if err != nil {
    45  			errorOccurred = true
    46  			log.Error(err)
    47  			continue
    48  		}
    49  	}
    50  	if errorOccurred {
    51  		return errors.New("Move finished with errors, please review the logs.")
    52  	}
    53  	return err
    54  
    55  }
    56  
    57  func (mc *MoveCommand) CommandName() string {
    58  	return "rt_move"
    59  }
    60  
    61  func getMoveParams(f *spec.File) (moveParams services.MoveCopyParams, err error) {
    62  	moveParams = services.NewMoveCopyParams()
    63  	moveParams.ArtifactoryCommonParams = f.ToArtifactoryCommonParams()
    64  	moveParams.Recursive, err = f.IsRecursive(true)
    65  	if err != nil {
    66  		return
    67  	}
    68  
    69  	moveParams.Flat, err = f.IsFlat(false)
    70  	if err != nil {
    71  		return
    72  	}
    73  	return
    74  }