github.com/jfrog/jfrog-cli-core/v2@v2.51.0/lifecycle/deleteremote.go (about) 1 package lifecycle 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/jfrog/jfrog-cli-core/v2/common/spec" 7 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 8 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 9 "github.com/jfrog/jfrog-client-go/lifecycle" 10 "github.com/jfrog/jfrog-client-go/lifecycle/services" 11 clientutils "github.com/jfrog/jfrog-client-go/utils" 12 "github.com/jfrog/jfrog-client-go/utils/distribution" 13 "github.com/jfrog/jfrog-client-go/utils/errorutils" 14 "github.com/jfrog/jfrog-client-go/utils/log" 15 ) 16 17 const avoidConfirmationMsg = "You can avoid this confirmation message by adding --quiet to the command." 18 19 type ReleaseBundleRemoteDeleteCommand struct { 20 releaseBundleCmd 21 distributionRules *spec.DistributionRules 22 dryRun bool 23 quiet bool 24 maxWaitMinutes int 25 } 26 27 func NewReleaseBundleRemoteDeleteCommand() *ReleaseBundleRemoteDeleteCommand { 28 return &ReleaseBundleRemoteDeleteCommand{} 29 } 30 31 func (rbd *ReleaseBundleRemoteDeleteCommand) SetServerDetails(serverDetails *config.ServerDetails) *ReleaseBundleRemoteDeleteCommand { 32 rbd.serverDetails = serverDetails 33 return rbd 34 } 35 36 func (rbd *ReleaseBundleRemoteDeleteCommand) SetReleaseBundleName(releaseBundleName string) *ReleaseBundleRemoteDeleteCommand { 37 rbd.releaseBundleName = releaseBundleName 38 return rbd 39 } 40 41 func (rbd *ReleaseBundleRemoteDeleteCommand) SetReleaseBundleVersion(releaseBundleVersion string) *ReleaseBundleRemoteDeleteCommand { 42 rbd.releaseBundleVersion = releaseBundleVersion 43 return rbd 44 } 45 46 func (rbd *ReleaseBundleRemoteDeleteCommand) SetSync(sync bool) *ReleaseBundleRemoteDeleteCommand { 47 rbd.sync = sync 48 return rbd 49 } 50 51 func (rbd *ReleaseBundleRemoteDeleteCommand) SetReleaseBundleProject(rbProjectKey string) *ReleaseBundleRemoteDeleteCommand { 52 rbd.rbProjectKey = rbProjectKey 53 return rbd 54 } 55 56 func (rbd *ReleaseBundleRemoteDeleteCommand) SetDistributionRules(distributionRules *spec.DistributionRules) *ReleaseBundleRemoteDeleteCommand { 57 rbd.distributionRules = distributionRules 58 return rbd 59 } 60 61 func (rbd *ReleaseBundleRemoteDeleteCommand) SetDryRun(dryRun bool) *ReleaseBundleRemoteDeleteCommand { 62 rbd.dryRun = dryRun 63 return rbd 64 } 65 66 func (rbd *ReleaseBundleRemoteDeleteCommand) SetQuiet(quiet bool) *ReleaseBundleRemoteDeleteCommand { 67 rbd.quiet = quiet 68 return rbd 69 } 70 71 func (rbd *ReleaseBundleRemoteDeleteCommand) SetMaxWaitMinutes(maxWaitMinutes int) *ReleaseBundleRemoteDeleteCommand { 72 rbd.maxWaitMinutes = maxWaitMinutes 73 return rbd 74 } 75 76 func (rbd *ReleaseBundleRemoteDeleteCommand) CommandName() string { 77 return "rb_remote_delete" 78 } 79 80 func (rbd *ReleaseBundleRemoteDeleteCommand) ServerDetails() (*config.ServerDetails, error) { 81 return rbd.serverDetails, nil 82 } 83 84 func (rbd *ReleaseBundleRemoteDeleteCommand) Run() error { 85 if err := validateArtifactoryVersionSupported(rbd.serverDetails); err != nil { 86 return err 87 } 88 89 servicesManager, rbDetails, queryParams, err := rbd.getPrerequisites() 90 if err != nil { 91 return err 92 } 93 94 return rbd.deleteRemote(servicesManager, rbDetails, queryParams) 95 } 96 97 func (rbd *ReleaseBundleRemoteDeleteCommand) deleteRemote(servicesManager *lifecycle.LifecycleServicesManager, 98 rbDetails services.ReleaseBundleDetails, queryParams services.CommonOptionalQueryParams) error { 99 100 confirm, err := rbd.confirmDelete() 101 if err != nil || !confirm { 102 return err 103 } 104 105 aggregatedRules := rbd.getAggregatedDistRules() 106 107 return servicesManager.RemoteDeleteReleaseBundle(rbDetails, services.ReleaseBundleRemoteDeleteParams{ 108 DistributionRules: aggregatedRules, 109 DryRun: rbd.dryRun, 110 MaxWaitMinutes: rbd.maxWaitMinutes, 111 CommonOptionalQueryParams: queryParams, 112 }) 113 } 114 115 func (rbd *ReleaseBundleRemoteDeleteCommand) distributionRulesEmpty() bool { 116 return rbd.distributionRules == nil || 117 len(rbd.distributionRules.DistributionRules) == 0 || 118 len(rbd.distributionRules.DistributionRules) == 1 && rbd.distributionRules.DistributionRules[0].IsEmpty() 119 } 120 121 func (rbd *ReleaseBundleRemoteDeleteCommand) confirmDelete() (bool, error) { 122 if rbd.quiet { 123 return true, nil 124 } 125 126 message := fmt.Sprintf("Are you sure you want to delete the release bundle '%s/%s' remotely ", rbd.releaseBundleName, rbd.releaseBundleVersion) 127 if rbd.distributionRulesEmpty() { 128 message += "from all edges?" 129 } else { 130 var distributionRulesBodies []distribution.DistributionRulesBody 131 for _, rule := range rbd.distributionRules.DistributionRules { 132 distributionRulesBodies = append(distributionRulesBodies, distribution.DistributionRulesBody{ 133 SiteName: rule.SiteName, 134 CityName: rule.CityName, 135 CountryCodes: rule.CountryCodes, 136 }) 137 } 138 bytes, err := json.Marshal(distributionRulesBodies) 139 if err != nil { 140 return false, errorutils.CheckError(err) 141 } 142 143 log.Output(clientutils.IndentJson(bytes)) 144 message += "from all edges with the above distribution rules?" 145 } 146 147 return coreutils.AskYesNo(message+"\n"+avoidConfirmationMsg, false), nil 148 } 149 150 func (rbd *ReleaseBundleRemoteDeleteCommand) getAggregatedDistRules() (aggregatedRules []*distribution.DistributionCommonParams) { 151 if rbd.distributionRulesEmpty() { 152 aggregatedRules = append(aggregatedRules, &distribution.DistributionCommonParams{SiteName: "*"}) 153 } else { 154 for _, rules := range rbd.distributionRules.DistributionRules { 155 aggregatedRules = append(aggregatedRules, rules.ToDistributionCommonParams()) 156 } 157 } 158 return 159 }