github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/organization/delete_org.go (about)

     1  package organization
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/organizations"
     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/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type DeleteOrg struct {
    18  	ui      terminal.UI
    19  	config  coreconfig.ReadWriter
    20  	orgRepo organizations.OrganizationRepository
    21  	orgReq  requirements.OrganizationRequirement
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&DeleteOrg{})
    26  }
    27  
    28  func (cmd *DeleteOrg) MetaData() commandregistry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "delete-org",
    34  		Description: T("Delete an org"),
    35  		Usage: []string{
    36  			T("CF_NAME delete-org ORG [-f]"),
    37  		},
    38  		Flags: fs,
    39  	}
    40  }
    41  
    42  func (cmd *DeleteOrg) 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-org"))
    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 *DeleteOrg) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    56  	cmd.ui = deps.UI
    57  	cmd.config = deps.Config
    58  	cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *DeleteOrg) Execute(c flags.FlagContext) error {
    63  	orgName := c.Args()[0]
    64  
    65  	if !c.Bool("f") {
    66  		if !cmd.ui.ConfirmDeleteWithAssociations(T("org"), orgName) {
    67  			return nil
    68  		}
    69  	}
    70  
    71  	cmd.ui.Say(T("Deleting org {{.OrgName}} as {{.Username}}...",
    72  		map[string]interface{}{
    73  			"OrgName":  terminal.EntityNameColor(orgName),
    74  			"Username": terminal.EntityNameColor(cmd.config.Username())}))
    75  
    76  	org, err := cmd.orgRepo.FindByName(orgName)
    77  
    78  	switch err.(type) {
    79  	case nil:
    80  	case *errors.ModelNotFoundError:
    81  		cmd.ui.Ok()
    82  		cmd.ui.Warn(T("Org {{.OrgName}} does not exist.",
    83  			map[string]interface{}{"OrgName": orgName}))
    84  		return nil
    85  	default:
    86  		return err
    87  	}
    88  
    89  	err = cmd.orgRepo.Delete(org.GUID)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	if org.GUID == cmd.config.OrganizationFields().GUID {
    95  		cmd.config.SetOrganizationFields(models.OrganizationFields{})
    96  		cmd.config.SetSpaceFields(models.SpaceFields{})
    97  	}
    98  
    99  	cmd.ui.Ok()
   100  	return nil
   101  }