cli: Output message on success when writing/deleting entries (#7806)

This provides a user with a better experience, knowing that the command
worked appropriately. The output of the write/delete CLI commands are
not going to be used in a bash script, in fact previously a success
provided no ouput, so we do not have to worry about spurious text being
injected into bash pipelines.
This commit is contained in:
Chris Piraino 2020-06-29 15:47:40 -05:00 committed by hashicorp-ci
parent 41cc82aa4d
commit 4b19d33d34
5 changed files with 12 additions and 7 deletions

View File

@ -58,11 +58,11 @@ func (c *cmd) Run(args []string) int {
_, err = client.ConfigEntries().Delete(c.kind, c.name, nil) _, err = client.ConfigEntries().Delete(c.kind, c.name, nil)
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error deleting config entry %q / %q: %v", c.kind, c.name, err)) c.UI.Error(fmt.Sprintf("Error deleting config entry %s/%s: %v", c.kind, c.name, err))
return 1 return 1
} }
// TODO (mkeeler) should we output anything when successful c.UI.Info(fmt.Sprintf("Config entry deleted: %s/%s", c.kind, c.name))
return 0 return 0
} }

View File

@ -40,7 +40,8 @@ func TestConfigDelete(t *testing.T) {
code := c.Run(args) code := c.Run(args)
require.Equal(t, 0, code) require.Equal(t, 0, code)
require.Empty(t, ui.OutputWriter.String()) require.Contains(t, ui.OutputWriter.String(),
"Config entry deleted: service-defaults/web")
require.Empty(t, ui.ErrorWriter.String()) require.Empty(t, ui.ErrorWriter.String())
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil) entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)

View File

@ -61,7 +61,7 @@ func (c *cmd) Run(args []string) int {
entry, _, err := client.ConfigEntries().Get(c.kind, c.name, nil) entry, _, err := client.ConfigEntries().Get(c.kind, c.name, nil)
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error reading config entry %q / %q: %v", c.kind, c.name, err)) c.UI.Error(fmt.Sprintf("Error reading config entry %s/%s: %v", c.kind, c.name, err))
return 1 return 1
} }

View File

@ -85,16 +85,16 @@ func (c *cmd) Run(args []string) int {
written, _, err = entries.Set(entry, nil) written, _, err = entries.Set(entry, nil)
} }
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error writing config entry %q / %q: %v", entry.GetKind(), entry.GetName(), err)) c.UI.Error(fmt.Sprintf("Error writing config entry %s/%s: %v", entry.GetKind(), entry.GetName(), err))
return 1 return 1
} }
if !written { if !written {
c.UI.Error(fmt.Sprintf("Config entry %q / %q not updated", entry.GetKind(), entry.GetName())) c.UI.Error(fmt.Sprintf("Config entry not updated: %s/%s", entry.GetKind(), entry.GetName()))
return 1 return 1
} }
// TODO (mkeeler) should we output anything when successful c.UI.Info(fmt.Sprintf("Config entry written: %s/%s", entry.GetKind(), entry.GetName()))
return 0 return 0
} }

View File

@ -48,6 +48,8 @@ func TestConfigWrite(t *testing.T) {
code := c.Run(args) code := c.Run(args)
require.Empty(t, ui.ErrorWriter.String()) require.Empty(t, ui.ErrorWriter.String())
require.Contains(t, ui.OutputWriter.String(),
`Config entry written: service-defaults/web`)
require.Equal(t, 0, code) require.Equal(t, 0, code)
entry, _, err := client.ConfigEntries().Get("service-defaults", "web", nil) entry, _, err := client.ConfigEntries().Get("service-defaults", "web", nil)
@ -85,6 +87,8 @@ func TestConfigWrite(t *testing.T) {
code := c.Run(args) code := c.Run(args)
require.Empty(t, ui.ErrorWriter.String()) require.Empty(t, ui.ErrorWriter.String())
require.Contains(t, ui.OutputWriter.String(),
`Config entry written: proxy-defaults/global`)
require.Equal(t, 0, code) require.Equal(t, 0, code)
entry, _, err := client.ConfigEntries().Get(api.ProxyDefaults, api.ProxyConfigGlobal, nil) entry, _, err := client.ConfigEntries().Get(api.ProxyDefaults, api.ProxyConfigGlobal, nil)