forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains.go
94 lines (80 loc) · 2.41 KB
/
domains.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
package domain
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type ListDomains struct {
ui terminal.UI
config configuration.Reader
orgReq requirements.TargetedOrgRequirement
domainRepo api.DomainRepository
}
func NewListDomains(ui terminal.UI, config configuration.Reader, domainRepo api.DomainRepository) (cmd *ListDomains) {
cmd = new(ListDomains)
cmd.ui = ui
cmd.config = config
cmd.domainRepo = domainRepo
return
}
func (command *ListDomains) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "domains",
Description: "List domains in the target org",
Usage: "CF_NAME domains",
}
}
func (cmd *ListDomains) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) > 0 {
err = errors.New("Incorrect Usage")
cmd.ui.FailWithUsage(c)
return
}
cmd.orgReq = requirementsFactory.NewTargetedOrgRequirement()
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
cmd.orgReq,
}
return
}
func (cmd *ListDomains) Run(c *cli.Context) {
org := cmd.orgReq.GetOrganizationFields()
cmd.ui.Say("Getting domains in org %s as %s...",
terminal.EntityNameColor(org.Name),
terminal.EntityNameColor(cmd.config.Username()),
)
domains := cmd.fetchAllDomains(org.Guid)
cmd.printDomainsTable(domains)
if len(domains) == 0 {
cmd.ui.Say("No domains found")
}
}
func (cmd *ListDomains) fetchAllDomains(orgGuid string) (domains []models.DomainFields) {
apiErr := cmd.domainRepo.ListDomainsForOrg(orgGuid, func(domain models.DomainFields) bool {
domains = append(domains, domain)
return true
})
if apiErr != nil {
cmd.ui.Failed("Failed fetching domains.\n%s", apiErr.Error())
}
return
}
func (cmd *ListDomains) printDomainsTable(domains []models.DomainFields) {
table := cmd.ui.Table([]string{"name", "status"})
for _, domain := range domains {
if domain.Shared {
table.Add([]string{domain.Name, "shared"})
}
}
for _, domain := range domains {
if !domain.Shared {
table.Add([]string{domain.Name, "owned"})
}
}
table.Print()
}