Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 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 | |
| 16 | package format |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "reflect" |
| 21 | |
| 22 | "github.com/guumaster/tablewriter" |
| 23 | ) |
| 24 | |
| 25 | // To append rows for database based commands. |
| 26 | func parseAndAppendRowNew(t *tablewriter.Table, key string, value interface{}, tab string, row *[][]string) { |
| 27 | switch reflect.ValueOf(value).Kind() { |
| 28 | case reflect.Map: |
| 29 | t.Append([]string{tab + key, ""}) |
| 30 | newValue := value.(map[string]interface{}) |
| 31 | pl := sortData(newValue) |
| 32 | for i := range pl { |
| 33 | parseAndAppendRowNew(t, pl[i].Key, pl[i].Value, tab+" ", row) |
| 34 | } |
| 35 | default: |
| 36 | switch (value).(type) { |
| 37 | case float64: |
| 38 | t.Append([]string{tab + key, fmt.Sprintf("%d", int(value.(float64)))}) |
| 39 | case uint64, uint32: |
| 40 | t.Append([]string{tab + key, fmt.Sprintf("%d", value)}) |
| 41 | default: |
| 42 | t.Append([]string{tab + key, fmt.Sprintf("%v", value)}) |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // parseAndAppendRowNewForAPICmd to append rows for api based commands. |
| 49 | func parseAndAppendRowNewForAPICmd(t *tablewriter.Table, key string, field string, value bool, tab string, row *[][][]string) { |
| 50 | t.Append([]string{tab + string(key), fmt.Sprintf("%v", field), fmt.Sprintf("%v", value)}) |
| 51 | } |
| 52 | |
| 53 | // parseAndAppendRowNewForIcmpAPICmd to append rows for api based commands. |
| 54 | func parseAndAppendRowNewForIcmpAPICmd(t *tablewriter.Table, key string, field string, value int, tab string, row *[][][]string) { |
| 55 | t.Append([]string{tab + string(key), fmt.Sprintf("%v", field), fmt.Sprintf("%d", value)}) |
| 56 | } |
| 57 | |
| 58 | // parseAndAppendHeaderRowForAPICmd adding table-header for api based commands. |
| 59 | func parseAndAppendHeaderRowForAPICmd(t *tablewriter.Table, deviceID string, vlan string, status string, tab string, row *[][][]string) { |
| 60 | t.Append([]string{tab + string(deviceID), fmt.Sprintf("%v", vlan), fmt.Sprintf("%v", status)}) |
| 61 | } |
| 62 | |
| 63 | // parseAndAppendRowNewForPortAPI to append row for cache port command. |
| 64 | func parseAndAppendRowNewForPortAPI(t *tablewriter.Table, key string, id uint32, name string, device string, ptype string, state string, count uint32, tab string, row *[][][][][][]string) { |
| 65 | t.Append([]string{tab + string(key), fmt.Sprintf("%v", id), fmt.Sprintf("%v", name), fmt.Sprintf("%v", device), fmt.Sprintf("%v", ptype), fmt.Sprintf("%v", state), fmt.Sprintf("%v", count)}) |
| 66 | } |
| 67 | |
| 68 | // parseAndAppendHeaderRowForPortAPI adding table header for port cache command. |
| 69 | func parseAndAppendHeaderRowForPortAPI(t *tablewriter.Table, key string, id string, name string, device string, ptype string, state string, count string, tab string, row *[][][][][][]string) { |
| 70 | t.Append([]string{tab + string(key), fmt.Sprintf("%v", id), fmt.Sprintf("%v", name), fmt.Sprintf("%v", device), fmt.Sprintf("%v", ptype), fmt.Sprintf("%v", state), fmt.Sprintf("%v", count)}) |
| 71 | } |
| 72 | |
| 73 | // parseAndAppendHeaderRowForPonPortAPI adding table header for PON port command. |
| 74 | func parseAndAppendHeaderRowForPonPortAPI(t *tablewriter.Table, key string, id string, kpiFlag string, maxActChan string, currChan string, tab string, row *[][][][][][]string) { |
| 75 | t.Append([]string{tab + string(key), fmt.Sprintf("%v", id), fmt.Sprintf("%v", kpiFlag), fmt.Sprintf("%v", maxActChan), fmt.Sprintf("%v", currChan)}) |
| 76 | } |
| 77 | |
| 78 | // parseAndAppendNewRowForPonPortAPI adding table row for PON port command. |
| 79 | func parseAndAppendNewRowForPonPortAPI(t *tablewriter.Table, key string, id uint32, kpiFlag bool, maxActChan uint32, currChan uint32, tab string, row *[][][][][][]string) { |
| 80 | t.Append([]string{tab + string(key), fmt.Sprintf("%v", id), fmt.Sprintf("%v", kpiFlag), fmt.Sprintf("%v", maxActChan), fmt.Sprintf("%v", currChan)}) |
| 81 | } |
| 82 | |
| 83 | // parseAndAppendHeaderRowForTaskList adding table-header for task list command. |
| 84 | func parseAndAppendHeaderRowForTaskList(t *tablewriter.Table, deviceID string, position string, taskID string, taskName string, time string, tab string, row *[][][][][]string) { |
| 85 | t.Append([]string{tab + string(deviceID), fmt.Sprintf("%v", position), fmt.Sprintf("%v", taskID), fmt.Sprintf("%v", taskName), fmt.Sprintf("%v", time)}) |
| 86 | } |
| 87 | |
| 88 | // parseAndAppendRowNewForTaskList adding new row for task list command. |
| 89 | func parseAndAppendRowNewForTaskList(t *tablewriter.Table, deviceID string, position int, taskID string, taskName string, time string, tab string, row *[][][][][]string) { |
| 90 | t.Append([]string{tab + string(deviceID), fmt.Sprintf("%v", position), fmt.Sprintf("%v", taskID), fmt.Sprintf("%v", taskName), fmt.Sprintf("%v", time)}) |
| 91 | } |
| 92 | |
| 93 | // parseAndAppendRowNewForDeviceInfo adding new row for device info command. |
| 94 | func parseAndAppendRowNewForDeviceInfo(t *tablewriter.Table, deviceID string, serialNum string, state string, tab string, row *[][][][][]string) { |
| 95 | t.Append([]string{tab + string(deviceID), fmt.Sprintf("%v", serialNum), fmt.Sprintf("%v", state)}) |
| 96 | } |
| 97 | |
| 98 | // parseAndAppendRowNewForDhcpCmd adding new row for api based dhcp command. |
| 99 | func parseAndAppendRowNewForDhcpCmd(t *tablewriter.Table, deviceID string, port string, svlan string, cvlan string, univlan string, macAddress string, ipAddress string, ipv6Address string, state string, statev6 string, leaseTime string, leaseTimev6 string, tab string, row *[][][][][][][][][][][][]string) { |
| 100 | t.Append([]string{tab + string(deviceID), fmt.Sprintf("%v", port), fmt.Sprintf("%v", svlan), fmt.Sprintf("%v", cvlan), fmt.Sprintf("%v", univlan), fmt.Sprintf("%v", macAddress), fmt.Sprintf("%v", ipAddress), fmt.Sprintf("%v", ipv6Address), fmt.Sprintf("%v", state), fmt.Sprintf("%v", statev6), fmt.Sprintf("%v", leaseTime), fmt.Sprintf("%v", leaseTimev6)}) |
| 101 | } |