File: export.go

package info (click to toggle)
docker-buildx 0.19.3%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,852 kB
  • sloc: sh: 318; makefile: 73
file content (146 lines) | stat: -rw-r--r-- 3,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package buildflags

import (
	"regexp"
	"strings"

	"github.com/containerd/platforms"
	controllerapi "github.com/docker/buildx/controller/pb"
	"github.com/moby/buildkit/client"
	"github.com/moby/buildkit/exporter/containerimage/exptypes"
	ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/pkg/errors"
	"github.com/tonistiigi/go-csvvalue"
)

func ParseExports(inp []string) ([]*controllerapi.ExportEntry, error) {
	var outs []*controllerapi.ExportEntry
	if len(inp) == 0 {
		return nil, nil
	}
	for _, s := range inp {
		if s == "" {
			continue
		}
		fields, err := csvvalue.Fields(s, nil)
		if err != nil {
			return nil, err
		}

		out := controllerapi.ExportEntry{
			Attrs: map[string]string{},
		}
		if len(fields) == 1 && fields[0] == s && !strings.HasPrefix(s, "type=") {
			if s != "-" {
				outs = append(outs, &controllerapi.ExportEntry{
					Type:        client.ExporterLocal,
					Destination: s,
				})
				continue
			}
			out = controllerapi.ExportEntry{
				Type:        client.ExporterTar,
				Destination: s,
			}
		}

		if out.Type == "" {
			for _, field := range fields {
				parts := strings.SplitN(field, "=", 2)
				if len(parts) != 2 {
					return nil, errors.Errorf("invalid value %s", field)
				}
				key := strings.TrimSpace(strings.ToLower(parts[0]))
				value := parts[1]
				switch key {
				case "type":
					out.Type = value
				default:
					out.Attrs[key] = value
				}
			}
		}
		if out.Type == "" {
			return nil, errors.Errorf("type is required for output")
		}

		if out.Type == "registry" {
			out.Type = client.ExporterImage
			if _, ok := out.Attrs["push"]; !ok {
				out.Attrs["push"] = "true"
			}
		}

		if dest, ok := out.Attrs["dest"]; ok {
			out.Destination = dest
			delete(out.Attrs, "dest")
		}

		outs = append(outs, &out)
	}
	return outs, nil
}

func ParseAnnotations(inp []string) (map[exptypes.AnnotationKey]string, error) {
	// TODO: use buildkit's annotation parser once it supports setting custom prefix and ":" separator

	// type followed by optional platform specifier in square brackets
	annotationTypeRegexp := regexp.MustCompile(`^([a-z-]+)(?:\[([A-Za-z0-9_/-]+)\])?$`)

	annotations := make(map[exptypes.AnnotationKey]string)
	for _, inp := range inp {
		k, v, ok := strings.Cut(inp, "=")
		if !ok {
			return nil, errors.Errorf("invalid annotation %q, expected key=value", inp)
		}

		types, key, ok := strings.Cut(k, ":")
		if !ok {
			// no types specified, swap Cut outputs
			key = types

			ak := exptypes.AnnotationKey{Key: key}
			annotations[ak] = v
			continue
		}

		typesSplit := strings.Split(types, ",")
		for _, typeAndPlatform := range typesSplit {
			groups := annotationTypeRegexp.FindStringSubmatch(typeAndPlatform)
			if groups == nil {
				return nil, errors.Errorf(
					"invalid annotation type %q, expected type and optional platform in square brackets",
					typeAndPlatform)
			}

			typ, platform := groups[1], groups[2]

			switch typ {
			case "":
			case exptypes.AnnotationIndex,
				exptypes.AnnotationIndexDescriptor,
				exptypes.AnnotationManifest,
				exptypes.AnnotationManifestDescriptor:
			default:
				return nil, errors.Errorf("unknown annotation type %q", typ)
			}

			var ociPlatform *ocispecs.Platform
			if platform != "" {
				p, err := platforms.Parse(platform)
				if err != nil {
					return nil, errors.Wrapf(err, "invalid platform %q", platform)
				}
				ociPlatform = &p
			}

			ak := exptypes.AnnotationKey{
				Type:     typ,
				Platform: ociPlatform,
				Key:      key,
			}
			annotations[ak] = v
		}
	}
	return annotations, nil
}