diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go index f7927b3..eba53f6 100644 --- a/internal/cmd/cmd_test.go +++ b/internal/cmd/cmd_test.go @@ -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) - } -} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index ea3c6b8..a14a3c3 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -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() { diff --git a/internal/display/display.go b/internal/display/display.go index eb6d534..c3f3222 100644 --- a/internal/display/display.go +++ b/internal/display/display.go @@ -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) } diff --git a/internal/display/display_wasm.go b/internal/display/display_wasm.go index 444b3f9..995aea5 100644 --- a/internal/display/display_wasm.go +++ b/internal/display/display_wasm.go @@ -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