File: reader.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (52 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package layout

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"

	"github.com/containers/image/v5/types"
	imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

// This file is named reader.go for consistency with other transports’
// handling of “image containers”, but we don’t actually need a stateful reader object.

// ListResult wraps the image reference and the manifest for loading
type ListResult struct {
	Reference          types.ImageReference
	ManifestDescriptor imgspecv1.Descriptor
}

// List returns a slice of manifests included in the archive
func List(dir string) ([]ListResult, error) {
	var res []ListResult

	indexJSON, err := os.ReadFile(filepath.Join(dir, imgspecv1.ImageIndexFile))
	if err != nil {
		return nil, err
	}
	var index imgspecv1.Index
	if err := json.Unmarshal(indexJSON, &index); err != nil {
		return nil, err
	}

	for manifestIndex, md := range index.Manifests {
		refName := md.Annotations[imgspecv1.AnnotationRefName]
		index := -1
		if refName == "" {
			index = manifestIndex
		}
		ref, err := newReference(dir, refName, index)
		if err != nil {
			return nil, fmt.Errorf("error creating image reference: %w", err)
		}
		reference := ListResult{
			Reference:          ref,
			ManifestDescriptor: md,
		}
		res = append(res, reference)
	}
	return res, nil
}