labels.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package labels
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/docker/libcompose/utils"
  6. )
  7. // Label represents a docker label.
  8. type Label string
  9. // Libcompose default labels.
  10. const (
  11. NUMBER = Label("com.docker.compose.container-number")
  12. ONEOFF = Label("com.docker.compose.oneoff")
  13. PROJECT = Label("com.docker.compose.project")
  14. SERVICE = Label("com.docker.compose.service")
  15. HASH = Label("com.docker.compose.config-hash")
  16. VERSION = Label("com.docker.compose.version")
  17. PROJECT_LEGACY = Label("io.docker.compose.project")
  18. SERVICE_LEGACY = Label("io.docker.compose.service")
  19. HASH_LEGACY = Label("io.docker.compose.config-hash")
  20. )
  21. // EqString returns a label json string representation with the specified value.
  22. func (f Label) EqString(value string) string {
  23. return LabelFilterString(string(f), value)
  24. }
  25. // Eq returns a label map representation with the specified value.
  26. func (f Label) Eq(value string) map[string][]string {
  27. return LabelFilter(string(f), value)
  28. }
  29. // AndString returns a json list of labels by merging the two specified values (left and right) serialized as string.
  30. func AndString(left, right string) string {
  31. leftMap := map[string][]string{}
  32. rightMap := map[string][]string{}
  33. // Ignore errors
  34. json.Unmarshal([]byte(left), &leftMap)
  35. json.Unmarshal([]byte(right), &rightMap)
  36. for k, v := range rightMap {
  37. existing, ok := leftMap[k]
  38. if ok {
  39. leftMap[k] = append(existing, v...)
  40. } else {
  41. leftMap[k] = v
  42. }
  43. }
  44. result, _ := json.Marshal(leftMap)
  45. return string(result)
  46. }
  47. // And returns a map of labels by merging the two specified values (left and right).
  48. func And(left, right map[string][]string) map[string][]string {
  49. result := map[string][]string{}
  50. for k, v := range left {
  51. result[k] = v
  52. }
  53. for k, v := range right {
  54. existing, ok := result[k]
  55. if ok {
  56. result[k] = append(existing, v...)
  57. } else {
  58. result[k] = v
  59. }
  60. }
  61. return result
  62. }
  63. // Str returns the label name.
  64. func (f Label) Str() string {
  65. return string(f)
  66. }
  67. // LabelFilterString returns a label json string representation of the specifed couple (key,value)
  68. // that is used as filter for docker.
  69. func LabelFilterString(key, value string) string {
  70. return utils.FilterString(map[string][]string{
  71. "label": {fmt.Sprintf("%s=%s", key, value)},
  72. })
  73. }
  74. // LabelFilter returns a label map representation of the specifed couple (key,value)
  75. // that is used as filter for docker.
  76. func LabelFilter(key, value string) map[string][]string {
  77. return map[string][]string{
  78. "label": {fmt.Sprintf("%s=%s", key, value)},
  79. }
  80. }