From 67611bfcd3f4b117e44a2b44f5fef59129ff2759 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 7 Sep 2017 21:59:46 -0400 Subject: [PATCH] Update list command --- command/list.go | 22 ++++++++++------------ command/list_test.go | 14 +++++++------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/command/list.go b/command/list.go index bdf5953cf9..ecfa0acb56 100644 --- a/command/list.go +++ b/command/list.go @@ -8,17 +8,15 @@ import ( "github.com/posener/complete" ) -// Ensure we are implementing the right interfaces. var _ cli.Command = (*ListCommand)(nil) var _ cli.CommandAutocomplete = (*ListCommand)(nil) -// ListCommand is a Command that lists data from the Vault. type ListCommand struct { *BaseCommand } func (c *ListCommand) Synopsis() string { - return "Lists data or secrets" + return "List data or secrets" } func (c *ListCommand) Help() string { @@ -27,14 +25,14 @@ func (c *ListCommand) Help() string { Usage: vault list [options] PATH Lists data from Vault at the given path. This can be used to list keys in a, - given backend. + given secret engine. - List values under the "my-app" folder: + List values under the "my-app" folder of the generic secret engine: $ vault list secret/my-app/ For a full list of examples and paths, please see the documentation that - corresponds to the secret backend in use. Not all backends support listing. + corresponds to the secret engine in use. Not all engines support listing. ` + c.Flags().Help() @@ -62,13 +60,11 @@ func (c *ListCommand) Run(args []string) int { } args = f.Args() - path, kvs, err := extractPath(args) - if err != nil { - c.UI.Error(err.Error()) + switch { + case len(args) < 1: + c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1, got %d)", len(args))) return 1 - } - - if len(kvs) > 0 { + case len(args) > 1: c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args))) return 1 } @@ -79,6 +75,8 @@ func (c *ListCommand) Run(args []string) int { return 2 } + path := ensureTrailingSlash(sanitizePath(args[0])) + secret, err := client.Logical().List(path) if err != nil { c.UI.Error(fmt.Sprintf("Error listing %s: %s", path, err)) diff --git a/command/list_test.go b/command/list_test.go index 712f1cae35..e5a77c532b 100644 --- a/command/list_test.go +++ b/command/list_test.go @@ -28,15 +28,15 @@ func TestListCommand_Run(t *testing.T) { code int }{ { - "empty", - nil, - "Missing PATH!", + "not_enough_args", + []string{}, + "Not enough arguments", 1, }, { - "slash", - []string{"/"}, - "Missing PATH!", + "too_many_args", + []string{"foo", "bar"}, + "Too many arguments", 1, }, { @@ -134,7 +134,7 @@ func TestListCommand_Run(t *testing.T) { t.Errorf("expected %d to be %d", code, exp) } - expected := "Error listing secret/list: " + expected := "Error listing secret/list/: " combined := ui.OutputWriter.String() + ui.ErrorWriter.String() if !strings.Contains(combined, expected) { t.Errorf("expected %q to contain %q", combined, expected)