build.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "context"
  16. "os"
  17. "github.com/compose-spec/compose-go/types"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/api/compose"
  21. "github.com/docker/compose-cli/api/progress"
  22. )
  23. type buildOptions struct {
  24. *projectOptions
  25. composeOptions
  26. quiet bool
  27. pull bool
  28. progress string
  29. args []string
  30. }
  31. func buildCommand(p *projectOptions) *cobra.Command {
  32. opts := buildOptions{
  33. projectOptions: p,
  34. }
  35. cmd := &cobra.Command{
  36. Use: "build [SERVICE...]",
  37. Short: "Build or rebuild services",
  38. RunE: func(cmd *cobra.Command, args []string) error {
  39. if opts.quiet {
  40. devnull, err := os.Open(os.DevNull)
  41. if err != nil {
  42. return err
  43. }
  44. os.Stdout = devnull
  45. }
  46. return runBuild(cmd.Context(), opts, args)
  47. },
  48. }
  49. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
  50. cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
  51. cmd.Flags().StringVar(&opts.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty")`)
  52. cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
  53. cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
  54. cmd.Flags().MarkHidden("parallel") //nolint:errcheck
  55. cmd.Flags().Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
  56. cmd.Flags().MarkHidden("compress") //nolint:errcheck
  57. cmd.Flags().Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
  58. cmd.Flags().MarkHidden("force-rm") //nolint:errcheck
  59. return cmd
  60. }
  61. func runBuild(ctx context.Context, opts buildOptions, services []string) error {
  62. c, err := client.New(ctx)
  63. if err != nil {
  64. return err
  65. }
  66. project, err := opts.toProject(services)
  67. if err != nil {
  68. return err
  69. }
  70. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  71. return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{
  72. Pull: opts.pull,
  73. Progress: opts.progress,
  74. Args: types.NewMapping(opts.args),
  75. })
  76. })
  77. return err
  78. }