Skip to content

Commit

Permalink
vault delete should allow the same output options as vault write,… (
Browse files Browse the repository at this point in the history
#11992)

* `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.
  • Loading branch information
ncabatoff committed Jul 6, 2021
1 parent 7e2d678 commit dff9456
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog/11992.txt
Original file line number Diff line number Diff line change
@@ -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.
```
20 changes: 16 additions & 4 deletions command/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
17 changes: 14 additions & 3 deletions command/kv_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit dff9456

Please sign in to comment.