github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/integration/helpers/app_instance_table.go (about)

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