Skip to content

Commit

Permalink
[Antctl] Add an output format to better display multi-line string res…
Browse files Browse the repository at this point in the history
…ponses.

Current output formatters (table, json, and yaml) of Antctl are not good at displaying multi-line string responses.
Added a new output formatter called "raw" whose output is similar to fmt.Print(responseString) which directly lets user redirect the output into a file and apply it afterwards.

Fixes antrea-io#3426

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
  • Loading branch information
Atish-iaf authored and Yongming Ding committed May 10, 2022
1 parent 45e4745 commit fd80385
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/antctl/command_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
jsonFormatter formatterType = "json"
yamlFormatter formatterType = "yaml"
tableFormatter formatterType = "table"
rawFormatter formatterType = "raw"
)

const (
Expand Down Expand Up @@ -540,6 +541,8 @@ func (cd *commandDefinition) output(resp io.Reader, writer io.Writer, ft formatt
} else {
return output.TableOutput(obj, writer)
}
case rawFormatter:
return output.RawOutput(obj, writer)
default:
return fmt.Errorf("unsupported format type: %v", ft)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/antctl/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ func YamlOutput(obj interface{}, writer io.Writer) error {
return nil
}

// RawOutput is an output formatter whose output is similar to fmt.Print(responseString)
// to better display multiple-line string responses.
func RawOutput(obj interface{}, writer io.Writer) error {
_, err := fmt.Fprintf(writer, "%v", obj)
if err != nil {
return fmt.Errorf("error when outputing in raw format: %w", err)
}
return nil
}

// tableOutputForGetCommands formats the table output for "get" commands.
func TableOutputForGetCommands(obj interface{}, writer io.Writer) error {
var list []common.TableOutput
Expand Down

0 comments on commit fd80385

Please sign in to comment.