watch.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "fmt"
  17. "github.com/docker/cli/cli/command"
  18. "github.com/docker/compose/v2/internal/locker"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/sirupsen/logrus"
  21. "github.com/spf13/cobra"
  22. )
  23. type watchOptions struct {
  24. *ProjectOptions
  25. quiet bool
  26. noUp bool
  27. }
  28. func watchCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  29. watchOpts := watchOptions{
  30. ProjectOptions: p,
  31. }
  32. buildOpts := buildOptions{
  33. ProjectOptions: p,
  34. }
  35. cmd := &cobra.Command{
  36. Use: "watch [SERVICE...]",
  37. Short: "Watch build context for service and rebuild/refresh containers when files are updated",
  38. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  39. return nil
  40. }),
  41. RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  42. if cmd.Parent().Name() == "alpha" {
  43. logrus.Warn("watch command is now available as a top level command")
  44. }
  45. return runWatch(ctx, dockerCli, backend, watchOpts, buildOpts, args)
  46. }),
  47. ValidArgsFunction: completeServiceNames(dockerCli, p),
  48. }
  49. cmd.Flags().BoolVar(&watchOpts.quiet, "quiet", false, "hide build output")
  50. cmd.Flags().BoolVar(&watchOpts.noUp, "no-up", false, "Do not build & start services before watching")
  51. return cmd
  52. }
  53. func runWatch(ctx context.Context, dockerCli command.Cli, backend api.Service, watchOpts watchOptions, buildOpts buildOptions, services []string) error {
  54. project, err := watchOpts.ToProject(dockerCli, nil)
  55. if err != nil {
  56. return err
  57. }
  58. if err := applyPlatforms(project, true); err != nil {
  59. return err
  60. }
  61. build, err := buildOpts.toAPIBuildOptions(nil)
  62. if err != nil {
  63. return err
  64. }
  65. // validation done -- ensure we have the lockfile for this project before doing work
  66. l, err := locker.NewPidfile(project.Name)
  67. if err != nil {
  68. return fmt.Errorf("cannot take exclusive lock for project %q: %w", project.Name, err)
  69. }
  70. if err := l.Lock(); err != nil {
  71. return fmt.Errorf("cannot take exclusive lock for project %q: %w", project.Name, err)
  72. }
  73. if !watchOpts.noUp {
  74. upOpts := api.UpOptions{
  75. Create: api.CreateOptions{
  76. Build: &build,
  77. Services: services,
  78. RemoveOrphans: false,
  79. Recreate: api.RecreateDiverged,
  80. RecreateDependencies: api.RecreateNever,
  81. Inherit: true,
  82. QuietPull: watchOpts.quiet,
  83. },
  84. Start: api.StartOptions{
  85. Project: project,
  86. Attach: nil,
  87. CascadeStop: false,
  88. Services: services,
  89. },
  90. }
  91. if err := backend.Up(ctx, project, upOpts); err != nil {
  92. return err
  93. }
  94. }
  95. return backend.Watch(ctx, project, services, api.WatchOptions{
  96. Build: build,
  97. })
  98. }