2019-04-17 03:01:10 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-01-05 20:11:53 +08:00
|
|
|
"context"
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
"github.com/docker/buildx/builder"
|
2023-04-10 03:20:18 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil/completion"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/util/imagetools"
|
2022-02-26 03:30:04 +08:00
|
|
|
"github.com/docker/cli-docs-tool/annotation"
|
2019-04-17 03:01:10 +08:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-02-25 00:28:28 +08:00
|
|
|
"github.com/pkg/errors"
|
2019-04-17 03:01:10 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type inspectOptions struct {
|
2021-11-04 12:17:27 +08:00
|
|
|
builder string
|
2022-02-25 00:28:28 +08:00
|
|
|
format string
|
|
|
|
raw bool
|
2019-04-17 03:01:10 +08:00
|
|
|
}
|
|
|
|
|
2024-01-05 20:11:53 +08:00
|
|
|
func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions, name string) error {
|
2022-02-25 00:28:28 +08:00
|
|
|
if in.format != "" && in.raw {
|
|
|
|
return errors.Errorf("format and raw cannot be used together")
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
b, err := builder.New(dockerCli, builder.WithName(in.builder))
|
2021-11-04 12:17:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-06 02:57:35 +08:00
|
|
|
imageopt, err := b.ImageOpt()
|
2021-11-04 12:17:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-17 06:14:03 +08:00
|
|
|
|
2022-02-25 00:28:28 +08:00
|
|
|
p, err := imagetools.NewPrinter(ctx, imageopt, name, in.format)
|
2019-04-17 06:14:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-25 00:28:28 +08:00
|
|
|
return p.Print(in.raw, dockerCli.Out())
|
2019-04-17 03:01:10 +08:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
|
2019-04-17 03:01:10 +08:00
|
|
|
var options inspectOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "inspect [OPTIONS] NAME",
|
2022-02-25 00:28:28 +08:00
|
|
|
Short: "Show details of an image in the registry",
|
2019-04-17 03:01:10 +08:00
|
|
|
Args: cli.ExactArgs(1),
|
2024-01-20 08:44:30 +08:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-04-13 21:36:48 +08:00
|
|
|
options.builder = *rootOpts.Builder
|
2024-01-05 20:11:53 +08:00
|
|
|
return runInspect(cmd.Context(), dockerCli, options, args[0])
|
2024-01-20 08:44:30 +08:00
|
|
|
},
|
2023-04-10 03:20:18 +08:00
|
|
|
ValidArgsFunction: completion.Disable,
|
2019-04-17 03:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2022-02-26 03:30:04 +08:00
|
|
|
|
|
|
|
flags.StringVar(&options.format, "format", "", "Format the output using the given Go template")
|
|
|
|
flags.SetAnnotation("format", annotation.DefaultValue, []string{`"{{.Manifest}}"`})
|
|
|
|
|
2022-02-25 00:28:28 +08:00
|
|
|
flags.BoolVar(&options.raw, "raw", false, "Show original, unformatted JSON manifest")
|
2019-04-17 03:01:10 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|