github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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  		requirementsFactory.NewTargetedSpaceRequirement(),
    51  	}
    52  
    53  	return reqs, nil
    54  }
    55  
    56  func (cmd *DeleteService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    57  	cmd.ui = deps.UI
    58  	cmd.config = deps.Config
    59  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    60  	return cmd
    61  }
    62  
    63  func (cmd *DeleteService) Execute(c flags.FlagContext) error {
    64  	serviceName := c.Args()[0]
    65  
    66  	if !c.Bool("f") {
    67  		if !cmd.ui.ConfirmDelete(T("service"), serviceName) {
    68  			return nil
    69  		}
    70  	}
    71  
    72  	cmd.ui.Say(T("Deleting service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    73  		map[string]interface{}{
    74  			"ServiceName": terminal.EntityNameColor(serviceName),
    75  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    76  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    77  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    78  		}))
    79  
    80  	instance, err := cmd.serviceRepo.FindInstanceByName(serviceName)
    81  
    82  	switch err.(type) {
    83  	case nil:
    84  	case *errors.ModelNotFoundError:
    85  		cmd.ui.Ok()
    86  		cmd.ui.Warn(T("Service {{.ServiceName}} does not exist.", map[string]interface{}{"ServiceName": serviceName}))
    87  		return nil
    88  	default:
    89  		return err
    90  	}
    91  
    92  	err = cmd.serviceRepo.DeleteService(instance)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	err = printSuccessMessageForServiceInstance(serviceName, cmd.serviceRepo, cmd.ui)
    98  	if err != nil {
    99  		cmd.ui.Ok()
   100  	}
   101  	return nil
   102  }