mirror of https://github.com/docker/cli.git
secrets: enable secret inspect and rm by secret name
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
15b97a39d7
commit
d22e1a91f6
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue