github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v7/delete_buildpack_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/v7/shared"
    10  )
    11  
    12  //go:generate counterfeiter . DeleteBuildpackActor
    13  
    14  type DeleteBuildpackActor interface {
    15  	DeleteBuildpackByNameAndStack(buildpackName string, buildpackStack string) (v7action.Warnings, error)
    16  }
    17  
    18  type DeleteBuildpackCommand struct {
    19  	RequiredArgs    flag.BuildpackName `positional-args:"yes"`
    20  	usage           interface{}        `usage:"CF_NAME delete-buildpack BUILDPACK [-f] [-s STACK]"`
    21  	relatedCommands interface{}        `related_commands:"buildpacks"`
    22  	Force           bool               `long:"force" short:"f" description:"Force deletion without confirmation"`
    23  	Stack           string             `long:"stack" short:"s" description:"Specify stack to disambiguate buildpacks with the same name. Required when buildpack name is ambiguous"`
    24  	Actor           DeleteBuildpackActor
    25  	UI              command.UI
    26  	Config          command.Config
    27  	SharedActor     command.SharedActor
    28  }
    29  
    30  func (cmd *DeleteBuildpackCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.UI = ui
    32  	cmd.Config = config
    33  	sharedActor := sharedaction.NewActor(config)
    34  	cmd.SharedActor = sharedActor
    35  
    36  	ccClient, uaaClient, err := shared.NewClients(config, ui, true, "")
    37  	if err != nil {
    38  		return err
    39  	}
    40  	cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient)
    41  
    42  	return nil
    43  }
    44  
    45  func (cmd DeleteBuildpackCommand) Execute(args []string) error {
    46  	err := cmd.SharedActor.CheckTarget(false, false)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	if !cmd.Force {
    52  		response, uiErr := cmd.UI.DisplayBoolPrompt(false, "Really delete the {{.ModelType}} {{.ModelName}}?", map[string]interface{}{
    53  			"ModelType": "buildpack",
    54  			"ModelName": cmd.RequiredArgs.Buildpack,
    55  		})
    56  		if uiErr != nil {
    57  			return uiErr
    58  		}
    59  
    60  		if !response {
    61  			cmd.UI.DisplayText("Delete cancelled")
    62  			return nil
    63  		}
    64  	}
    65  
    66  	if cmd.Stack == "" {
    67  		cmd.UI.DisplayTextWithFlavor("Deleting buildpack {{.BuildpackName}}...", map[string]interface{}{
    68  			"BuildpackName": cmd.RequiredArgs.Buildpack,
    69  		})
    70  
    71  	} else {
    72  		cmd.UI.DisplayTextWithFlavor("Deleting buildpack {{.BuildpackName}} with stack {{.Stack}}...", map[string]interface{}{
    73  			"BuildpackName": cmd.RequiredArgs.Buildpack,
    74  			"Stack":         cmd.Stack,
    75  		})
    76  	}
    77  	warnings, err := cmd.Actor.DeleteBuildpackByNameAndStack(cmd.RequiredArgs.Buildpack, cmd.Stack)
    78  	cmd.UI.DisplayWarnings(warnings)
    79  
    80  	if err != nil {
    81  		switch err.(type) {
    82  		case actionerror.BuildpackNotFoundError:
    83  			if cmd.Stack == "" {
    84  				cmd.UI.DisplayWarning("Buildpack {{.BuildpackName}} does not exist.", map[string]interface{}{
    85  					"BuildpackName": cmd.RequiredArgs.Buildpack,
    86  				})
    87  			} else {
    88  				cmd.UI.DisplayWarning("Buildpack {{.BuildpackName}} with stack {{.Stack}} not found.", map[string]interface{}{
    89  					"BuildpackName": cmd.RequiredArgs.Buildpack,
    90  					"Stack":         cmd.Stack,
    91  				})
    92  			}
    93  		default:
    94  			return err
    95  		}
    96  	}
    97  
    98  	cmd.UI.DisplayOK()
    99  
   100  	return nil
   101  }