github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/delete_space.go (about)

     1  package space
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/api/organizations"
     8  	"code.cloudfoundry.org/cli/cf/api/spaces"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/models"
    14  	"code.cloudfoundry.org/cli/cf/requirements"
    15  	"code.cloudfoundry.org/cli/cf/terminal"
    16  )
    17  
    18  type DeleteSpace struct {
    19  	ui        terminal.UI
    20  	config    coreconfig.ReadWriter
    21  	spaceRepo spaces.SpaceRepository
    22  	spaceReq  requirements.SpaceRequirement
    23  	orgRepo   organizations.OrganizationRepository
    24  }
    25  
    26  func init() {
    27  	commandregistry.Register(&DeleteSpace{})
    28  }
    29  
    30  func (cmd *DeleteSpace) MetaData() commandregistry.CommandMetadata {
    31  	fs := make(map[string]flags.FlagSet)
    32  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    33  	fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Delete space within specified org")}
    34  
    35  	return commandregistry.CommandMetadata{
    36  		Name:        "delete-space",
    37  		Description: T("Delete a space"),
    38  		Usage: []string{
    39  			T("CF_NAME delete-space SPACE [-o ORG] [-f]"),
    40  		},
    41  		Flags: fs,
    42  	}
    43  }
    44  
    45  func (cmd *DeleteSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    46  	if len(fc.Args()) != 1 {
    47  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-space"))
    48  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    49  	}
    50  
    51  	cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0])
    52  
    53  	reqs := []requirements.Requirement{
    54  		requirementsFactory.NewLoginRequirement(),
    55  	}
    56  
    57  	if fc.String("o") == "" {
    58  		reqs = append(reqs, requirementsFactory.NewTargetedOrgRequirement())
    59  		reqs = append(reqs, cmd.spaceReq)
    60  	}
    61  
    62  	return reqs, nil
    63  }
    64  
    65  func (cmd *DeleteSpace) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    66  	cmd.ui = deps.UI
    67  	cmd.config = deps.Config
    68  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    69  	cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
    70  	return cmd
    71  }
    72  func (cmd *DeleteSpace) Execute(c flags.FlagContext) error {
    73  	var space models.Space
    74  
    75  	spaceName := c.Args()[0]
    76  	orgName := c.String("o")
    77  
    78  	if orgName == "" {
    79  		space = cmd.spaceReq.GetSpace()
    80  
    81  		orgName = cmd.config.OrganizationFields().Name
    82  	} else {
    83  		org, err := cmd.orgRepo.FindByName(orgName)
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		space, err = cmd.spaceRepo.FindByNameInOrg(spaceName, org.GUID)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	}
    93  
    94  	if !c.Bool("f") {
    95  		if !cmd.ui.ConfirmDelete(T("space"), spaceName) {
    96  			return nil
    97  		}
    98  	}
    99  
   100  	cmd.ui.Say(T("Deleting space {{.TargetSpace}} in org {{.TargetOrg}} as {{.CurrentUser}}...",
   101  		map[string]interface{}{
   102  			"TargetSpace": terminal.EntityNameColor(spaceName),
   103  			"TargetOrg":   terminal.EntityNameColor(orgName),
   104  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
   105  		}))
   106  
   107  	err := cmd.spaceRepo.Delete(space.GUID)
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	cmd.ui.Ok()
   113  
   114  	if cmd.config.SpaceFields().GUID == space.GUID {
   115  		cmd.config.SetSpaceFields(models.SpaceFields{})
   116  		cmd.ui.Say(T("TIP: No space targeted, use '{{.CfTargetCommand}}' to target a space.",
   117  			map[string]interface{}{"CfTargetCommand": cf.Name + " target -s"}))
   118  	}
   119  
   120  	return nil
   121  }