github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/serviceauthtoken/update_service_auth_token.go (about)

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