Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/cmd/consumer/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/consumergroup/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/globalrule/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/pluginconfig/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/proto/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/route/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/service/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/ssl/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/streamroute/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

Comment on lines 52 to +56
cfg, err := opts.Config()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/upstream/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewCmdExport(f *cmd.Factory) *cobra.Command {
}

func exportRun(opts *Options) error {
if err := cmdutil.ValidateExportFormat(opts.Output); err != nil {
return err
}

cfg, err := opts.Config()
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions pkg/cmdutil/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ func NewExporter(format string, writer io.Writer) *Exporter {
}
}

// ValidateExportFormat returns an error if format is not one of the formats
// that Exporter.Write accepts. The empty string is treated as valid because
// every export command defaults it to "yaml" later in its run.
//
// Callers should invoke this before any work that could short-circuit the
// run (e.g. an "empty collection" early return), so an invalid -o flag is
// rejected consistently regardless of the result set size.
func ValidateExportFormat(format string) error {
switch format {
case "", "json", "yaml":
return nil
default:
return fmt.Errorf("unsupported output format: %s", format)
}
}
Comment on lines +33 to +40

// Write formats and writes the given data.
func (e *Exporter) Write(data interface{}) error {
switch e.format {
Expand Down
22 changes: 22 additions & 0 deletions pkg/cmdutil/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,25 @@ func TestExporter_JSONPrettyPrinted(t *testing.T) {
// Verify pretty printing (indented)
assert.Contains(t, buf.String(), " \"key\"")
}

func TestValidateExportFormat(t *testing.T) {
t.Run("accepts json", func(t *testing.T) {
require.NoError(t, ValidateExportFormat("json"))
})
t.Run("accepts yaml", func(t *testing.T) {
require.NoError(t, ValidateExportFormat("yaml"))
})
t.Run("accepts empty (callers default later)", func(t *testing.T) {
require.NoError(t, ValidateExportFormat(""))
})
t.Run("rejects table", func(t *testing.T) {
err := ValidateExportFormat("table")
require.Error(t, err)
assert.Contains(t, err.Error(), "unsupported output format: table")
})
t.Run("rejects garbage", func(t *testing.T) {
err := ValidateExportFormat("garbage-format")
require.Error(t, err)
assert.Contains(t, err.Error(), "unsupported output format: garbage-format")
})
}
Loading