diff --git a/cli/command/secret/inspect.go b/cli/command/secret/inspect.go index c8d5cd8f79..c5b0aa6a3d 100644 --- a/cli/command/secret/inspect.go +++ b/cli/command/secret/inspect.go @@ -34,9 +34,23 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error { client := dockerCli.Client() ctx := context.Background() - getRef := func(name string) (interface{}, []byte, error) { - return client.SecretInspectWithRaw(ctx, name) + // attempt to lookup secret by name + secrets, err := getSecrets(client, ctx, []string{opts.name}) + if err != nil { + return err } - return inspect.Inspect(dockerCli.Out(), []string{opts.name}, opts.format, getRef) + 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) { + return client.SecretInspectWithRaw(ctx, id) + } + + return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef) } diff --git a/cli/command/secret/remove.go b/cli/command/secret/remove.go index f336c6161a..9396b9b179 100644 --- a/cli/command/secret/remove.go +++ b/cli/command/secret/remove.go @@ -31,7 +31,30 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error { client := dockerCli.Client() ctx := context.Background() - for _, id := range opts.ids { + // attempt to lookup secret by name + secrets, err := getSecrets(client, ctx, opts.ids) + 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 } diff --git a/cli/command/secret/utils.go b/cli/command/secret/utils.go new file mode 100644 index 0000000000..40aa4a6d77 --- /dev/null +++ b/cli/command/secret/utils.go @@ -0,0 +1,21 @@ +package secret + +import ( + "context" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types/swarm" + "github.com/docker/docker/client" +) + +func getSecrets(client client.APIClient, ctx context.Context, names []string) ([]swarm.Secret, error) { + args := filters.NewArgs() + for _, n := range names { + args.Add("names", n) + } + + return client.SecretList(ctx, types.SecretListOptions{ + Filter: args, + }) +}