Files
buildx/util/ocilayout/parse.go
T
Tonis Tiigi 9894189361 imagetools: support oci-layout refs
Add oci-layout:// source and target support to imagetools create and
inspect while keeping merge, filter, and referrer logic shared.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-03-12 22:45:01 -07:00

61 lines
1.1 KiB
Go

package ocilayout
import (
"strings"
"github.com/distribution/reference"
digest "github.com/opencontainers/go-digest"
)
type Ref struct {
Path string
Tag string
Digest digest.Digest
}
const prefix = "oci-layout://"
func Parse(s string) (Ref, bool, error) {
if !strings.HasPrefix(s, prefix) {
return Ref{}, false, nil
}
localPath := strings.TrimPrefix(s, prefix)
var out Ref
if i := strings.LastIndex(localPath, "@"); i >= 0 {
after := localPath[i+1:]
if reference.DigestRegexp.MatchString(after) {
dgst, err := digest.Parse(after)
if err != nil {
return Ref{}, true, err
}
localPath, out.Digest = localPath[:i], dgst
}
}
if i := strings.LastIndex(localPath, ":"); i >= 0 {
after := localPath[i+1:]
if reference.TagRegexp.MatchString(after) {
localPath, out.Tag = localPath[:i], after
}
}
out.Path = localPath
if out.Tag == "" && out.Digest == "" {
out.Tag = "latest"
}
return out, true, nil
}
func (r Ref) String() string {
s := prefix + r.Path
if r.Tag != "" {
return s + ":" + r.Tag
}
if r.Digest != "" {
return s + "@" + r.Digest.String()
}
return s
}