github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/space/delete_space.go (about)

     1  package space
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf"
     5  	"github.com/cloudfoundry/cli/cf/api/spaces"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	"github.com/cloudfoundry/cli/cf/requirements"
    11  	"github.com/cloudfoundry/cli/cf/terminal"
    12  	"github.com/cloudfoundry/cli/flags"
    13  	"github.com/cloudfoundry/cli/flags/flag"
    14  )
    15  
    16  type DeleteSpace struct {
    17  	ui        terminal.UI
    18  	config    core_config.ReadWriter
    19  	spaceRepo spaces.SpaceRepository
    20  	spaceReq  requirements.SpaceRequirement
    21  }
    22  
    23  func init() {
    24  	command_registry.Register(&DeleteSpace{})
    25  }
    26  
    27  func (cmd *DeleteSpace) MetaData() command_registry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["f"] = &cliFlags.BoolFlag{Name: "f", Usage: T("Force deletion without confirmation")}
    30  
    31  	return command_registry.CommandMetadata{
    32  		Name:        "delete-space",
    33  		Description: T("Delete a space"),
    34  		Usage:       T("CF_NAME delete-space SPACE [-f]"),
    35  		Flags:       fs,
    36  	}
    37  }
    38  
    39  func (cmd *DeleteSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    40  	if len(fc.Args()) != 1 {
    41  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("delete-space"))
    42  	}
    43  
    44  	cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0])
    45  	reqs = []requirements.Requirement{
    46  		requirementsFactory.NewLoginRequirement(),
    47  		requirementsFactory.NewTargetedOrgRequirement(),
    48  		cmd.spaceReq,
    49  	}
    50  	return
    51  }
    52  
    53  func (cmd *DeleteSpace) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    54  	cmd.ui = deps.Ui
    55  	cmd.config = deps.Config
    56  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    57  	return cmd
    58  }
    59  func (cmd *DeleteSpace) Execute(c flags.FlagContext) {
    60  	spaceName := c.Args()[0]
    61  
    62  	if !c.Bool("f") {
    63  		if !cmd.ui.ConfirmDelete(T("space"), spaceName) {
    64  			return
    65  		}
    66  	}
    67  
    68  	cmd.ui.Say(T("Deleting space {{.TargetSpace}} in org {{.TargetOrg}} as {{.CurrentUser}}...",
    69  		map[string]interface{}{
    70  			"TargetSpace": terminal.EntityNameColor(spaceName),
    71  			"TargetOrg":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    72  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    73  		}))
    74  
    75  	space := cmd.spaceReq.GetSpace()
    76  
    77  	apiErr := cmd.spaceRepo.Delete(space.Guid)
    78  	if apiErr != nil {
    79  		cmd.ui.Failed(apiErr.Error())
    80  		return
    81  	}
    82  
    83  	cmd.ui.Ok()
    84  
    85  	if cmd.config.SpaceFields().Guid == space.Guid {
    86  		cmd.config.SetSpaceFields(models.SpaceFields{})
    87  		cmd.ui.Say(T("TIP: No space targeted, use '{{.CfTargetCommand}}' to target a space",
    88  			map[string]interface{}{"CfTargetCommand": cf.Name() + " target -s"}))
    89  	}
    90  
    91  	return
    92  }