Replace olekukonko/tablewriter with cheynewallace/tabby

- Closes #1497
- Closes #1498

This library is simpler and doesn't produce extraneous whitespace around
the output.

A local helper function restores "--" for empty output.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-11-07 16:17:40 -07:00
parent 3cbb295e7b
commit a8554b0af5
11 changed files with 194 additions and 168 deletions

View File

@@ -3,12 +3,12 @@ package list
import (
"strings"
"github.com/warewulf/warewulf/internal/app/wwctl/helper"
apiprofile "github.com/warewulf/warewulf/internal/pkg/api/profile"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/app/wwctl/table"
apiprofile "github.com/warewulf/warewulf/internal/pkg/api/profile"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
@@ -30,12 +30,12 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
if vars.showYaml || vars.showJson {
wwlog.Info(profileInfo.Output[0])
} else {
ph := helper.New(strings.Split(profileInfo.Output[0], ":=:"))
t := table.New(cmd.OutOrStdout())
t.AddHeader(table.Prep(strings.Split(profileInfo.Output[0], ":=:"))...)
for _, val := range profileInfo.Output[1:] {
ph.Append(strings.Split(val, ":=:"))
t.AddLine(table.Prep(strings.Split(val, ":=:"))...)
}
ph.Render()
wwlog.Info("%s", ph.String())
t.Print()
}
}
return

View File

@@ -4,9 +4,9 @@ import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/node"
@@ -24,8 +24,11 @@ func Test_List(t *testing.T) {
{
name: "profile list test",
args: []string{},
stdout: `PROFILE NAME COMMENT/DESCRIPTION
default`,
stdout: `
PROFILE NAME COMMENT/DESCRIPTION
------------ -------------------
default --
`,
inDb: `WW_INTERNAL: 45
nodeprofiles:
default: {}
@@ -38,9 +41,12 @@ nodes:
{
name: "profile list returns multiple profiles",
args: []string{"default,test"},
stdout: `PROFILE NAME COMMENT/DESCRIPTION
default
test`,
stdout: `
PROFILE NAME COMMENT/DESCRIPTION
------------ -------------------
default --
test --
`,
inDb: `WW_INTERNAL: 45
nodeprofiles:
default: {}
@@ -54,8 +60,11 @@ nodes:
{
name: "profile list returns one profile",
args: []string{"test,"},
stdout: `PROFILE NAME COMMENT/DESCRIPTION
test`,
stdout: `
PROFILE NAME COMMENT/DESCRIPTION
------------ -------------------
test --
`,
inDb: `WW_INTERNAL: 45
nodeprofiles:
default: {}
@@ -69,9 +78,12 @@ nodes:
{
name: "profile list returns all profiles",
args: []string{","},
stdout: `PROFILE NAME COMMENT/DESCRIPTION
default
test`,
stdout: `
PROFILE NAME COMMENT/DESCRIPTION
------------ -------------------
default --
test --
`,
inDb: `WW_INTERNAL: 45
nodeprofiles:
default: {}
@@ -111,7 +123,26 @@ nodes:
t.Run(tt.name, func(t *testing.T) {
baseCmd := GetCommand()
baseCmd.SetArgs(tt.args)
verifyOutput(t, baseCmd, tt.stdout)
stdoutR, stdoutW, _ := os.Pipe()
oriout := os.Stdout
os.Stdout = stdoutW
wwlog.SetLogWriter(os.Stdout)
baseCmd.SetOut(os.Stdout)
baseCmd.SetErr(os.Stdout)
err := baseCmd.Execute()
assert.NoError(t, err)
stdoutC := make(chan string)
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, stdoutR)
stdoutC <- buf.String()
}()
stdoutW.Close()
os.Stdout = oriout
stdout := <-stdoutC
assert.Equal(t, strings.TrimSpace(tt.stdout), strings.TrimSpace(stdout))
})
}
}
@@ -219,27 +250,3 @@ nodes:
})
}
}
func verifyOutput(t *testing.T, baseCmd *cobra.Command, content string) {
stdoutR, stdoutW, _ := os.Pipe()
oriout := os.Stdout
os.Stdout = stdoutW
wwlog.SetLogWriter(os.Stdout)
baseCmd.SetOut(os.Stdout)
baseCmd.SetErr(os.Stdout)
err := baseCmd.Execute()
assert.NoError(t, err)
stdoutC := make(chan string)
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, stdoutR)
stdoutC <- buf.String()
}()
stdoutW.Close()
os.Stdout = oriout
stdout := <-stdoutC
assert.NotEmpty(t, stdout)
assert.Contains(t, stdout, content)
}