github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/organization/rename_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/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  type RenameOrg struct {
    16  	ui      terminal.UI
    17  	config  coreconfig.ReadWriter
    18  	orgRepo organizations.OrganizationRepository
    19  	orgReq  requirements.OrganizationRequirement
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&RenameOrg{})
    24  }
    25  
    26  func (cmd *RenameOrg) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "rename-org",
    29  		Description: T("Rename an org"),
    30  		Usage: []string{
    31  			T("CF_NAME rename-org ORG NEW_ORG"),
    32  		},
    33  	}
    34  }
    35  
    36  func (cmd *RenameOrg) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    37  	if len(fc.Args()) != 2 {
    38  		cmd.ui.Failed(T("Incorrect Usage. Requires old org name, new org name as arguments\n\n") + commandregistry.Commands.CommandUsage("rename-org"))
    39  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    40  	}
    41  
    42  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[0])
    43  
    44  	reqs := []requirements.Requirement{
    45  		requirementsFactory.NewLoginRequirement(),
    46  		cmd.orgReq,
    47  	}
    48  
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *RenameOrg) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.config = deps.Config
    55  	cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
    56  	return cmd
    57  }
    58  
    59  func (cmd *RenameOrg) Execute(c flags.FlagContext) error {
    60  	org := cmd.orgReq.GetOrganization()
    61  	newName := c.Args()[1]
    62  
    63  	cmd.ui.Say(T("Renaming org {{.OrgName}} to {{.NewName}} as {{.Username}}...",
    64  		map[string]interface{}{
    65  			"OrgName":  terminal.EntityNameColor(org.Name),
    66  			"NewName":  terminal.EntityNameColor(newName),
    67  			"Username": terminal.EntityNameColor(cmd.config.Username())}))
    68  
    69  	err := cmd.orgRepo.Rename(org.GUID, newName)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	cmd.ui.Ok()
    74  
    75  	if org.GUID == cmd.config.OrganizationFields().GUID {
    76  		org.Name = newName
    77  		cmd.config.SetOrganizationFields(org.OrganizationFields)
    78  	}
    79  	return nil
    80  }