go.ligato.io/vpp-agent/v3@v3.5.0/tests/e2e/e2etest/util.go (about)

     1  //  Copyright (c) 2020 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package e2etest
    16  
    17  import "strings"
    18  
    19  // parseVPPTable parses table returned by one of the VPP show commands.
    20  func ParseVPPTable(table string) (parsed []map[string]string) {
    21  	lines := strings.Split(table, "\r\n")
    22  	if len(lines) == 0 {
    23  		return
    24  	}
    25  	head := lines[0]
    26  	rows := lines[1:]
    27  
    28  	var columns []string
    29  	for _, column := range strings.Split(head, " ") {
    30  		if column != "" {
    31  			columns = append(columns, column)
    32  		}
    33  	}
    34  	for _, row := range rows {
    35  		parsedRow := make(map[string]string)
    36  		i := 0
    37  		for _, cell := range strings.Split(row, " ") {
    38  			if cell == "" {
    39  				continue
    40  			}
    41  			if i >= len(columns) {
    42  				break
    43  			}
    44  			parsedRow[columns[i]] = cell
    45  			i++
    46  		}
    47  		if len(parsedRow) > 0 {
    48  			parsed = append(parsed, parsedRow)
    49  		}
    50  	}
    51  	return
    52  }