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