Skip to content

Commit

Permalink
Add oci/layout.List
Browse files Browse the repository at this point in the history
Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Sep 10, 2024
1 parent 8bb77a7 commit 13d3ebf
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions oci/layout/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package layout

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

"github.com/containers/image/v5/types"
digest "github.com/opencontainers/go-digest"
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
MIMEType string
Digest digest.Digest
ReferenceName string // imgspecv1.AnnotationRefName or ""
}

// 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,
MIMEType: md.MediaType,
Digest: md.Digest,
ReferenceName: refName,
}
res = append(res, reference)
}
return res, nil
}

0 comments on commit 13d3ebf

Please sign in to comment.