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