github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/update_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 UpdateServiceBroker struct {
    16  	ui     terminal.UI
    17  	config coreconfig.Reader
    18  	repo   api.ServiceBrokerRepository
    19  }
    20  
    21  func init() {
    22  	commandregistry.Register(&UpdateServiceBroker{})
    23  }
    24  
    25  func (cmd *UpdateServiceBroker) MetaData() commandregistry.CommandMetadata {
    26  	return commandregistry.CommandMetadata{
    27  		Name:        "update-service-broker",
    28  		Description: T("Update a service broker"),
    29  		Usage: []string{
    30  			T("CF_NAME update-service-broker SERVICE_BROKER USERNAME PASSWORD URL"),
    31  		},
    32  	}
    33  }
    34  
    35  func (cmd *UpdateServiceBroker) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    36  	if len(fc.Args()) != 4 {
    37  		cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_BROKER, USERNAME, PASSWORD, URL as arguments\n\n") + commandregistry.Commands.CommandUsage("update-service-broker"))
    38  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 4)
    39  	}
    40  
    41  	reqs := []requirements.Requirement{
    42  		requirementsFactory.NewLoginRequirement(),
    43  	}
    44  
    45  	return reqs, nil
    46  }
    47  
    48  func (cmd *UpdateServiceBroker) 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 *UpdateServiceBroker) 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("Updating service broker {{.Name}} as {{.Username}}...",
    62  		map[string]interface{}{
    63  			"Name":     terminal.EntityNameColor(serviceBroker.Name),
    64  			"Username": terminal.EntityNameColor(cmd.config.Username())}))
    65  
    66  	serviceBroker.Username = c.Args()[1]
    67  	serviceBroker.Password = c.Args()[2]
    68  	serviceBroker.URL = c.Args()[3]
    69  
    70  	err = cmd.repo.Update(serviceBroker)
    71  
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	cmd.ui.Ok()
    77  	return nil
    78  }