Skip to content

Commit

Permalink
Add raw formatter for direct []byte data
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cipherboy committed Oct 26, 2022
1 parent d19bbe7 commit a02a290
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var Formatters = map[string]Formatter{
"yaml": YamlFormatter{},
"yml": YamlFormatter{},
"pretty": PrettyFormatter{},
"raw": RawFormatter{},
}

func Format(ui cli.Ui) string {
Expand Down Expand Up @@ -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{}

Expand Down

0 comments on commit a02a290

Please sign in to comment.