From fa382861cb38b603ccbb3c59720292e18ac57836 Mon Sep 17 00:00:00 2001 From: Evan Hazlett Date: Tue, 22 Nov 2016 11:18:28 -0500 Subject: [PATCH] update secret inspect to support IDs This updates secret inspect to support inspect by ID in addition to name as well as inspecting multiple secrets. This also cleans up the help text for consistency. Signed-off-by: Evan Hazlett Upstream-commit: 46cd1fa87b2eecb9dba9141d5fe18215a7fb0fd3 Component: cli --- components/cli/command/secret/inspect.go | 24 ++++++-------------- components/cli/command/secret/remove.go | 28 +++++------------------- components/cli/command/secret/utils.go | 27 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/components/cli/command/secret/inspect.go b/components/cli/command/secret/inspect.go index 04a5bd8a88..a82a26e4a8 100644 --- a/components/cli/command/secret/inspect.go +++ b/components/cli/command/secret/inspect.go @@ -9,18 +9,18 @@ import ( ) type inspectOptions struct { - name string + names []string format string } func newSecretInspectCommand(dockerCli *command.DockerCli) *cobra.Command { opts := inspectOptions{} cmd := &cobra.Command{ - Use: "inspect [name]", + Use: "inspect SECRET [SECRET]", Short: "Inspect a secret", - Args: cli.ExactArgs(1), + Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - opts.name = args[0] + opts.names = args return runSecretInspect(dockerCli, opts) }, } @@ -33,23 +33,13 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error { client := dockerCli.Client() ctx := context.Background() - // attempt to lookup secret by name - secrets, err := getSecretsByName(ctx, client, []string{opts.name}) + ids, err := getCliRequestedSecretIDs(ctx, client, opts.names) if err != nil { return err } - - id := opts.name - for _, s := range secrets { - if s.Spec.Annotations.Name == opts.name { - id = s.ID - break - } - } - - getRef := func(name string) (interface{}, []byte, error) { + getRef := func(id string) (interface{}, []byte, error) { return client.SecretInspectWithRaw(ctx, id) } - return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef) + return inspect.Inspect(dockerCli.Out(), ids, opts.format, getRef) } diff --git a/components/cli/command/secret/remove.go b/components/cli/command/secret/remove.go index 44a71ef013..75b4be622b 100644 --- a/components/cli/command/secret/remove.go +++ b/components/cli/command/secret/remove.go @@ -10,17 +10,17 @@ import ( ) type removeOptions struct { - ids []string + names []string } func newSecretRemoveCommand(dockerCli *command.DockerCli) *cobra.Command { return &cobra.Command{ - Use: "rm [id]", + Use: "rm SECRET [SECRET]", Short: "Remove a secret", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts := removeOptions{ - ids: args, + names: args, } return runSecretRemove(dockerCli, opts) }, @@ -31,32 +31,14 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error { client := dockerCli.Client() ctx := context.Background() - // attempt to lookup secret by name - secrets, err := getSecretsByName(ctx, client, opts.ids) + ids, err := getCliRequestedSecretIDs(ctx, client, opts.names) if err != nil { return err } - ids := opts.ids - - names := make(map[string]int) - for _, id := range ids { - names[id] = 1 - } - - if len(secrets) > 0 { - ids = []string{} - - for _, s := range secrets { - if _, ok := names[s.Spec.Annotations.Name]; ok { - ids = append(ids, s.ID) - } - } - } - for _, id := range ids { if err := client.SecretRemove(ctx, id); err != nil { - return err + fmt.Fprintf(dockerCli.Out(), "WARN: %s\n", err) } fmt.Fprintln(dockerCli.Out(), id) diff --git a/components/cli/command/secret/utils.go b/components/cli/command/secret/utils.go index c6e3cb61a2..0134853e09 100644 --- a/components/cli/command/secret/utils.go +++ b/components/cli/command/secret/utils.go @@ -18,3 +18,30 @@ func getSecretsByName(ctx context.Context, client client.APIClient, names []stri Filters: args, }) } + +func getCliRequestedSecretIDs(ctx context.Context, client client.APIClient, names []string) ([]string, error) { + ids := names + + // attempt to lookup secret by name + secrets, err := getSecretsByName(ctx, client, ids) + if err != nil { + return nil, err + } + + lookup := make(map[string]struct{}) + for _, id := range ids { + lookup[id] = struct{}{} + } + + if len(secrets) > 0 { + ids = []string{} + + for _, s := range secrets { + if _, ok := lookup[s.Spec.Annotations.Name]; ok { + ids = append(ids, s.ID) + } + } + } + + return ids, nil +}