github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/layout/image.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package layout contains functions for interacting with Jackal's package layout on disk.
     5  package layout
     6  
     7  import (
     8  	"path/filepath"
     9  
    10  	"slices"
    11  
    12  	v1 "github.com/google/go-containerregistry/pkg/v1"
    13  )
    14  
    15  // Images contains paths for images.
    16  type Images struct {
    17  	Base      string
    18  	Index     string
    19  	OCILayout string
    20  	Blobs     []string
    21  }
    22  
    23  // AddBlob adds a blob to the Images struct.
    24  func (i *Images) AddBlob(blob string) {
    25  	if len(blob) != 64 {
    26  		return
    27  	}
    28  	abs := filepath.Join(i.Base, "blobs", "sha256", blob)
    29  	if !slices.Contains(i.Blobs, abs) {
    30  		i.Blobs = append(i.Blobs, abs)
    31  	}
    32  }
    33  
    34  // AddV1Image adds a v1.Image to the Images struct.
    35  func (i *Images) AddV1Image(img v1.Image) error {
    36  	layers, err := img.Layers()
    37  	if err != nil {
    38  		return err
    39  	}
    40  	for _, layer := range layers {
    41  		digest, err := layer.Digest()
    42  		if err != nil {
    43  			return err
    44  		}
    45  		i.AddBlob(digest.Hex)
    46  	}
    47  	imgCfgSha, err := img.ConfigName()
    48  	if err != nil {
    49  		return err
    50  	}
    51  	i.AddBlob(imgCfgSha.Hex)
    52  	manifestSha, err := img.Digest()
    53  	if err != nil {
    54  		return err
    55  	}
    56  	i.AddBlob(manifestSha.Hex)
    57  
    58  	return nil
    59  }