WithLabel sets or deletes a label from the image config.
(name, value string)
| 365 | |
| 366 | // WithLabel sets or deletes a label from the image config. |
| 367 | func WithLabel(name, value string) Opts { |
| 368 | return func(dc *dagConfig, dm *dagManifest) error { |
| 369 | // extract the list for platforms to update from the name |
| 370 | name = strings.TrimSpace(name) |
| 371 | platforms := []platform.Platform{} |
| 372 | if name[0] == '[' && strings.Index(name, "]") > 0 { |
| 373 | end := strings.Index(name, "]") |
| 374 | for entry := range strings.SplitSeq(name[1:end], ",") { |
| 375 | entry = strings.TrimSpace(entry) |
| 376 | if entry == "*" { |
| 377 | continue |
| 378 | } |
| 379 | p, err := platform.Parse(entry) |
| 380 | if err != nil { |
| 381 | return fmt.Errorf("failed to parse label platform %s: %w", entry, err) |
| 382 | } |
| 383 | platforms = append(platforms, p) |
| 384 | } |
| 385 | name = name[end+1:] |
| 386 | } |
| 387 | dc.stepsOCIConfig = append(dc.stepsOCIConfig, func(c context.Context, rc *regclient.RegClient, rSrc, rTgt ref.Ref, doc *dagOCIConfig) error { |
| 388 | // if platforms are listed, skip non-matching platforms |
| 389 | if len(platforms) > 0 { |
| 390 | p := doc.oc.GetConfig().Platform |
| 391 | found := false |
| 392 | for _, pe := range platforms { |
| 393 | if platform.Match(p, pe) { |
| 394 | found = true |
| 395 | break |
| 396 | } |
| 397 | } |
| 398 | if !found { |
| 399 | return nil |
| 400 | } |
| 401 | } |
| 402 | changed := false |
| 403 | oc := doc.oc.GetConfig() |
| 404 | if oc.Config.Labels == nil { |
| 405 | oc.Config.Labels = map[string]string{} |
| 406 | } |
| 407 | cur, ok := oc.Config.Labels[name] |
| 408 | if value == "" && ok { |
| 409 | delete(oc.Config.Labels, name) |
| 410 | changed = true |
| 411 | } else if value != "" && value != cur { |
| 412 | oc.Config.Labels[name] = value |
| 413 | changed = true |
| 414 | } |
| 415 | if changed { |
| 416 | doc.oc.SetConfig(oc) |
| 417 | doc.modified = true |
| 418 | doc.newDesc = doc.oc.GetDescriptor() |
| 419 | } |
| 420 | return nil |
| 421 | }) |
| 422 | return nil |
| 423 | } |
| 424 | } |