diff --git a/changelog/11992.txt b/changelog/11992.txt new file mode 100644 index 000000000000..c9b2c4749dd5 --- /dev/null +++ b/changelog/11992.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: vault delete and vault kv delete should support the same output options (e.g. -format) as vault write. +``` diff --git a/command/delete.go b/command/delete.go index b22a06df618c..78d786f11756 100644 --- a/command/delete.go +++ b/command/delete.go @@ -53,7 +53,7 @@ Usage: vault delete [options] PATH } func (c *DeleteCommand) Flags() *FlagSets { - return c.flagSet(FlagSetHTTP) + return c.flagSet(FlagSetHTTP | FlagSetOutputField | FlagSetOutputFormat) } func (c *DeleteCommand) AutocompleteArgs() complete.Predictor { @@ -95,7 +95,7 @@ func (c *DeleteCommand) Run(args []string) int { data, err := parseArgsDataStringLists(stdin, args[1:]) if err != nil { - c.UI.Error(fmt.Sprintf("Failed to parse K=V data: %s", err)) + c.UI.Error(fmt.Sprintf("Failed to parse string list data: %s", err)) return 1 } @@ -108,6 +108,18 @@ func (c *DeleteCommand) Run(args []string) int { return 2 } - c.UI.Info(fmt.Sprintf("Success! Data deleted (if it existed) at: %s", path)) - return 0 + if secret == nil { + // Don't output anything unless using the "table" format + if Format(c.UI) == "table" { + c.UI.Info(fmt.Sprintf("Success! Data deleted (if it existed) at: %s", path)) + } + return 0 + } + + // Handle single field output + if c.flagField != "" { + return PrintRawField(c.UI, secret, c.flagField) + } + + return OutputSecret(c.UI, secret) } diff --git a/command/kv_delete.go b/command/kv_delete.go index 47d66b76917b..73583d171dc4 100644 --- a/command/kv_delete.go +++ b/command/kv_delete.go @@ -50,7 +50,7 @@ Usage: vault kv delete [options] PATH } func (c *KVDeleteCommand) Flags() *FlagSets { - set := c.flagSet(FlagSetHTTP) + set := c.flagSet(FlagSetHTTP | FlagSetOutputField | FlagSetOutputFormat) // Common Options f := set.NewFlagSet("Common Options") @@ -118,8 +118,19 @@ func (c *KVDeleteCommand) Run(args []string) int { return 2 } - c.UI.Info(fmt.Sprintf("Success! Data deleted (if it existed) at: %s", path)) - return 0 + if secret == nil { + // Don't output anything unless using the "table" format + if Format(c.UI) == "table" { + c.UI.Info(fmt.Sprintf("Success! Data deleted (if it existed) at: %s", path)) + } + return 0 + } + + if c.flagField != "" { + return PrintRawField(c.UI, secret, c.flagField) + } + + return OutputSecret(c.UI, secret) } func (c *KVDeleteCommand) deleteV2(path, mountPath string, client *api.Client) (*api.Secret, error) {