|
| 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 | + "fmt" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + |
| 24 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" |
| 25 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/cmd/manifest" |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/formatter" |
| 29 | +) |
| 30 | + |
| 31 | +func InspectCommand() *cobra.Command { |
| 32 | + var cmd = &cobra.Command{ |
| 33 | + Use: "inspect MANIFEST", |
| 34 | + Short: "Display the contents of a manifest or image index/manifest list", |
| 35 | + Args: cobra.MinimumNArgs(1), |
| 36 | + RunE: inspectAction, |
| 37 | + ValidArgsFunction: inspectShellComplete, |
| 38 | + SilenceUsage: true, |
| 39 | + SilenceErrors: true, |
| 40 | + } |
| 41 | + cmd.Flags().Bool("verbose", false, "Verbose output additional info including layers and platform") |
| 42 | + cmd.Flags().Bool("insecure", false, "Allow communication with an insecure registry") |
| 43 | + return cmd |
| 44 | +} |
| 45 | + |
| 46 | +func processInspectFlags(cmd *cobra.Command) (types.ManifestInspectOptions, error) { |
| 47 | + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) |
| 48 | + if err != nil { |
| 49 | + return types.ManifestInspectOptions{}, err |
| 50 | + } |
| 51 | + verbose, err := cmd.Flags().GetBool("verbose") |
| 52 | + if err != nil { |
| 53 | + return types.ManifestInspectOptions{}, err |
| 54 | + } |
| 55 | + insecure, err := cmd.Flags().GetBool("insecure") |
| 56 | + if err != nil { |
| 57 | + return types.ManifestInspectOptions{}, err |
| 58 | + } |
| 59 | + return types.ManifestInspectOptions{ |
| 60 | + Stdout: cmd.OutOrStdout(), |
| 61 | + GOptions: globalOptions, |
| 62 | + Verbose: verbose, |
| 63 | + Insecure: insecure, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +func inspectAction(cmd *cobra.Command, args []string) error { |
| 68 | + inspectOptions, err := processInspectFlags(cmd) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + rawRef := args[0] |
| 73 | + res, err := manifest.Inspect(cmd.Context(), rawRef, inspectOptions) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // Output format: single object for single result, array for multiple results |
| 79 | + if len(res) == 1 { |
| 80 | + jsonStr, err := formatter.ToJSON(res[0], "", " ") |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + fmt.Fprint(inspectOptions.Stdout, jsonStr) |
| 85 | + } else { |
| 86 | + if formatErr := formatter.FormatSlice("", inspectOptions.Stdout, res); formatErr != nil { |
| 87 | + return formatErr |
| 88 | + } |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 94 | + return completion.ImageNames(cmd) |
| 95 | +} |
0 commit comments