github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/delete_service_broker.go (about)

     1  package servicebroker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type DeleteServiceBroker struct {
    17  	ui     terminal.UI
    18  	config coreconfig.Reader
    19  	repo   api.ServiceBrokerRepository
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&DeleteServiceBroker{})
    24  }
    25  
    26  func (cmd *DeleteServiceBroker) MetaData() commandregistry.CommandMetadata {
    27  	fs := make(map[string]flags.FlagSet)
    28  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    29  
    30  	return commandregistry.CommandMetadata{
    31  		Name:        "delete-service-broker",
    32  		Description: T("Delete a service broker"),
    33  		Usage: []string{
    34  			T("CF_NAME delete-service-broker SERVICE_BROKER [-f]"),
    35  		},
    36  		Flags: fs,
    37  	}
    38  }
    39  
    40  func (cmd *DeleteServiceBroker) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    41  	if len(fc.Args()) != 1 {
    42  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-service-broker"))
    43  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    44  	}
    45  
    46  	reqs := []requirements.Requirement{
    47  		requirementsFactory.NewLoginRequirement(),
    48  	}
    49  
    50  	return reqs, nil
    51  }
    52  
    53  func (cmd *DeleteServiceBroker) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    54  	cmd.ui = deps.UI
    55  	cmd.config = deps.Config
    56  	cmd.repo = deps.RepoLocator.GetServiceBrokerRepository()
    57  	return cmd
    58  }
    59  
    60  func (cmd *DeleteServiceBroker) Execute(c flags.FlagContext) error {
    61  	brokerName := c.Args()[0]
    62  	if !c.Bool("f") && !cmd.ui.ConfirmDelete(T("service-broker"), brokerName) {
    63  		return nil
    64  	}
    65  
    66  	cmd.ui.Say(T("Deleting service broker {{.Name}} as {{.Username}}...",
    67  		map[string]interface{}{
    68  			"Name":     terminal.EntityNameColor(brokerName),
    69  			"Username": terminal.EntityNameColor(cmd.config.Username()),
    70  		}))
    71  
    72  	broker, err := cmd.repo.FindByName(brokerName)
    73  
    74  	switch err.(type) {
    75  	case nil:
    76  	case *errors.ModelNotFoundError:
    77  		cmd.ui.Ok()
    78  		cmd.ui.Warn(T("Service Broker {{.Name}} does not exist.", map[string]interface{}{"Name": brokerName}))
    79  		return nil
    80  	default:
    81  		return err
    82  	}
    83  
    84  	err = cmd.repo.Delete(broker.GUID)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	cmd.ui.Ok()
    90  	return nil
    91  }