convert.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "fmt"
  16. "time"
  17. compose "github.com/compose-spec/compose-go/types"
  18. "github.com/docker/docker/api/types/container"
  19. )
  20. // ToMobyEnv convert into []string
  21. func ToMobyEnv(environment compose.MappingWithEquals) []string {
  22. var env []string
  23. for k, v := range environment {
  24. if v == nil {
  25. env = append(env, k)
  26. } else {
  27. env = append(env, fmt.Sprintf("%s=%s", k, *v))
  28. }
  29. }
  30. return env
  31. }
  32. // ToMobyHealthCheck convert into container.HealthConfig
  33. func ToMobyHealthCheck(check *compose.HealthCheckConfig) *container.HealthConfig {
  34. if check == nil {
  35. return nil
  36. }
  37. var (
  38. interval time.Duration
  39. timeout time.Duration
  40. period time.Duration
  41. retries int
  42. )
  43. if check.Interval != nil {
  44. interval = time.Duration(*check.Interval)
  45. }
  46. if check.Timeout != nil {
  47. timeout = time.Duration(*check.Timeout)
  48. }
  49. if check.StartPeriod != nil {
  50. period = time.Duration(*check.StartPeriod)
  51. }
  52. if check.Retries != nil {
  53. retries = int(*check.Retries)
  54. }
  55. test := check.Test
  56. if check.Disable {
  57. test = []string{"NONE"}
  58. }
  59. return &container.HealthConfig{
  60. Test: test,
  61. Interval: interval,
  62. Timeout: timeout,
  63. StartPeriod: period,
  64. Retries: retries,
  65. }
  66. }
  67. // ToSeconds convert into seconds
  68. func ToSeconds(d *compose.Duration) *int {
  69. if d == nil {
  70. return nil
  71. }
  72. s := int(time.Duration(*d).Seconds())
  73. return &s
  74. }