github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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, err := regexp.MatchString(`\Atype:([^:]+)\z`, row)
    37  			if err != nil {
    38  				panic(err)
    39  			}
    40  			if ok {
    41  				foundFirstProcess = true
    42  			} else {
    43  				continue
    44  			}
    45  		}
    46  
    47  		if row == "" {
    48  			continue
    49  		}
    50  
    51  		if strings.HasPrefix(row, "#") {
    52  			// instance row
    53  			columns := splitColumns(row)
    54  			details := ""
    55  			if len(columns) >= 7 {
    56  				details = columns[6]
    57  			}
    58  
    59  			instanceRow := AppInstanceRow{
    60  				Index:   columns[0],
    61  				State:   columns[1],
    62  				Since:   columns[2],
    63  				CPU:     columns[3],
    64  				Memory:  columns[4],
    65  				Disk:    columns[5],
    66  				Details: details,
    67  			}
    68  			lastProcessIndex := len(appTable.Processes) - 1
    69  			appTable.Processes[lastProcessIndex].Instances = append(
    70  				appTable.Processes[lastProcessIndex].Instances,
    71  				instanceRow,
    72  			)
    73  
    74  		} else if strings.HasPrefix(row, "type:") {
    75  			appTable.Processes = append(appTable.Processes, AppProcessTable{
    76  				Type: strings.TrimSpace(strings.TrimPrefix(row, "type:")),
    77  			})
    78  		} else if strings.HasPrefix(row, "instances:") {
    79  			lpi := len(appTable.Processes) - 1
    80  			iVal := strings.TrimSpace(strings.TrimPrefix(row, "instances:"))
    81  			appTable.Processes[lpi].InstanceCount = iVal
    82  		} else if 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  		} else {
    87  			// column headers
    88  			continue
    89  		}
    90  
    91  	}
    92  
    93  	return appTable
    94  }
    95  
    96  func splitColumns(row string) []string {
    97  	// uses 3 spaces between columns
    98  	return regexp.MustCompile(`\s{3,}`).Split(strings.TrimSpace(row), -1)
    99  }