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

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