From bbdbf7ab6cbeee49756e431ad8c9d25abff1e543 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Tue, 22 Aug 2023 16:01:23 -0700 Subject: [PATCH] Add inspect image commands Outputs the image structure as a tree with an option to show content and related labels for the image. Signed-off-by: Derek McGowan --- cmd/ctr/commands/images/images.go | 1 + cmd/ctr/commands/images/inspect.go | 65 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 cmd/ctr/commands/images/inspect.go diff --git a/cmd/ctr/commands/images/images.go b/cmd/ctr/commands/images/images.go index e5caa96f4..2856c7898 100644 --- a/cmd/ctr/commands/images/images.go +++ b/cmd/ctr/commands/images/images.go @@ -42,6 +42,7 @@ var Command = cli.Command{ checkCommand, exportCommand, importCommand, + inspectCommand, listCommand, mountCommand, unmountCommand, diff --git a/cmd/ctr/commands/images/inspect.go b/cmd/ctr/commands/images/inspect.go new file mode 100644 index 000000000..7dff057d1 --- /dev/null +++ b/cmd/ctr/commands/images/inspect.go @@ -0,0 +1,65 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package images + +import ( + "os" + + "github.com/containerd/containerd/cmd/ctr/commands" + "github.com/containerd/containerd/pkg/display" + "github.com/urfave/cli" +) + +var inspectCommand = cli.Command{ + Name: "inspect", + Aliases: []string{"i"}, + Usage: "inspect an image", + ArgsUsage: " [flags]", + Description: `Inspect an image`, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "content", + Usage: "Show JSON content", + }, + }, + Action: func(clicontext *cli.Context) error { + client, ctx, cancel, err := commands.NewClient(clicontext) + if err != nil { + return err + } + defer cancel() + var ( + ref = clicontext.Args().First() + imageStore = client.ImageService() + cs = client.ContentStore() + ) + + img, err := imageStore.Get(ctx, ref) + if err != nil { + return err + } + + opts := []display.PrintOpt{ + display.WithWriter(os.Stdout), + } + if clicontext.Bool("content") { + opts = append(opts, display.Verbose) + } + + return display.NewImageTreePrinter(opts...).PrintImageTree(ctx, img, cs) + }, +}