github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/securitygroup/security_groups.go (about) 1 package securitygroup 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/flags" 7 . "code.cloudfoundry.org/cli/cf/i18n" 8 9 "code.cloudfoundry.org/cli/cf/api/securitygroups" 10 "code.cloudfoundry.org/cli/cf/commandregistry" 11 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 12 "code.cloudfoundry.org/cli/cf/models" 13 "code.cloudfoundry.org/cli/cf/requirements" 14 "code.cloudfoundry.org/cli/cf/terminal" 15 ) 16 17 type SecurityGroups struct { 18 ui terminal.UI 19 securityGroupRepo securitygroups.SecurityGroupRepo 20 configRepo coreconfig.Reader 21 } 22 23 func init() { 24 commandregistry.Register(&SecurityGroups{}) 25 } 26 27 func (cmd *SecurityGroups) MetaData() commandregistry.CommandMetadata { 28 return commandregistry.CommandMetadata{ 29 Name: "security-groups", 30 Description: T("List all security groups"), 31 Usage: []string{ 32 "CF_NAME security-groups", 33 }, 34 } 35 } 36 37 func (cmd *SecurityGroups) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 38 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 39 T("No argument required"), 40 func() bool { 41 return len(fc.Args()) != 0 42 }, 43 ) 44 45 reqs := []requirements.Requirement{ 46 usageReq, 47 requirementsFactory.NewLoginRequirement(), 48 } 49 return reqs, nil 50 } 51 52 func (cmd *SecurityGroups) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 53 cmd.ui = deps.UI 54 cmd.configRepo = deps.Config 55 cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository() 56 return cmd 57 } 58 59 func (cmd *SecurityGroups) Execute(c flags.FlagContext) error { 60 cmd.ui.Say(T("Getting security groups as {{.username}}", 61 map[string]interface{}{ 62 "username": terminal.EntityNameColor(cmd.configRepo.Username()), 63 })) 64 65 securityGroups, err := cmd.securityGroupRepo.FindAll() 66 if err != nil { 67 return err 68 } 69 70 cmd.ui.Ok() 71 cmd.ui.Say("") 72 73 if len(securityGroups) == 0 { 74 cmd.ui.Say(T("No security groups")) 75 return nil 76 } 77 78 table := cmd.ui.Table([]string{"", T("Name"), T("Organization"), T("Space")}) 79 80 for index, securityGroup := range securityGroups { 81 if len(securityGroup.Spaces) > 0 { 82 cmd.printSpaces(table, securityGroup, index) 83 } else { 84 table.Add(fmt.Sprintf("#%d", index), securityGroup.Name, "", "") 85 } 86 } 87 err = table.Print() 88 if err != nil { 89 return err 90 } 91 return nil 92 } 93 94 type table interface { 95 Add(row ...string) 96 Print() error 97 } 98 99 func (cmd SecurityGroups) printSpaces(table table, securityGroup models.SecurityGroup, index int) { 100 outputtedIndex := false 101 102 for _, space := range securityGroup.Spaces { 103 if !outputtedIndex { 104 table.Add(fmt.Sprintf("#%d", index), securityGroup.Name, space.Organization.Name, space.Name) 105 outputtedIndex = true 106 } else { 107 table.Add("", securityGroup.Name, space.Organization.Name, space.Name) 108 } 109 } 110 }