Skip to content

Commit

Permalink
Don't say "Success!" when a specific field is requested. (#21546)
Browse files Browse the repository at this point in the history
* add a test to show the bug

* do not output a "Success!" message if a specific field was requested

* Create 21545.txt

* Fix changelog name

---------

Co-authored-by: Violet Hynes <[email protected]>
  • Loading branch information
maokt and VioletHynes authored Aug 17, 2023
1 parent 7d361ee commit 6ceee62
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 19 deletions.
3 changes: 3 additions & 0 deletions changelog/21546.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Avoid printing "Success" message when `-field` flag is provided during a `vault write`.
```
3 changes: 2 additions & 1 deletion command/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ func handleWriteSecretOutput(c *BaseCommand, path string, secret *api.Secret, er
}
if secret == nil {
// Don't output anything unless using the "table" format
if Format(c.UI) == "table" {
// and even then, don't output anything if a specific field was requested
if c.flagField == "" && Format(c.UI) == "table" {
c.UI.Info(fmt.Sprintf("Success! Data written to: %s", path))
}
return 0
Expand Down
24 changes: 24 additions & 0 deletions command/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ func TestWriteCommand_Run(t *testing.T) {
})
}

// If we ask for a field and get an empty result, do not output "Success!" or anything else
t.Run("field_from_nothing", func(t *testing.T) {
t.Parallel()

client, closer := testVaultServer(t)
defer closer()

ui, cmd := testWriteCommand(t)
cmd.client = client

code := cmd.Run([]string{
"-field", "somefield",
"secret/write/foo", "foo=bar",
})
if exp := 0; code != exp {
t.Fatalf("expected %d to be %d: %q", code, exp, ui.ErrorWriter.String())
}

combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if combined != "" {
t.Errorf("expected %q to be empty", combined)
}
})

t.Run("force", func(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/commands/login.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ flags](/vault/docs/commands) included on all commands.

### Output options

- `-field` `(string: "")` - Print only the field with the given name. Specifying
this option will take precedence over other formatting directives. The result
will not have a trailing newline making it ideal for piping to other processes.
- `-field` `(string: "")` - Print only the field with the given name, in the format
specified in the `-format` directive. The result will not have a trailing
newline making it ideal for piping to other processes.

- `-format` `(string: "table")` - Print the output in the given format. Valid
formats are "table", "json", or "yaml". This can also be specified via the
Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/commands/patch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ flags](/vault/docs/commands) included on all commands.

### Output options

- `-field` `(string: "")` - Print only the field with the given name. Specifying
this option will take precedence over other formatting directives. The result
will not have a trailing newline making it ideal for piping to other processes.
- `-field` `(string: "")` - Print only the field with the given name, in the format
specified in the `-format` directive. The result will not have a trailing
newline making it ideal for piping to other processes.

- `-format` `(string: "table")` - Print the output in the given format. Valid
formats are "table", "json", or "yaml". This can also be specified via the
Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/commands/read.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ flags](/vault/docs/commands) included on all commands.

### Output options

- `-field` `(string: "")` - Print only the field with the given name. Specifying
this option will take precedence over other formatting directives. The result
will not have a trailing newline making it ideal for piping to other processes.
- `-field` `(string: "")` - Print only the field with the given name, in the format
specified in the `-format` directive. The result will not have a trailing
newline making it ideal for piping to other processes.

- `-format` `(string: "table")` - Print the output in the given format. Valid
formats are "table", "json", "yaml", or "raw". This can also be specified
Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/commands/ssh.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ flags](/vault/docs/commands) included on all commands.

### Output options

- `-field` `(string: "")` - Print only the field with the given name. Specifying
this option will take precedence over other formatting directives. The result
will not have a trailing newline making it ideal for piping to other processes.
- `-field` `(string: "")` - Print only the field with the given name, in the format
specified in the `-format` directive. The result will not have a trailing
newline making it ideal for piping to other processes.

- `-format` `(string: "table")` - Print the output in the given format. Valid
formats are "table", "json", or "yaml". This can also be specified via the
Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/commands/unwrap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ flags](/vault/docs/commands) included on all commands.

### Output options

- `-field` `(string: "")` - Print only the field with the given name. Specifying
this option will take precedence over other formatting directives. The result
will not have a trailing newline making it ideal for piping to other processes.
- `-field` `(string: "")` - Print only the field with the given name, in the format
specified in the `-format` directive. The result will not have a trailing
newline making it ideal for piping to other processes.

- `-format` `(string: "table")` - Print the output in the given format. Valid
formats are "table", "json", or "yaml". This can also be specified via the
Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/commands/write.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ flags](/vault/docs/commands) included on all commands.

### Output options

- `-field` `(string: "")` - Print only the field with the given name. Specifying
this option will take precedence over other formatting directives. The result
will not have a trailing newline making it ideal for piping to other processes.
- `-field` `(string: "")` - Print only the field with the given name, in the format
specified in the `-format` directive. The result will not have a trailing
newline making it ideal for piping to other processes.

- `-format` `(string: "table")` - Print the output in the given format. Valid
formats are "table", "json", or "yaml". This can also be specified via the
Expand Down

0 comments on commit 6ceee62

Please sign in to comment.