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

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