github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/rename_service_broker.go (about)

     1  package servicebroker
     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/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 RenameServiceBroker struct {
    16  	ui     terminal.UI
    17  	config coreconfig.Reader
    18  	repo   api.ServiceBrokerRepository
    19  }
    20  
    21  func init() {
    22  	commandregistry.Register(&RenameServiceBroker{})
    23  }
    24  
    25  func (cmd *RenameServiceBroker) MetaData() commandregistry.CommandMetadata {
    26  	return commandregistry.CommandMetadata{
    27  		Name:        "rename-service-broker",
    28  		Description: T("Rename a service broker"),
    29  		Usage: []string{
    30  			T("CF_NAME rename-service-broker SERVICE_BROKER NEW_SERVICE_BROKER"),
    31  		},
    32  	}
    33  }
    34  
    35  func (cmd *RenameServiceBroker) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    36  	if len(fc.Args()) != 2 {
    37  		cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_BROKER, NEW_SERVICE_BROKER as arguments\n\n") + commandregistry.Commands.CommandUsage("rename-service-broker"))
    38  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    39  	}
    40  
    41  	reqs := []requirements.Requirement{
    42  		requirementsFactory.NewLoginRequirement(),
    43  	}
    44  
    45  	return reqs, nil
    46  }
    47  
    48  func (cmd *RenameServiceBroker) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    49  	cmd.ui = deps.UI
    50  	cmd.config = deps.Config
    51  	cmd.repo = deps.RepoLocator.GetServiceBrokerRepository()
    52  	return cmd
    53  }
    54  
    55  func (cmd *RenameServiceBroker) Execute(c flags.FlagContext) error {
    56  	serviceBroker, err := cmd.repo.FindByName(c.Args()[0])
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	cmd.ui.Say(T("Renaming service broker {{.OldName}} to {{.NewName}} as {{.Username}}",
    62  		map[string]interface{}{
    63  			"OldName":  terminal.EntityNameColor(serviceBroker.Name),
    64  			"NewName":  terminal.EntityNameColor(c.Args()[1]),
    65  			"Username": terminal.EntityNameColor(cmd.config.Username())}))
    66  
    67  	newName := c.Args()[1]
    68  
    69  	err = cmd.repo.Rename(serviceBroker.GUID, newName)
    70  
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	cmd.ui.Ok()
    76  	return nil
    77  }