github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/serviceauthtoken/delete_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/flags" 8 . "code.cloudfoundry.org/cli/cf/i18n" 9 10 "code.cloudfoundry.org/cli/cf/api" 11 "code.cloudfoundry.org/cli/cf/commandregistry" 12 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 13 "code.cloudfoundry.org/cli/cf/errors" 14 "code.cloudfoundry.org/cli/cf/requirements" 15 "code.cloudfoundry.org/cli/cf/terminal" 16 ) 17 18 type DeleteServiceAuthTokenFields struct { 19 ui terminal.UI 20 config coreconfig.Reader 21 authTokenRepo api.ServiceAuthTokenRepository 22 } 23 24 func init() { 25 commandregistry.Register(&DeleteServiceAuthTokenFields{}) 26 } 27 28 func (cmd *DeleteServiceAuthTokenFields) MetaData() commandregistry.CommandMetadata { 29 fs := make(map[string]flags.FlagSet) 30 fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")} 31 32 return commandregistry.CommandMetadata{ 33 Name: "delete-service-auth-token", 34 Description: T("Delete a service auth token"), 35 Usage: []string{ 36 T("CF_NAME delete-service-auth-token LABEL PROVIDER [-f]"), 37 }, 38 Flags: fs, 39 } 40 } 41 42 func (cmd *DeleteServiceAuthTokenFields) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 43 if len(fc.Args()) != 2 { 44 cmd.ui.Failed(T("Incorrect Usage. Requires LABEL, PROVIDER as arguments\n\n") + commandregistry.Commands.CommandUsage("delete-service-auth-token")) 45 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 46 } 47 48 reqs := []requirements.Requirement{ 49 requirementsFactory.NewLoginRequirement(), 50 requirementsFactory.NewMaxAPIVersionRequirement( 51 "delete-service-auth-token", 52 cf.ServiceAuthTokenMaximumAPIVersion, 53 ), 54 } 55 56 return reqs, nil 57 } 58 59 func (cmd *DeleteServiceAuthTokenFields) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 60 cmd.ui = deps.UI 61 cmd.config = deps.Config 62 cmd.authTokenRepo = deps.RepoLocator.GetServiceAuthTokenRepository() 63 return cmd 64 } 65 66 func (cmd *DeleteServiceAuthTokenFields) Execute(c flags.FlagContext) error { 67 tokenLabel := c.Args()[0] 68 tokenProvider := c.Args()[1] 69 70 if c.Bool("f") == false { 71 if !cmd.ui.ConfirmDelete(T("service auth token"), fmt.Sprintf("%s %s", tokenLabel, tokenProvider)) { 72 return nil 73 } 74 } 75 76 cmd.ui.Say(T("Deleting service auth token as {{.CurrentUser}}", 77 map[string]interface{}{ 78 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 79 })) 80 token, err := cmd.authTokenRepo.FindByLabelAndProvider(tokenLabel, tokenProvider) 81 82 switch err.(type) { 83 case nil: 84 case *errors.ModelNotFoundError: 85 cmd.ui.Ok() 86 cmd.ui.Warn(T("Service Auth Token {{.Label}} {{.Provider}} does not exist.", map[string]interface{}{"Label": tokenLabel, "Provider": tokenProvider})) 87 return nil 88 default: 89 return err 90 } 91 92 err = cmd.authTokenRepo.Delete(token) 93 if err != nil { 94 return err 95 } 96 97 cmd.ui.Ok() 98 return nil 99 }