|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package manifest |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/containerd/log" |
| 21 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" |
| 22 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" |
| 23 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 24 | + "github.com/containerd/nerdctl/v2/pkg/clientutil" |
| 25 | + "github.com/containerd/nerdctl/v2/pkg/cmd/manifest" |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/formatter" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +func InspectCommand() *cobra.Command { |
| 31 | + var cmd = &cobra.Command{ |
| 32 | + Use: "inspect [OPTIONS] [MANIFEST_LIST] MANIFEST", |
| 33 | + Short: "Display the contents of a manifest list or manifest", |
| 34 | + Args: cobra.MinimumNArgs(1), |
| 35 | + RunE: inspectAction, |
| 36 | + ValidArgsFunction: inspectShellComplete, |
| 37 | + SilenceUsage: true, |
| 38 | + SilenceErrors: true, |
| 39 | + } |
| 40 | + cmd.Flags().BoolP("insecure", "i", false, "Allow insecure TLS connections to the registry") |
| 41 | + cmd.Flags().BoolP("verbose", "v", false, "Verbose output additional info including layers and platform") |
| 42 | + return cmd |
| 43 | +} |
| 44 | + |
| 45 | +func processInspectFlags(cmd *cobra.Command) (types.ManifestInspectOptions, error) { |
| 46 | + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) |
| 47 | + if err != nil { |
| 48 | + return types.ManifestInspectOptions{}, err |
| 49 | + } |
| 50 | + insecure, err := cmd.Flags().GetBool("insecure") |
| 51 | + if err != nil { |
| 52 | + return types.ManifestInspectOptions{}, err |
| 53 | + } |
| 54 | + verbose, err := cmd.Flags().GetBool("verbose") |
| 55 | + if err != nil { |
| 56 | + return types.ManifestInspectOptions{}, err |
| 57 | + } |
| 58 | + return types.ManifestInspectOptions{ |
| 59 | + Stdout: cmd.OutOrStdout(), |
| 60 | + GOptions: globalOptions, |
| 61 | + Insecure: insecure, |
| 62 | + Verbose: verbose, |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +func inspectAction(cmd *cobra.Command, args []string) error { |
| 67 | + inspectOptions, err := processInspectFlags(cmd) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + rawRef := args[0] |
| 72 | + |
| 73 | + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), inspectOptions.GOptions.Namespace, inspectOptions.GOptions.Address) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + defer cancel() |
| 78 | + |
| 79 | + res, err := manifest.Inspect(ctx, client, rawRef, inspectOptions) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + if formatErr := formatter.FormatSlice("", inspectOptions.Stdout, res); formatErr != nil { |
| 84 | + log.G(ctx).Error(formatErr) |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 90 | + return completion.ImageNames(cmd) |
| 91 | +} |
0 commit comments