utils.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package docker
  2. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  3. // If OpenStdin is set, then it differs
  4. func CompareConfig(a, b *Config) bool {
  5. if a == nil || b == nil ||
  6. a.OpenStdin || b.OpenStdin {
  7. return false
  8. }
  9. if a.AttachStdout != b.AttachStdout ||
  10. a.AttachStderr != b.AttachStderr ||
  11. a.User != b.User ||
  12. a.Memory != b.Memory ||
  13. a.MemorySwap != b.MemorySwap ||
  14. a.CpuShares != b.CpuShares ||
  15. a.OpenStdin != b.OpenStdin ||
  16. a.Tty != b.Tty {
  17. return false
  18. }
  19. if len(a.Cmd) != len(b.Cmd) ||
  20. len(a.Dns) != len(b.Dns) ||
  21. len(a.Env) != len(b.Env) ||
  22. len(a.PortSpecs) != len(b.PortSpecs) {
  23. return false
  24. }
  25. for i := 0; i < len(a.Cmd); i++ {
  26. if a.Cmd[i] != b.Cmd[i] {
  27. return false
  28. }
  29. }
  30. for i := 0; i < len(a.Dns); i++ {
  31. if a.Dns[i] != b.Dns[i] {
  32. return false
  33. }
  34. }
  35. for i := 0; i < len(a.Env); i++ {
  36. if a.Env[i] != b.Env[i] {
  37. return false
  38. }
  39. }
  40. for i := 0; i < len(a.PortSpecs); i++ {
  41. if a.PortSpecs[i] != b.PortSpecs[i] {
  42. return false
  43. }
  44. }
  45. return true
  46. }
  47. func MergeConfig(userConf, imageConf *Config) {
  48. if userConf.Hostname == "" {
  49. userConf.Hostname = imageConf.Hostname
  50. }
  51. if userConf.User == "" {
  52. userConf.User = imageConf.User
  53. }
  54. if userConf.Memory == 0 {
  55. userConf.Memory = imageConf.Memory
  56. }
  57. if userConf.MemorySwap == 0 {
  58. userConf.MemorySwap = imageConf.MemorySwap
  59. }
  60. if userConf.CpuShares == 0 {
  61. userConf.CpuShares = imageConf.CpuShares
  62. }
  63. if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 {
  64. userConf.PortSpecs = imageConf.PortSpecs
  65. }
  66. if !userConf.Tty {
  67. userConf.Tty = imageConf.Tty
  68. }
  69. if !userConf.OpenStdin {
  70. userConf.OpenStdin = imageConf.OpenStdin
  71. }
  72. if !userConf.StdinOnce {
  73. userConf.StdinOnce = imageConf.StdinOnce
  74. }
  75. if userConf.Env == nil || len(userConf.Env) == 0 {
  76. userConf.Env = imageConf.Env
  77. }
  78. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  79. userConf.Cmd = imageConf.Cmd
  80. }
  81. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  82. userConf.Dns = imageConf.Dns
  83. }
  84. }