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