From a2dcb131ee34623e8fe0284956e02621ddb3a82f Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Tue, 6 Jul 2021 16:36:07 +0200 Subject: [PATCH] =?UTF-8?q?`vault=20delete`=20should=20allow=20the=20same?= =?UTF-8?q?=20output=20options=20as=20`vault=20write`,=E2=80=A6=20(#11992)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * `vault delete` and `vault kv delete` should allow the same output options as `vault write`, as delete operations can similarly return data. This is needed if you want to use control groups with deletion. --- changelog/11992.txt | 3 +++ command/delete.go | 20 ++++++++++++++++---- command/kv_delete.go | 17 ++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 changelog/11992.txt diff --git a/changelog/11992.txt b/changelog/11992.txt new file mode 100644 index 000000000..c9b2c4749 --- /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 b22a06df6..78d786f11 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 47d66b769..73583d171 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) {