buildflags: merge attest flags if disabled is set

This ensures that `--sbom=<bool>` and `--attest type=sbom`
can be appropriately merged for build, and `--sbom=<bool>` and
`target.attest=["type=sbom"]` can be appropriately merged for bake.

Signed-off-by: Justin Chadwell <me@jedevc.com>
pull/1700/head
Justin Chadwell 2 years ago
parent 3e60bbe30f
commit 1a01779e5b

@ -30,9 +30,16 @@ func ParseAttests(in []string) (map[string]*string, error) {
k := "attest:" + attestType k := "attest:" + attestType
if _, ok := out[k]; ok { if _, ok := out[k]; ok {
if disabled == nil {
return nil, errors.Errorf("duplicate attestation field %s", attestType) return nil, errors.Errorf("duplicate attestation field %s", attestType)
} }
if disabled { if *disabled {
out[k] = nil
}
continue
}
if disabled != nil && *disabled {
out[k] = nil out[k] = nil
} else { } else {
out[k] = &in out[k] = &in
@ -41,23 +48,23 @@ func ParseAttests(in []string) (map[string]*string, error) {
return out, nil return out, nil
} }
func parseAttest(in string) (string, bool, error) { func parseAttest(in string) (string, *bool, error) {
if in == "" { if in == "" {
return "", false, nil return "", nil, nil
} }
csvReader := csv.NewReader(strings.NewReader(in)) csvReader := csv.NewReader(strings.NewReader(in))
fields, err := csvReader.Read() fields, err := csvReader.Read()
if err != nil { if err != nil {
return "", false, err return "", nil, err
} }
attestType := "" attestType := ""
disabled := true var disabled *bool
for _, field := range fields { for _, field := range fields {
key, value, ok := strings.Cut(field, "=") key, value, ok := strings.Cut(field, "=")
if !ok { if !ok {
return "", false, errors.Errorf("invalid value %s", field) return "", nil, errors.Errorf("invalid value %s", field)
} }
key = strings.TrimSpace(strings.ToLower(key)) key = strings.TrimSpace(strings.ToLower(key))
@ -65,14 +72,15 @@ func parseAttest(in string) (string, bool, error) {
case "type": case "type":
attestType = value attestType = value
case "disabled": case "disabled":
disabled, err = strconv.ParseBool(value) b, err := strconv.ParseBool(value)
if err != nil { if err != nil {
return "", false, err return "", nil, err
} }
disabled = &b
} }
} }
if attestType == "" { if attestType == "" {
return "", false, errors.Errorf("attestation type not specified") return "", nil, errors.Errorf("attestation type not specified")
} }
return attestType, disabled, nil return attestType, disabled, nil

Loading…
Cancel
Save