-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
parse.go
63 lines (53 loc) · 1.34 KB
/
parse.go
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
package epoch
import (
"strconv"
"time"
"github.com/moby/buildkit/exporter"
commonexptypes "github.com/moby/buildkit/exporter/exptypes"
"github.com/pkg/errors"
)
const (
frontendSourceDateEpochArg = "build-arg:SOURCE_DATE_EPOCH"
)
func ParseBuildArgs(opt map[string]string) (string, bool) {
v, ok := opt[frontendSourceDateEpochArg]
return v, ok
}
func ParseExporterAttrs(opt map[string]string) (*time.Time, map[string]string, error) {
rest := make(map[string]string, len(opt))
var tm *time.Time
for k, v := range opt {
switch k {
case string(commonexptypes.OptKeySourceDateEpoch):
var err error
tm, err = parseTime(k, v)
if err != nil {
return nil, nil, err
}
default:
rest[k] = v
}
}
return tm, rest, nil
}
func ParseSource(inp *exporter.Source) (*time.Time, bool, error) {
if v, ok := inp.Metadata[commonexptypes.ExporterEpochKey]; ok {
epoch, err := parseTime("", string(v))
if err != nil {
return nil, false, errors.Wrapf(err, "invalid SOURCE_DATE_EPOCH from frontend: %q", v)
}
return epoch, true, nil
}
return nil, false, nil
}
func parseTime(key, value string) (*time.Time, error) {
if value == "" {
return nil, nil
}
sde, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "invalid %s: %s", key, err)
}
tm := time.Unix(sde, 0).UTC()
return &tm, nil
}