2022-05-23 15:48:01 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PrettyPrintJSON prints m as an indented JSON to the cmd output.
|
|
|
|
func PrettyPrintJSON(cmd *cobra.Command, m json.Marshaler, entity string) {
|
|
|
|
data, err := m.MarshalJSON()
|
|
|
|
if err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Can't convert %s to json: %w", entity, err)
|
2022-05-23 15:48:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := json.Indent(buf, data, "", " "); err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Can't pretty print json: %w", err)
|
2022-05-23 15:48:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd.Println(buf)
|
|
|
|
}
|