github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/delete.go (about) 1 package generic 2 3 import ( 4 "github.com/jfrog/jfrog-cli-go/artifactory/spec" 5 "github.com/jfrog/jfrog-cli-go/artifactory/utils" 6 "github.com/jfrog/jfrog-client-go/artifactory/services" 7 clientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" 8 "github.com/jfrog/jfrog-client-go/utils/errorutils" 9 ) 10 11 type DeleteCommand struct { 12 deleteItems []clientutils.ResultItem 13 GenericCommand 14 quiet bool 15 threads int 16 } 17 18 func NewDeleteCommand() *DeleteCommand { 19 return &DeleteCommand{GenericCommand: *NewGenericCommand()} 20 } 21 22 func (dc *DeleteCommand) Threads() int { 23 return dc.threads 24 } 25 26 func (dc *DeleteCommand) SetThreads(threads int) *DeleteCommand { 27 dc.threads = threads 28 return dc 29 } 30 31 func (dc *DeleteCommand) Quiet() bool { 32 return dc.quiet 33 } 34 35 func (dc *DeleteCommand) SetQuiet(quiet bool) *DeleteCommand { 36 dc.quiet = quiet 37 return dc 38 } 39 40 func (dc *DeleteCommand) DeleteItems() []clientutils.ResultItem { 41 return dc.deleteItems 42 } 43 44 func (dc *DeleteCommand) SetDeleteItems(deleteItems []clientutils.ResultItem) *DeleteCommand { 45 dc.deleteItems = deleteItems 46 return dc 47 } 48 49 func (dc *DeleteCommand) CommandName() string { 50 return "rt_delete" 51 } 52 53 func (dc *DeleteCommand) Run() error { 54 err := dc.GetPathsToDelete() 55 if err != nil { 56 return err 57 } 58 if dc.quiet || utils.ConfirmDelete(dc.deleteItems) { 59 success, failed, err := dc.DeleteFiles() 60 result := dc.Result() 61 result.SetFailCount(failed) 62 result.SetSuccessCount(success) 63 return err 64 } 65 return nil 66 } 67 68 func (dc *DeleteCommand) GetPathsToDelete() error { 69 rtDetails, err := dc.RtDetails() 70 if errorutils.CheckError(err) != nil { 71 return err 72 } 73 servicesManager, err := utils.CreateServiceManager(rtDetails, dc.DryRun()) 74 if err != nil { 75 return err 76 } 77 for i := 0; i < len(dc.Spec().Files); i++ { 78 deleteParams, err := getDeleteParams(dc.Spec().Get(i)) 79 if err != nil { 80 return err 81 } 82 83 currentResultItems, err := servicesManager.GetPathsToDelete(deleteParams) 84 if err != nil { 85 return err 86 } 87 dc.deleteItems = append(dc.deleteItems, currentResultItems...) 88 } 89 return nil 90 } 91 92 func (dc *DeleteCommand) DeleteFiles() (successCount, failedCount int, err error) { 93 rtDetails, err := dc.RtDetails() 94 if errorutils.CheckError(err) != nil { 95 return 0, 0, err 96 } 97 servicesManager, err := utils.CreateDeleteServiceManager(rtDetails, dc.Threads(), dc.DryRun()) 98 if err != nil { 99 return 0, 0, err 100 } 101 deletedCount, err := servicesManager.DeleteFiles(dc.deleteItems) 102 return deletedCount, len(dc.deleteItems) - deletedCount, err 103 } 104 105 func getDeleteParams(f *spec.File) (deleteParams services.DeleteParams, err error) { 106 deleteParams = services.NewDeleteParams() 107 deleteParams.ArtifactoryCommonParams = f.ToArtifactoryCommonParams() 108 deleteParams.Recursive, err = f.IsRecursive(true) 109 if err != nil { 110 return 111 } 112 return 113 }