build.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. noCache bool
  31. memory string
  32. }
  33. func buildCommand(p *projectOptions) *cobra.Command {
  34. opts := buildOptions{
  35. projectOptions: p,
  36. }
  37. cmd := &cobra.Command{
  38. Use: "build [SERVICE...]",
  39. Short: "Build or rebuild services",
  40. RunE: func(cmd *cobra.Command, args []string) error {
  41. if opts.quiet {
  42. devnull, err := os.Open(os.DevNull)
  43. if err != nil {
  44. return err
  45. }
  46. os.Stdout = devnull
  47. }
  48. return runBuild(cmd.Context(), opts, args)
  49. },
  50. }
  51. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
  52. cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
  53. cmd.Flags().StringVar(&opts.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty")`)
  54. cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
  55. cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
  56. cmd.Flags().MarkHidden("parallel") //nolint:errcheck
  57. cmd.Flags().Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
  58. cmd.Flags().MarkHidden("compress") //nolint:errcheck
  59. cmd.Flags().Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
  60. cmd.Flags().MarkHidden("force-rm") //nolint:errcheck
  61. cmd.Flags().BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
  62. cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
  63. cmd.Flags().MarkHidden("no-rm") //nolint:errcheck
  64. cmd.Flags().StringVarP(&opts.memory, "memory", "m", "", "Set memory limit for the build container. DEPRECATED")
  65. cmd.Flags().MarkHidden("memory") //nolint:errcheck
  66. return cmd
  67. }
  68. func runBuild(ctx context.Context, opts buildOptions, services []string) error {
  69. c, err := client.New(ctx)
  70. if err != nil {
  71. return err
  72. }
  73. project, err := opts.toProject(services)
  74. if err != nil {
  75. return err
  76. }
  77. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  78. return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{
  79. Pull: opts.pull,
  80. Progress: opts.progress,
  81. Args: types.NewMapping(opts.args),
  82. NoCache: opts.noCache,
  83. })
  84. })
  85. return err
  86. }