github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/image/inspect.go (about) 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 image 18 19 import ( 20 "context" 21 "fmt" 22 "time" 23 24 "github.com/containerd/containerd" 25 "github.com/containerd/log" 26 "github.com/containerd/nerdctl/v2/pkg/api/types" 27 "github.com/containerd/nerdctl/v2/pkg/formatter" 28 "github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker" 29 "github.com/containerd/nerdctl/v2/pkg/imageinspector" 30 "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" 31 ) 32 33 // Inspect prints detailed information of each image in `images`. 34 func Inspect(ctx context.Context, client *containerd.Client, images []string, options types.ImageInspectOptions) error { 35 f := &imageInspector{ 36 mode: options.Mode, 37 } 38 walker := &imagewalker.ImageWalker{ 39 Client: client, 40 OnFound: func(ctx context.Context, found imagewalker.Found) error { 41 ctx, cancel := context.WithTimeout(ctx, 5*time.Second) 42 defer cancel() 43 44 n, err := imageinspector.Inspect(ctx, client, found.Image, options.GOptions.Snapshotter) 45 if err != nil { 46 return err 47 } 48 switch f.mode { 49 case "native": 50 f.entries = append(f.entries, n) 51 case "dockercompat": 52 d, err := dockercompat.ImageFromNative(n) 53 if err != nil { 54 return err 55 } 56 f.entries = append(f.entries, d) 57 default: 58 return fmt.Errorf("unknown mode %q", f.mode) 59 } 60 return nil 61 }, 62 } 63 64 err := walker.WalkAll(ctx, images, true) 65 if len(f.entries) > 0 { 66 if formatErr := formatter.FormatSlice(options.Format, options.Stdout, f.entries); formatErr != nil { 67 log.G(ctx).Error(formatErr) 68 } 69 } 70 return err 71 } 72 73 type imageInspector struct { 74 mode string 75 entries []interface{} 76 }