...

Source file src/edge-infra.dev/pkg/f8n/warehouse/oci/name/name.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/oci/name

     1  // Package name implements functionality for parsing package names from OCI
     2  // artifacts and working with them.
     3  package name
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/google/go-containerregistry/pkg/name"
     9  	v1 "github.com/google/go-containerregistry/pkg/v1"
    10  
    11  	wh "edge-infra.dev/pkg/f8n/warehouse"
    12  	"edge-infra.dev/pkg/f8n/warehouse/lift"
    13  	"edge-infra.dev/pkg/f8n/warehouse/oci"
    14  )
    15  
    16  // Type aliases from ggcr
    17  type Reference = name.Reference
    18  
    19  // TODO(aw185176): FromIndex, tests
    20  
    21  // FromImage reads the Warehouse package name from the input image
    22  func FromImage(img v1.Image) (string, error) {
    23  	return FromArtifact(img)
    24  }
    25  
    26  // FromArtifact reads the Warehouse package name from the input Artifact
    27  func FromArtifact(a oci.Artifact) (string, error) {
    28  	annos, err := oci.Annotations(a)
    29  	switch {
    30  	case err != nil:
    31  		return "", err
    32  	case annos[wh.AnnotationName] == "":
    33  		return "", fmt.Errorf("%w", oci.ErrInvalidArtifact)
    34  	default:
    35  		return annos[wh.AnnotationName], nil
    36  	}
    37  }
    38  
    39  // Tag creates a tag with weak validation, defaulting to the local
    40  // registry alias for Warehouse packages if not set. Default options can be
    41  // overridden by passing name.Options
    42  func Tag(t string, opts ...name.Option) (name.Tag, error) {
    43  	return name.NewTag(t,
    44  		append([]name.Option{
    45  			name.WeakValidation,
    46  			name.WithDefaultRegistry(lift.LocalRegistryAlias),
    47  			name.WithDefaultTag("latest"),
    48  		}, opts...,
    49  		)...,
    50  	)
    51  }
    52  
    53  // Digest creates a digest with weak validation, defaulting to the local
    54  // registry alias for Warehouse packages if not set. Default options can be
    55  // overridden by passing name.Options
    56  func Digest(d string, opts ...name.Option) (name.Digest, error) {
    57  	return name.NewDigest(d,
    58  		append([]name.Option{
    59  			name.WeakValidation,
    60  			name.WithDefaultRegistry(lift.LocalRegistryAlias),
    61  		}, opts...,
    62  		)...,
    63  	)
    64  }
    65  
    66  // TruncatedDigest truncates the result of digest.String() to omit "sha256" and
    67  // capture 13 characters of the actual digest value.
    68  func TruncatedDigest(d string) string {
    69  	return d[7:20]
    70  }
    71  

View as plain text