-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving the print functions to a new package: printer
Signed-off-by: cmoulliard <[email protected]>
- Loading branch information
1 parent
288284e
commit 771fd89
Showing
4 changed files
with
38 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package printer | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func PrintTable(table metav1.Table, outWriter io.Writer) error { | ||
printer := printers.NewTablePrinter(printers.PrintOptions{}) | ||
return printer.PrintObj(&table, outWriter) | ||
} | ||
|
||
func PrintDataAsJson(data any, outWriter io.Writer) error { | ||
enc := json.NewEncoder(outWriter) | ||
enc.SetEscapeHTML(false) | ||
enc.SetIndent("", " ") | ||
return enc.Encode(data) | ||
} | ||
|
||
func PrintDataAsYaml(data any, outWriter io.Writer) error { | ||
b, err := yaml.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = outWriter.Write(b) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters