build_win.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "github.com/docker/buildx/build"
  17. "github.com/docker/compose-cli/cli/mobycli"
  18. )
  19. func (s *composeService) windowsBuild(opts map[string]build.Options, mode string) error {
  20. for serviceName, options := range opts {
  21. imageName := serviceName
  22. dockerfile := options.Inputs.DockerfilePath
  23. if options.Inputs.DockerfilePath == "-" { // image needs to be pulled
  24. imageName := options.Tags[0]
  25. err := shellOutMoby("pull", imageName)
  26. if err != nil {
  27. return err
  28. }
  29. } else {
  30. cmd := &commandBuilder{
  31. Path: options.Inputs.ContextPath,
  32. }
  33. cmd.addParams("--build-arg", options.BuildArgs)
  34. cmd.addFlag("--pull", options.Pull)
  35. cmd.addArg("--progress", mode)
  36. cacheFrom := []string{}
  37. for _, cacheImage := range options.CacheFrom {
  38. cacheFrom = append(cacheFrom, cacheImage.Attrs["ref"])
  39. }
  40. cmd.addList("--cache-from", cacheFrom)
  41. cmd.addArg("--file", dockerfile)
  42. cmd.addParams("--label", options.Labels)
  43. cmd.addArg("--network", options.NetworkMode)
  44. cmd.addArg("--target", options.Target)
  45. cmd.addList("--add-host", options.ExtraHosts)
  46. cmd.addArg("--tag", imageName)
  47. err := shellOutMoby(cmd.getArguments()...)
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. return nil
  54. }
  55. func shellOutMoby(args ...string) error {
  56. childExit := make(chan bool)
  57. err := mobycli.RunDocker(childExit, args...)
  58. childExit <- true
  59. return err
  60. }
  61. type commandBuilder struct {
  62. Args []string
  63. Path string
  64. }
  65. func (c *commandBuilder) addArg(name, value string) {
  66. if value != "" {
  67. c.Args = append(c.Args, name, value)
  68. }
  69. }
  70. func (c *commandBuilder) addFlag(name string, flag bool) {
  71. if flag {
  72. c.Args = append(c.Args, name)
  73. }
  74. }
  75. func (c *commandBuilder) addParams(name string, params map[string]string) {
  76. if len(params) > 0 {
  77. for k, v := range params {
  78. c.Args = append(c.Args, name, fmt.Sprintf("%s=%s", k, v))
  79. }
  80. }
  81. }
  82. func (c *commandBuilder) addList(name string, values []string) {
  83. if len(values) > 0 {
  84. for _, v := range values {
  85. c.Args = append(c.Args, name, v)
  86. }
  87. }
  88. }
  89. func (c *commandBuilder) getArguments() []string {
  90. cmd := []string{"build"}
  91. cmd = append(cmd, c.Args...)
  92. cmd = append(cmd, c.Path)
  93. return cmd
  94. }