package mutate import ( v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/empty" "github.com/google/go-containerregistry/pkg/v1/mutate" ) // TODO: implement v1.Image and use private struct for mutatings a la ggcr? // ReplaceLayers removes all v1.Layers from img and creates a new image containing // layers, preserving the media type and annotations from img. // // NOTE: v1.Layer is used instead of layer.Layer for interop and because this // function does not leverage any of the additional functionality layer.Layer // provides. func ReplaceLayers(img v1.Image, layers ...v1.Layer) (v1.Image, error) { m, err := img.Manifest() if err != nil { return nil, err } i, err := mutate.AppendLayers(empty.Image, layers...) if err != nil { return nil, err } return mutate.Annotations( mutate.MediaType(i, m.MediaType), m.Annotations, ).(v1.Image), nil }