MCPcopy Create free account
hub / github.com/containerd/nerdctl / Save

Function Save

pkg/cmd/image/save.go:41–114  ·  view source on GitHub ↗

Save exports `images` to a `io.Writer` (e.g., a file writer, or os.Stdout) specified by `options.Stdout`.

(ctx context.Context, client *containerd.Client, images []string, options types.ImageSaveOptions)

Source from the content-addressed store, hash-verified

39
40// Save exports `images` to a `io.Writer` (e.g., a file writer, or os.Stdout) specified by `options.Stdout`.
41func Save(ctx context.Context, client *containerd.Client, images []string, options types.ImageSaveOptions) error {
42 images = strutil.DedupeStrSlice(images)
43
44 var exportOpts []tarchive.ExportOpt
45
46 if len(options.Platform) > 0 {
47 for _, ps := range options.Platform {
48 p, err := platforms.Parse(ps)
49 if err != nil {
50 return fmt.Errorf("invalid platform %q: %w", ps, err)
51 }
52 exportOpts = append(exportOpts, tarchive.WithPlatform(p))
53 }
54 }
55 if options.AllPlatforms {
56 exportOpts = append(exportOpts, tarchive.WithAllPlatforms)
57 }
58
59 platMC, err := platformutil.NewMatchComparer(options.AllPlatforms, options.Platform)
60 if err != nil {
61 return err
62 }
63
64 imageService := client.ImageService()
65 var storeOpts []transferimage.StoreOpt
66 for _, img := range images {
67 var imageRef string
68
69 var dgst digest.Digest
70 var err error
71 if dgst, err = digest.Parse(img); err != nil {
72 if dgst, err = digest.Parse("sha256:" + img); err != nil {
73 named, err := reference.ParseNormalizedNamed(img)
74 if err != nil {
75 return fmt.Errorf("invalid image name %q: %w", img, err)
76 }
77 imageRef = reference.TagNameOnly(named).String()
78 err = EnsureAllContent(ctx, client, imageRef, platMC, options.GOptions)
79 if err != nil {
80 return err
81 }
82 storeOpts = append(storeOpts, transferimage.WithExtraReference(imageRef))
83 continue
84 }
85 }
86
87 filters := []string{fmt.Sprintf("target.digest~=^%s$", dgst.String())}
88 imageList, err := imageService.List(ctx, filters...)
89 if err != nil {
90 return fmt.Errorf("failed to list images: %w", err)
91 }
92 if len(imageList) == 0 {
93 return fmt.Errorf("image %q: not found", img)
94 }
95
96 imageRef = imageList[0].Name
97 err = EnsureAllContent(ctx, client, imageRef, platMC, options.GOptions)
98 if err != nil {

Callers 1

saveActionFunction · 0.92

Calls 7

DedupeStrSliceFunction · 0.92
NewMatchComparerFunction · 0.92
ProgressHandlerFunction · 0.92
EnsureAllContentFunction · 0.85
ParseMethod · 0.65
ListMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…