File: parse.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (63 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download | duplicates (3)
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
}