github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  }
    16  
    17  type AppProcessTable struct {
    18  	Title     string
    19  	Instances []AppInstanceRow
    20  }
    21  
    22  type AppTable struct {
    23  	Processes []AppProcessTable
    24  }
    25  
    26  func ParseV3AppProcessTable(input []byte) AppTable {
    27  	appTable := AppTable{}
    28  
    29  	rows := strings.Split(string(input), "\n")
    30  	foundFirstProcess := false
    31  	for _, row := range rows {
    32  		if !foundFirstProcess {
    33  			ok, err := regexp.MatchString(`\A([^:]+):\d/\d\z`, row)
    34  			if err != nil {
    35  				panic(err)
    36  			}
    37  			if ok {
    38  				foundFirstProcess = true
    39  			} else {
    40  				continue
    41  			}
    42  		}
    43  
    44  		if row == "" {
    45  			continue
    46  		}
    47  
    48  		if strings.HasPrefix(row, "#") {
    49  			// instance row
    50  			columns := splitColumns(row)
    51  			instanceRow := AppInstanceRow{
    52  				Index:  columns[0],
    53  				State:  columns[1],
    54  				Since:  columns[2],
    55  				CPU:    columns[3],
    56  				Memory: columns[4],
    57  				Disk:   columns[5],
    58  			}
    59  			lastProcessIndex := len(appTable.Processes) - 1
    60  			appTable.Processes[lastProcessIndex].Instances = append(
    61  				appTable.Processes[lastProcessIndex].Instances,
    62  				instanceRow,
    63  			)
    64  
    65  		} else if !strings.HasPrefix(row, " ") {
    66  			// process title
    67  			appTable.Processes = append(appTable.Processes, AppProcessTable{
    68  				Title: row,
    69  			})
    70  		} else {
    71  			// column headers
    72  			continue
    73  		}
    74  
    75  	}
    76  
    77  	return appTable
    78  }
    79  
    80  func splitColumns(row string) []string {
    81  	// uses 3 spaces between columns
    82  	return regexp.MustCompile(`\s{3,}`).Split(strings.TrimSpace(row), -1)
    83  }