Skip to content

Commit

Permalink
replaced tablewriter with go-pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
hyposcaler-bot committed Nov 15, 2024
1 parent d442919 commit 6ea1998
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
40 changes: 24 additions & 16 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"slices"
"sort"

"github.com/olekukonko/tablewriter"
tableWriter "github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
Expand Down Expand Up @@ -130,12 +131,12 @@ func inspectFn(_ *cobra.Command, _ []string) error {
return err
}

func toTableData(det []types.ContainerDetails) [][]string {
tabData := make([][]string, 0, len(det))
func toTableData(det []types.ContainerDetails) []tableWriter.Row {
tabData := make([]tableWriter.Row, 0, len(det))
for i := range det {
d := &det[i]

tabRow := []string{fmt.Sprintf("%d", i+1)}
tabRow := tableWriter.Row{fmt.Sprintf("%d", i+1)}

if all {
tabRow = append(tabRow, d.LabPath, d.LabName)
Expand Down Expand Up @@ -213,35 +214,42 @@ func printContainerInspect(containers []runtime.GenericContainer, format string)

case "table":
tabData := toTableData(contDetails)
table := tablewriter.NewWriter(os.Stdout)
header := []string{
table := tableWriter.NewWriter()
table.SetOutputMirror(os.Stdout)
table.SetStyle(tableWriter.StyleLight)
table.Style().Format.Header = text.FormatTitle

prettyHeader := tableWriter.Row{
"Lab Name",
"Name",
"Container ID",
"Image",
"Kind",
"State",
"IPv4 Address",
"IPv6 Address",
}
"IPv6 Address"}

if wide {
header = slices.Insert(header, 1, "Owner")
prettyHeader = slices.Insert(prettyHeader, 1, "Owner")
}

if all {
table.SetHeader(append([]string{"#", "Topo Path"}, header...))
table.AppendHeader(append(tableWriter.Row{"#", "Topo Path"}, prettyHeader...))
} else {
table.SetHeader(append([]string{"#"}, header[1:]...))
table.AppendHeader(append(tableWriter.Row{"#"}, prettyHeader[1:]...))
}
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
// merge cells with lab name and topo file path
table.SetAutoMergeCellsByColumnIndex([]int{1, 2})
table.SetColumnConfigs([]tableWriter.ColumnConfig{
{Number: 2, AutoMerge: true},
{Number: 3, AutoMerge: true},
})
if wide {
table.SetAutoMergeCellsByColumnIndex([]int{1, 2, 3})
table.SetColumnConfigs([]tableWriter.ColumnConfig{
{Number: 2, AutoMerge: true},
})
}
table.AppendBulk(tabData)

table.AppendRows(tabData)
table.Render()

return nil
Expand Down
27 changes: 15 additions & 12 deletions cmd/tools_netem.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

"github.com/containernetworking/plugins/pkg/ns"
gotc "github.com/florianl/go-tc"
"github.com/olekukonko/tablewriter"
tableWriter "github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
Expand Down Expand Up @@ -164,8 +165,12 @@ func validateInput(_ *cobra.Command, _ []string) error {
}

func printImpairments(qdiscs []gotc.Object) {
table := tablewriter.NewWriter(os.Stdout)
header := []string{
table := tableWriter.NewWriter()
table.SetOutputMirror(os.Stdout)
table.SetStyle(tableWriter.StyleLight)
table.Style().Format.Header = text.FormatTitle

prettyHeader := tableWriter.Row{
"Interface",
"Delay",
"Jitter",
Expand All @@ -174,21 +179,19 @@ func printImpairments(qdiscs []gotc.Object) {
"Corruption",
}

table.SetHeader(header)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.AppendHeader(prettyHeader)

var rows [][]string
var prettyRows []tableWriter.Row

for _, qdisc := range qdiscs {
rows = append(rows, qdiscToTableData(qdisc))
prettyRows = append(prettyRows, qdiscToTableData(qdisc))
}

table.AppendBulk(rows)
table.AppendRows(prettyRows)
table.Render()
}

func qdiscToTableData(qdisc gotc.Object) []string {
func qdiscToTableData(qdisc gotc.Object) tableWriter.Row {
link, err := netlink.LinkByIndex(int(qdisc.Ifindex))
if err != nil {
log.Errorf("could not get netlink interface by index: %v", err)
Expand All @@ -204,7 +207,7 @@ func qdiscToTableData(qdisc gotc.Object) []string {
// return N/A values when netem is not set
// which is the case when qdisc is not set for an interface
if qdisc.Netem == nil {
return []string{
return tableWriter.Row{
ifDisplayName,
"N/A", // delay
"N/A", // jitter
Expand All @@ -226,7 +229,7 @@ func qdiscToTableData(qdisc gotc.Object) []string {
rate = strconv.Itoa(int(qdisc.Netem.Rate.Rate * 8 / 1000))
corruption = strconv.FormatFloat(float64(qdisc.Netem.Corrupt.Probability)/float64(math.MaxUint32)*100, 'f', 2, 64) + "%"

return []string{
return tableWriter.Row{
ifDisplayName,
delay,
jitter,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ require (
github.com/h2non/gock v1.2.0
github.com/hairyhenderson/gomplate/v3 v3.11.8
github.com/hashicorp/go-version v1.7.0
github.com/jedib0t/go-pretty/v6 v6.6.1
github.com/joho/godotenv v1.5.1
github.com/jsimonetti/rtnetlink v1.4.2
github.com/kellerza/template v0.0.6
github.com/klauspost/cpuid/v2 v2.2.8
github.com/mackerelio/go-osstat v0.2.5
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/opencontainers/runtime-spec v1.2.0
github.com/pkg/errors v0.9.1
github.com/pmorjan/kmod v1.1.1
Expand Down Expand Up @@ -180,7 +180,7 @@ require (
github.com/containers/storage v1.55.1 // indirect
github.com/coreos/go-iptables v0.7.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.1-0.20231103132048-7d375ecc2b09 // indirect
github.com/creack/pty v1.1.24 // indirect
github.com/creack/pty v1.1.24
github.com/cyphar/filepath-securejoin v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/disiqueira/gotree/v3 v3.0.2 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
Expand Down Expand Up @@ -752,6 +751,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jedib0t/go-pretty/v6 v6.6.1 h1:iJ65Xjb680rHcikRj6DSIbzCex2huitmc7bDtxYVWyc=
github.com/jedib0t/go-pretty/v6 v6.6.1/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
Expand Down Expand Up @@ -877,7 +878,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-shellwords v1.0.9/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
Expand Down Expand Up @@ -974,8 +974,6 @@ github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JX
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down

0 comments on commit 6ea1998

Please sign in to comment.