diff --git a/command/read.go b/command/read.go index b12eb3f60ae1..9faf2f40225a 100644 --- a/command/read.go +++ b/command/read.go @@ -91,19 +91,40 @@ func (c *ReadCommand) Run(args []string) int { return 1 } - secret, err := client.Logical().ReadWithData(path, data) + if Format(c.UI) != "raw" { + secret, err := client.Logical().ReadWithData(path, data) + if err != nil { + c.UI.Error(fmt.Sprintf("Error reading %s: %s", path, err)) + return 2 + } + if secret == nil { + c.UI.Error(fmt.Sprintf("No value found at %s", path)) + return 2 + } + + if c.flagField != "" { + return PrintRawField(c.UI, secret, c.flagField) + } + + return OutputSecret(c.UI, secret) + } + + resp, err := client.Logical().ReadRawWithData(path, data) if err != nil { - c.UI.Error(fmt.Sprintf("Error reading %s: %s", path, err)) + c.UI.Error(fmt.Sprintf("Error reading: %s: %s", path, err)) return 2 } - if secret == nil { + if resp == nil || resp.Body == nil { c.UI.Error(fmt.Sprintf("No value found at %s", path)) return 2 } + defer resp.Body.Close() - if c.flagField != "" { - return PrintRawField(c.UI, secret, c.flagField) + contents, err := io.ReadAll(resp.Body) + if err != nil { + c.UI.Error(fmt.Sprintf("Error reading: %s: %s", path, err)) + return 2 } - return OutputSecret(c.UI, secret) + return OutputData(c.UI, contents) }