github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/app_instance_table.go (about) 1 package helpers 2 3 import ( 4 "regexp" 5 "strings" 6 ) 7 8 // AppInstanceRow represents an instance of a V3 app's process, 9 // as displayed in the 'cf app' output. 10 type AppInstanceRow struct { 11 Index string 12 State string 13 Since string 14 CPU string 15 Memory string 16 Disk string 17 Details string 18 } 19 20 // AppProcessTable represents a process of a V3 app, as displayed in the 'cf 21 // app' output. 22 type AppProcessTable struct { 23 Type string 24 Sidecars string 25 InstanceCount string 26 MemUsage string 27 Instances []AppInstanceRow 28 } 29 30 // AppTable represents a V3 app as a collection of processes, 31 // as displayed in the 'cf app' output. 32 type AppTable struct { 33 Processes []AppProcessTable 34 } 35 36 // ParseV3AppProcessTable parses bytes from 'cf app' stdout into an AppTable. 37 func ParseV3AppProcessTable(input []byte) AppTable { 38 appTable := AppTable{} 39 40 rows := strings.Split(string(input), "\n") 41 foundFirstProcess := false 42 for _, row := range rows { 43 if !foundFirstProcess { 44 ok := regexp.MustCompile(`\Atype:([^:]+)\z`).Match([]byte(row)) 45 if ok { 46 foundFirstProcess = true 47 } else { 48 continue 49 } 50 } 51 52 if row == "" { 53 continue 54 } 55 56 switch { 57 case strings.HasPrefix(row, "#"): 58 // instance row 59 columns := splitColumns(row) 60 details := "" 61 if len(columns) >= 7 { 62 details = columns[6] 63 } 64 65 instanceRow := AppInstanceRow{ 66 Index: columns[0], 67 State: columns[1], 68 Since: columns[2], 69 CPU: columns[3], 70 Memory: columns[4], 71 Disk: columns[5], 72 Details: details, 73 } 74 lastProcessIndex := len(appTable.Processes) - 1 75 appTable.Processes[lastProcessIndex].Instances = append( 76 appTable.Processes[lastProcessIndex].Instances, 77 instanceRow, 78 ) 79 80 case strings.HasPrefix(row, "type:"): 81 appTable.Processes = append(appTable.Processes, AppProcessTable{ 82 Type: strings.TrimSpace(strings.TrimPrefix(row, "type:")), 83 }) 84 85 case strings.HasPrefix(row, "instances:"): 86 lpi := len(appTable.Processes) - 1 87 iVal := strings.TrimSpace(strings.TrimPrefix(row, "instances:")) 88 appTable.Processes[lpi].InstanceCount = iVal 89 90 case strings.HasPrefix(row, "memory usage:"): 91 lpi := len(appTable.Processes) - 1 92 mVal := strings.TrimSpace(strings.TrimPrefix(row, "memory usage:")) 93 appTable.Processes[lpi].MemUsage = mVal 94 95 default: 96 // column headers 97 continue 98 } 99 100 } 101 102 return appTable 103 } 104 105 func splitColumns(row string) []string { 106 // uses 3 spaces between columns 107 return regexp.MustCompile(`\s{3,}`).Split(strings.TrimSpace(row), -1) 108 }