2016-10-19 12:22:02 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-05-15 18:23:20 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 18:23:20 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type inspectOptions struct {
|
2016-11-22 11:18:28 -05:00
|
|
|
names []string
|
2016-10-19 12:22:02 -04:00
|
|
|
format string
|
2017-05-15 18:23:20 -04:00
|
|
|
pretty bool
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-04-01 03:07:22 -04:00
|
|
|
func newSecretInspectCommand(dockerCli command.Cli) *cobra.Command {
|
2016-10-19 12:22:02 -04:00
|
|
|
opts := inspectOptions{}
|
|
|
|
cmd := &cobra.Command{
|
2016-11-18 01:28:21 -05:00
|
|
|
Use: "inspect [OPTIONS] SECRET [SECRET...]",
|
|
|
|
Short: "Display detailed information on one or more secrets",
|
2016-11-22 11:18:28 -05:00
|
|
|
Args: cli.RequiresMinArgs(1),
|
2016-10-19 12:22:02 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-22 11:18:28 -05:00
|
|
|
opts.names = args
|
2016-10-19 12:22:02 -04:00
|
|
|
return runSecretInspect(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-29 10:44:12 -05:00
|
|
|
cmd.Flags().StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
|
2017-05-15 18:23:20 -04:00
|
|
|
cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
|
2016-10-19 12:22:02 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-04-01 03:07:22 -04:00
|
|
|
func runSecretInspect(dockerCli command.Cli, opts inspectOptions) error {
|
2016-10-19 12:22:02 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-05-15 18:23:20 -04:00
|
|
|
if opts.pretty {
|
|
|
|
opts.format = "pretty"
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:18:28 -05:00
|
|
|
getRef := func(id string) (interface{}, []byte, error) {
|
2016-11-01 23:32:21 -04:00
|
|
|
return client.SecretInspectWithRaw(ctx, id)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
2017-05-15 18:23:20 -04:00
|
|
|
f := opts.format
|
|
|
|
|
|
|
|
// check if the user is trying to apply a template to the pretty format, which
|
|
|
|
// is not supported
|
|
|
|
if strings.HasPrefix(f, "pretty") && f != "pretty" {
|
|
|
|
return fmt.Errorf("Cannot supply extra formatting options to the pretty template")
|
|
|
|
}
|
|
|
|
|
|
|
|
secretCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewSecretFormat(f, false),
|
|
|
|
}
|
2016-10-19 12:22:02 -04:00
|
|
|
|
2017-05-15 18:23:20 -04:00
|
|
|
if err := formatter.SecretInspectWrite(secretCtx, opts.names, getRef); err != nil {
|
|
|
|
return cli.StatusError{StatusCode: 1, Status: err.Error()}
|
|
|
|
}
|
|
|
|
return nil
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|