From a02a290405e684314f63b3f9c8c16157159caa19 Mon Sep 17 00:00:00 2001 From: Alexander Scheel Date: Fri, 4 Feb 2022 14:07:07 -0500 Subject: [PATCH] Add raw formatter for direct []byte data As mentioned in the previous commit, some API endpoints return non-JSON data. We get as far as fetching this data (via client.Logical().Read), but parsing it as an api.Secret fails (as in this case, it is non-JSON). Given that we intend to update `vault read` to support such endpoints, we'll need a "raw" formatter that accepts []byte-encoded data and simply writes it to the UI. Signed-off-by: Alexander Scheel --- command/format.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/command/format.go b/command/format.go index 4dffed646359..b138f13290cf 100644 --- a/command/format.go +++ b/command/format.go @@ -70,6 +70,7 @@ var Formatters = map[string]Formatter{ "yaml": YamlFormatter{}, "yml": YamlFormatter{}, "pretty": PrettyFormatter{}, + "raw": RawFormatter{}, } func Format(ui cli.Ui) string { @@ -125,6 +126,28 @@ func (j JsonFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) e return nil } +// An output formatter for raw output of the original request object +type RawFormatter struct{} + +func (r RawFormatter) Format(data interface{}) ([]byte, error) { + byte_data, ok := data.([]byte) + if !ok { + panic("backtrace") + return nil, fmt.Errorf("unable to type assert to []byte: %T", data) + } + + return byte_data, nil +} + +func (r RawFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error { + b, err := r.Format(data) + if err != nil { + return err + } + ui.Output(string(b)) + return nil +} + // An output formatter for yaml output format of an object type YamlFormatter struct{}