github.com/jfrog/jfrog-cli-core/v2@v2.52.0/distribution/commands/deletebundle.go (about) 1 package commands 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/jfrog/jfrog-client-go/utils/distribution" 7 8 "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" 9 "github.com/jfrog/jfrog-cli-core/v2/common/spec" 10 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 11 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 12 "github.com/jfrog/jfrog-client-go/distribution/services" 13 clientutils "github.com/jfrog/jfrog-client-go/utils" 14 "github.com/jfrog/jfrog-client-go/utils/errorutils" 15 "github.com/jfrog/jfrog-client-go/utils/log" 16 ) 17 18 type DeleteReleaseBundleCommand struct { 19 serverDetails *config.ServerDetails 20 deleteBundlesParams services.DeleteDistributionParams 21 distributionRules *spec.DistributionRules 22 dryRun bool 23 quiet bool 24 } 25 26 func NewReleaseBundleDeleteParams() *DeleteReleaseBundleCommand { 27 return &DeleteReleaseBundleCommand{} 28 } 29 30 func (db *DeleteReleaseBundleCommand) SetServerDetails(serverDetails *config.ServerDetails) *DeleteReleaseBundleCommand { 31 db.serverDetails = serverDetails 32 return db 33 } 34 35 func (db *DeleteReleaseBundleCommand) SetDistributeBundleParams(params services.DeleteDistributionParams) *DeleteReleaseBundleCommand { 36 db.deleteBundlesParams = params 37 return db 38 } 39 40 func (db *DeleteReleaseBundleCommand) SetDistributionRules(distributionRules *spec.DistributionRules) *DeleteReleaseBundleCommand { 41 db.distributionRules = distributionRules 42 return db 43 } 44 45 func (db *DeleteReleaseBundleCommand) SetDryRun(dryRun bool) *DeleteReleaseBundleCommand { 46 db.dryRun = dryRun 47 return db 48 } 49 50 func (db *DeleteReleaseBundleCommand) SetQuiet(quiet bool) *DeleteReleaseBundleCommand { 51 db.quiet = quiet 52 return db 53 } 54 55 func (db *DeleteReleaseBundleCommand) Run() error { 56 servicesManager, err := utils.CreateDistributionServiceManager(db.serverDetails, db.dryRun) 57 if err != nil { 58 return err 59 } 60 61 for _, spec := range db.distributionRules.DistributionRules { 62 db.deleteBundlesParams.DistributionRules = append(db.deleteBundlesParams.DistributionRules, spec.ToDistributionCommonParams()) 63 } 64 65 distributionRulesEmpty := db.distributionRulesEmpty() 66 if !db.quiet { 67 confirm, err := db.confirmDelete(distributionRulesEmpty) 68 if err != nil { 69 return err 70 } 71 if !confirm { 72 return nil 73 } 74 } 75 76 if distributionRulesEmpty && db.deleteBundlesParams.DeleteFromDistribution { 77 return servicesManager.DeleteLocalReleaseBundle(db.deleteBundlesParams) 78 } 79 return servicesManager.DeleteReleaseBundle(db.deleteBundlesParams) 80 } 81 82 func (db *DeleteReleaseBundleCommand) ServerDetails() (*config.ServerDetails, error) { 83 return db.serverDetails, nil 84 } 85 86 func (db *DeleteReleaseBundleCommand) CommandName() string { 87 return "rt_bundle_delete" 88 } 89 90 // Return true iff there are no distribution rules 91 func (db *DeleteReleaseBundleCommand) distributionRulesEmpty() bool { 92 return db.distributionRules == nil || 93 len(db.distributionRules.DistributionRules) == 0 || 94 len(db.distributionRules.DistributionRules) == 1 && db.distributionRules.DistributionRules[0].IsEmpty() 95 } 96 97 func (db *DeleteReleaseBundleCommand) confirmDelete(distributionRulesEmpty bool) (bool, error) { 98 message := fmt.Sprintf("Are you sure you want to delete the release bundle \"%s\"/\"%s\" ", db.deleteBundlesParams.Name, db.deleteBundlesParams.Version) 99 if distributionRulesEmpty && db.deleteBundlesParams.DeleteFromDistribution { 100 return coreutils.AskYesNo(message+"locally from distribution?\n"+ 101 "You can avoid this confirmation message by adding --quiet to the command.", false), nil 102 } 103 104 var distributionRulesBodies []distribution.DistributionRulesBody 105 for _, rule := range db.deleteBundlesParams.DistributionRules { 106 distributionRulesBodies = append(distributionRulesBodies, distribution.DistributionRulesBody{ 107 SiteName: rule.GetSiteName(), 108 CityName: rule.GetCityName(), 109 CountryCodes: rule.GetCountryCodes(), 110 }) 111 } 112 bytes, err := json.Marshal(distributionRulesBodies) 113 if err != nil { 114 return false, errorutils.CheckError(err) 115 } 116 117 log.Output(clientutils.IndentJson(bytes)) 118 if db.deleteBundlesParams.DeleteFromDistribution { 119 log.Output("This command will also delete the release bundle locally from distribution.") 120 } 121 return coreutils.AskYesNo(message+"with the above distribution rules?\n"+ 122 "You can avoid this confirmation message by adding --quiet to the command.", false), nil 123 }