feat: Filter table columns in WASM mode

Signed-off-by: Arthur Amstutz <arthur.amstutz@corp.ovh.com>
This commit is contained in:
Arthur Amstutz 2025-10-14 13:20:10 +00:00
parent 9bd8c36251
commit 450e8cd5a5
No known key found for this signature in database
GPG key ID: F3C6FC59C43A6EF6
4 changed files with 72 additions and 28 deletions

View file

@ -14,8 +14,6 @@ import (
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/ovhcloud-cli/internal/cmd"
httplib "github.com/ovh/ovhcloud-cli/internal/http"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type MockSuite struct{}
@ -47,31 +45,9 @@ func (ms *MockSuite) PreTest(t *td.T, testName string) error {
func (ms *MockSuite) PostTest(_ *td.T, _ string) error {
httpmock.Reset()
cmd.PostExecute()
resetSubCommandFlagValues(cmd.GetRootCommand())
return nil
}
func TestMockSuite(t *testing.T) {
tdsuite.Run(t, &MockSuite{})
}
// resetSubCommandFlagValues resets all flags of all subcommands of the given root command to their default values.
func resetSubCommandFlagValues(root *cobra.Command) {
for _, c := range root.Commands() {
c.Flags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
if f.Value.Type() == "stringArray" {
// Special handling for stringArray for which we cannot
// use DefValue since it is equal to "[]".
if r, ok := f.Value.(pflag.SliceValue); ok {
r.Replace(nil)
}
} else {
f.Value.Set(f.DefValue)
}
f.Changed = false
}
})
resetSubCommandFlagValues(c)
}
}

View file

@ -72,6 +72,30 @@ func PostExecute() {
flags.OutputFormatConfig = display.OutputFormat{}
flags.ParametersViaEditor = false
flags.ParametersFile = ""
// Recursively reset all flags of all subcommands to their default values
resetSubCommandFlagValues(rootCmd)
}
// resetSubCommandFlagValues resets all flags of all subcommands of the given root command to their default values.
func resetSubCommandFlagValues(root *cobra.Command) {
for _, c := range root.Commands() {
c.Flags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
if f.Value.Type() == "stringArray" {
// Special handling for stringArray for which we cannot
// use DefValue since it is equal to "[]".
if r, ok := f.Value.(pflag.SliceValue); ok {
r.Replace(nil)
}
} else {
f.Value.Set(f.DefValue)
}
f.Changed = false
}
})
resetSubCommandFlagValues(c)
}
}
func initRootCmd() {

View file

@ -98,10 +98,10 @@ func RenderTable(values []map[string]any, columnsToDisplay []string, outputForma
columnsTitles := make([]string, 0, len(columnsToDisplay))
for _, col := range columnsToDisplay {
// If column to display contains an alias, use it as column title
parts := strings.SplitN(col, " ", 2)
if len(parts) == 2 {
col = parts[0]
columnsTitles = append(columnsTitles, parts[1])
colName, alias, ok := strings.Cut(col, " ")
if ok {
col = colName
columnsTitles = append(columnsTitles, alias)
} else {
columnsTitles = append(columnsTitles, col)
}

View file

@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/PaesslerAG/gval"
"github.com/ovh/ovhcloud-cli/internal/filters"
@ -62,6 +63,49 @@ func RenderTable(values []map[string]any, columnsToDisplay []string, outputForma
return
}
// If not JSON or YAML output, extract only the columns to display
// as the values will be displayed in a table
if !outputFormat.JsonOutput && !outputFormat.YamlOutput {
var (
selectors gval.Evaluables
extractedValues = make([]map[string]any, 0, len(values))
columnsTitles = make([]string, 0, len(columnsToDisplay))
)
for _, col := range columnsToDisplay {
// If column to display contains an alias, use it as column title
colName, alias, ok := strings.Cut(col, " ")
if ok {
col = colName
columnsTitles = append(columnsTitles, alias)
} else {
columnsTitles = append(columnsTitles, col)
}
// Create selector to extract value at given key
evaluator, err := gval.Base().NewEvaluable(col)
if err != nil {
exitError("invalid column to display %q: %s", col, err)
}
selectors = append(selectors, evaluator)
}
// Extract values to display
for _, value := range values {
row := make(map[string]any)
for i, selector := range selectors {
val, err := selector(context.Background(), value)
if err != nil {
exitError("failed to select row field: %s", err)
}
row[columnsTitles[i]] = val
}
extractedValues = append(extractedValues, row)
}
values = extractedValues
}
if err := prettyPrintJSON(values); err != nil {
exitError("error displaying JSON results: %s", err)
return