Skip to content

Commit

Permalink
Fallback to direct serialiser when runtime printer returns an error
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Mar 5, 2019
1 parent 92f1f01 commit f2dfe94
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/printers/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func NewJSONPrinter() OutputPrinter {
// the supplied writer.
func (j *JSONPrinter) PrintObj(obj interface{}, writer io.Writer) error {
if obj, ok := obj.(runtime.Object); ok {
return j.runtimePrinter.PrintObj(obj, writer)
if err := j.runtimePrinter.PrintObj(obj, writer); err == nil {
// if an error occurred, we may still be able to serialise using json package directly
return nil
}
}

b, err := json.MarshalIndent(obj, "", " ")
Expand Down
5 changes: 4 additions & 1 deletion pkg/printers/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func NewYAMLPrinter() OutputPrinter {
// the supplied writer.
func (y *YAMLPrinter) PrintObj(obj interface{}, writer io.Writer) error {
if obj, ok := obj.(runtime.Object); ok {
return y.runtimePrinter.PrintObj(obj, writer)
if err := y.runtimePrinter.PrintObj(obj, writer); err == nil {
// if an error occurred, we may still be able to serialise using yaml package directly
return nil
}
}

b, err := yaml.Marshal(obj)
Expand Down

0 comments on commit f2dfe94

Please sign in to comment.