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

     1  package serviceauthtoken
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type UpdateServiceAuthTokenFields struct {
    17  	ui            terminal.UI
    18  	config        coreconfig.Reader
    19  	authTokenRepo api.ServiceAuthTokenRepository
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&UpdateServiceAuthTokenFields{})
    24  }
    25  
    26  func (cmd *UpdateServiceAuthTokenFields) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "update-service-auth-token",
    29  		Description: T("Update a service auth token"),
    30  		Usage: []string{
    31  			T("CF_NAME update-service-auth-token LABEL PROVIDER TOKEN"),
    32  		},
    33  	}
    34  }
    35  
    36  func (cmd *UpdateServiceAuthTokenFields) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    37  	if len(fc.Args()) != 3 {
    38  		cmd.ui.Failed(T("Incorrect Usage. Requires LABEL, PROVIDER and TOKEN as arguments\n\n") + commandregistry.Commands.CommandUsage("update-service-auth-token"))
    39  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 3)
    40  	}
    41  
    42  	reqs := []requirements.Requirement{
    43  		requirementsFactory.NewLoginRequirement(),
    44  		requirementsFactory.NewMaxAPIVersionRequirement(
    45  			"update-service-auth-token",
    46  			cf.ServiceAuthTokenMaximumAPIVersion,
    47  		),
    48  	}
    49  
    50  	return reqs, nil
    51  }
    52  
    53  func (cmd *UpdateServiceAuthTokenFields) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    54  	cmd.ui = deps.UI
    55  	cmd.config = deps.Config
    56  	cmd.authTokenRepo = deps.RepoLocator.GetServiceAuthTokenRepository()
    57  	return cmd
    58  }
    59  
    60  func (cmd *UpdateServiceAuthTokenFields) Execute(c flags.FlagContext) error {
    61  	cmd.ui.Say(T("Updating service auth token as {{.CurrentUser}}...", map[string]interface{}{"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
    62  
    63  	serviceAuthToken, err := cmd.authTokenRepo.FindByLabelAndProvider(c.Args()[0], c.Args()[1])
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	serviceAuthToken.Token = c.Args()[2]
    69  
    70  	err = cmd.authTokenRepo.Update(serviceAuthToken)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	cmd.ui.Ok()
    76  	return nil
    77  }