github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/services_command.go (about) 1 package v7 2 3 import ( 4 "strings" 5 6 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 7 8 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/types" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 11 ) 12 13 type ServicesCommand struct { 14 BaseCommand 15 16 OmitApps bool `long:"no-apps" description:"Do not retrieve bound apps information."` 17 relatedCommands interface{} `related_commands:"create-service, marketplace"` 18 } 19 20 func (cmd ServicesCommand) Execute(args []string) error { 21 if err := cmd.SharedActor.CheckTarget(true, true); err != nil { 22 return err 23 } 24 25 if err := cmd.displayMessage(); err != nil { 26 return err 27 } 28 29 instances, warnings, err := cmd.Actor.GetServiceInstancesForSpace(cmd.Config.TargetedSpace().GUID, cmd.OmitApps) 30 cmd.UI.DisplayWarnings(warnings) 31 if err != nil { 32 return err 33 } 34 35 cmd.displayTable(instances) 36 return nil 37 } 38 39 func (cmd ServicesCommand) Usage() string { 40 return "CF_NAME services" 41 } 42 43 func (cmd ServicesCommand) displayMessage() error { 44 user, err := cmd.Actor.GetCurrentUser() 45 if err != nil { 46 return err 47 } 48 49 cmd.UI.DisplayTextWithFlavor("Getting service instances in org {{.OrgName}} / space {{.SpaceName}} as {{.UserName}}...", map[string]interface{}{ 50 "OrgName": cmd.Config.TargetedOrganization().Name, 51 "SpaceName": cmd.Config.TargetedSpace().Name, 52 "UserName": user.Name, 53 }) 54 cmd.UI.DisplayNewline() 55 56 return nil 57 } 58 59 func (cmd ServicesCommand) displayTable(instances []v7action.ServiceInstance) { 60 if len(instances) == 0 { 61 cmd.UI.DisplayText("No service instances found.") 62 return 63 } 64 65 table := NewServicesTable(false, cmd.OmitApps) 66 67 for _, si := range instances { 68 table.AppendRow(si) 69 } 70 cmd.UI.DisplayTableWithHeader("", table.table, ui.DefaultTableSpacePadding) 71 } 72 73 func upgradeAvailableString(u types.OptionalBoolean) string { 74 switch { 75 case u.IsSet && u.Value: 76 return "yes" 77 case u.IsSet: 78 return "no" 79 default: 80 return "" 81 } 82 } 83 84 func serviceOfferingName(si v7action.ServiceInstance) string { 85 if si.Type == resources.UserProvidedServiceInstance { 86 return "user-provided" 87 } 88 return si.ServiceOfferingName 89 } 90 91 type ServicesTable struct { 92 table [][]string 93 short bool 94 showApps bool 95 } 96 97 func NewServicesTable(short bool, omitApps bool) *ServicesTable { 98 t := &ServicesTable{ 99 short: short, 100 showApps: !omitApps, 101 } 102 103 return t.withHeaders() 104 } 105 106 func (t *ServicesTable) withHeaders() *ServicesTable { 107 headers := []string{"name"} 108 if t.short { 109 if t.showApps { 110 headers = append(headers, "bound apps") 111 } 112 } else { 113 headers = append(headers, "offering", "plan") 114 if t.showApps { 115 headers = append(headers, "bound apps") 116 } 117 headers = append(headers, "last operation", "broker", "upgrade available") 118 } 119 t.table = [][]string{headers} 120 return t 121 } 122 123 func (t *ServicesTable) AppendRow(si v7action.ServiceInstance) { 124 row := []string{si.Name} 125 if t.short { 126 if t.showApps { 127 row = append(row, strings.Join(si.BoundApps, ", ")) 128 } 129 } else { 130 row = append(row, serviceOfferingName(si), si.ServicePlanName) 131 if t.showApps { 132 row = append(row, strings.Join(si.BoundApps, ", ")) 133 } 134 row = append(row, si.LastOperation, si.ServiceBrokerName, upgradeAvailableString(si.UpgradeAvailable)) 135 } 136 t.table = append(t.table, row) 137 }