Skip to content

Commit

Permalink
cilium-dbg: Use a tabwriter that remembers the widths
Browse files Browse the repository at this point in the history
Use the same tabwriter fork as Kubernetes to make "--watch"
in StateDB commands not show different indentantions after
every Flush().

Signed-off-by: Jussi Maki <jussi@isovalent.com>
  • Loading branch information
joamaki committed May 10, 2024
1 parent 8c03a3b commit d079381
Show file tree
Hide file tree
Showing 8 changed files with 702 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cilium-dbg/cmd/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"net/url"
"os"
"strings"
"text/tabwriter"
"time"

"github.com/liggitt/tabwriter"
"github.com/spf13/cobra"

"github.com/cilium/statedb"
Expand Down Expand Up @@ -61,6 +61,17 @@ func newRemoteTable[Obj any](tableName string) *statedb.RemoteTable[Obj] {
return table
}

func newTabWriter(out io.Writer) *tabwriter.Writer {
const (
minWidth = 6
width = 4
padding = 3
padChar = ' '
flags = tabwriter.RememberWidths
)
return tabwriter.NewWriter(out, minWidth, width, padding, padChar, flags)
}

func statedbTableCommand[Obj statedb.TableWritable](tableName string) *cobra.Command {
var watchInterval time.Duration
cmd := &cobra.Command{
Expand All @@ -69,7 +80,7 @@ func statedbTableCommand[Obj statedb.TableWritable](tableName string) *cobra.Com
Run: func(cmd *cobra.Command, args []string) {
table := newRemoteTable[Obj](tableName)

w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)
w := newTabWriter(os.Stdout)
var obj Obj
fmt.Fprintf(w, "%s\n", strings.Join(obj.TableHeader(), "\t"))
defer w.Flush()
Expand All @@ -82,7 +93,7 @@ func statedbTableCommand[Obj statedb.TableWritable](tableName string) *cobra.Com
iter, errChan := table.LowerBound(context.Background(), statedb.ByRevision[Obj](revision))

if iter != nil {
err := statedb.ProcessEach[Obj](
err := statedb.ProcessEach(
iter,
func(obj Obj, rev statedb.Revision) error {
// Remember the latest revision to query from.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/jeremywohl/flatten v1.0.1
github.com/kevinburke/ssh_config v1.2.0
github.com/kr/pretty v0.3.1
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
github.com/mackerelio/go-osstat v0.2.4
github.com/mattn/go-shellwords v1.0.12
github.com/mdlayher/arp v0.0.0-20220221190821-c37aaafac7f9
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/liggitt/tabwriter/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/liggitt/tabwriter/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/liggitt/tabwriter/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d079381

Please sign in to comment.