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