forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_keys.go
89 lines (72 loc) · 2.84 KB
/
service_keys.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
package servicekey
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type ServiceKeys struct {
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
serviceInstanceRequirement requirements.ServiceInstanceRequirement
}
func init() {
command_registry.Register(&ServiceKeys{})
}
func (cmd *ServiceKeys) MetaData() command_registry.CommandMetadata {
return command_registry.CommandMetadata{
Name: "service-keys",
ShortName: "sk",
Description: T("List keys for a service instance"),
Usage: T(`CF_NAME service-keys SERVICE_INSTANCE
EXAMPLE:
CF_NAME service-keys mydb`),
}
}
func (cmd *ServiceKeys) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("service-keys"))
}
loginRequirement := requirementsFactory.NewLoginRequirement()
cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()
reqs := []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}
return reqs, nil
}
func (cmd *ServiceKeys) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
cmd.serviceKeyRepo = deps.RepoLocator.GetServiceKeyRepository()
return cmd
}
func (cmd *ServiceKeys) Execute(c flags.FlagContext) {
serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
cmd.ui.Say(T("Getting keys for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
serviceKeys, err := cmd.serviceKeyRepo.ListServiceKeys(serviceInstance.Guid)
if err != nil {
cmd.ui.Failed(err.Error())
return
}
table := cmd.ui.Table([]string{T("name")})
for _, serviceKey := range serviceKeys {
table.Add(serviceKey.Fields.Name)
}
if len(serviceKeys) == 0 {
cmd.ui.Say(T("No service key for service instance {{.ServiceInstanceName}}",
map[string]interface{}{"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
return
} else {
cmd.ui.Say("")
table.Print()
}
}