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

     1  package service
     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 DeleteService struct {
    17  	ui                 terminal.UI
    18  	config             coreconfig.Reader
    19  	serviceRepo        api.ServiceRepository
    20  	serviceInstanceReq requirements.ServiceInstanceRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&DeleteService{})
    25  }
    26  
    27  func (cmd *DeleteService) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    30  
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "delete-service",
    33  		ShortName:   "ds",
    34  		Description: T("Delete a service instance"),
    35  		Usage: []string{
    36  			T("CF_NAME delete-service SERVICE_INSTANCE [-f]"),
    37  		},
    38  		Flags: fs,
    39  	}
    40  }
    41  
    42  func (cmd *DeleteService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	if len(fc.Args()) != 1 {
    44  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-service"))
    45  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    46  	}
    47  
    48  	reqs := []requirements.Requirement{
    49  		requirementsFactory.NewLoginRequirement(),
    50  	}
    51  
    52  	return reqs, nil
    53  }
    54  
    55  func (cmd *DeleteService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    56  	cmd.ui = deps.UI
    57  	cmd.config = deps.Config
    58  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *DeleteService) Execute(c flags.FlagContext) error {
    63  	serviceName := c.Args()[0]
    64  
    65  	if !c.Bool("f") {
    66  		if !cmd.ui.ConfirmDelete(T("service"), serviceName) {
    67  			return nil
    68  		}
    69  	}
    70  
    71  	cmd.ui.Say(T("Deleting service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    72  		map[string]interface{}{
    73  			"ServiceName": terminal.EntityNameColor(serviceName),
    74  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    75  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    76  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    77  		}))
    78  
    79  	instance, err := cmd.serviceRepo.FindInstanceByName(serviceName)
    80  
    81  	switch err.(type) {
    82  	case nil:
    83  	case *errors.ModelNotFoundError:
    84  		cmd.ui.Ok()
    85  		cmd.ui.Warn(T("Service {{.ServiceName}} does not exist.", map[string]interface{}{"ServiceName": serviceName}))
    86  		return nil
    87  	default:
    88  		return err
    89  	}
    90  
    91  	err = cmd.serviceRepo.DeleteService(instance)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	err = printSuccessMessageForServiceInstance(serviceName, cmd.serviceRepo, cmd.ui)
    97  	if err != nil {
    98  		cmd.ui.Ok()
    99  	}
   100  	return nil
   101  }