|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "github.com/docker/compose-cli/api/compose" |
| 25 | + "github.com/docker/compose-cli/local/moby" |
| 26 | + |
| 27 | + "github.com/compose-spec/compose-go/types" |
| 28 | +) |
| 29 | + |
| 30 | +func (s *composeService) windowsBuild(project *types.Project, options compose.BuildOptions) error { |
| 31 | + projectDir := project.WorkingDir |
| 32 | + for _, service := range project.Services { |
| 33 | + if service.Build != nil { |
| 34 | + imageName := getImageName(service, project.Name) |
| 35 | + dockerfile := service.Build.Dockerfile |
| 36 | + if dockerfile != "" { |
| 37 | + if stat, err := os.Stat(projectDir); err == nil && stat.IsDir() { |
| 38 | + |
| 39 | + dockerfile = filepath.Join(projectDir, dockerfile) |
| 40 | + } |
| 41 | + } |
| 42 | + // build args |
| 43 | + cmd := &commandBuilder{ |
| 44 | + Path: filepath.Join(projectDir, service.Build.Context), |
| 45 | + } |
| 46 | + cmd.addParams("--build-arg", options.Args) |
| 47 | + cmd.addFlag("--pull", options.Pull) |
| 48 | + cmd.addArg("--progress", options.Progress) |
| 49 | + |
| 50 | + cmd.addList("--cache-from", service.Build.CacheFrom) |
| 51 | + cmd.addArg("--file", dockerfile) |
| 52 | + cmd.addParams("--label", service.Build.Labels) |
| 53 | + cmd.addArg("--network", service.Build.Network) |
| 54 | + cmd.addArg("--target", service.Build.Target) |
| 55 | + cmd.addArg("--platform", service.Platform) |
| 56 | + cmd.addArg("--isolation", service.Build.Isolation) |
| 57 | + cmd.addList("--add-host", service.Build.ExtraHosts) |
| 58 | + |
| 59 | + cmd.addArg("--tag", imageName) |
| 60 | + |
| 61 | + args := cmd.getArguments() |
| 62 | + // shell out to moby cli |
| 63 | + err := moby.Exec(args) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +type commandBuilder struct { |
| 73 | + Args []string |
| 74 | + Path string |
| 75 | +} |
| 76 | + |
| 77 | +func (c *commandBuilder) addArg(name, value string) { |
| 78 | + if value != "" { |
| 79 | + c.Args = append(c.Args, name, value) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (c *commandBuilder) addFlag(name string, flag bool) { |
| 84 | + if flag { |
| 85 | + c.Args = append(c.Args, name) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (c *commandBuilder) addParams(name string, params map[string]string) { |
| 90 | + if len(params) > 0 { |
| 91 | + for k, v := range params { |
| 92 | + c.Args = append(c.Args, name, fmt.Sprintf("%s=%s", k, v)) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (c *commandBuilder) addList(name string, values []string) { |
| 98 | + if len(values) > 0 { |
| 99 | + for _, v := range values { |
| 100 | + c.Args = append(c.Args, name, v) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (c *commandBuilder) getArguments() []string { |
| 106 | + cmd := []string{"build"} |
| 107 | + cmd = append(cmd, c.Args...) |
| 108 | + cmd = append(cmd, c.Path) |
| 109 | + return cmd |
| 110 | +} |
0 commit comments