watch.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. "os"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/spf13/cobra"
  20. )
  21. type watchOptions struct {
  22. *ProjectOptions
  23. quiet bool
  24. }
  25. func watchCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  26. opts := watchOptions{
  27. ProjectOptions: p,
  28. }
  29. cmd := &cobra.Command{
  30. Use: "watch [SERVICE...]",
  31. Short: "EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated",
  32. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  33. return nil
  34. }),
  35. RunE: Adapt(func(ctx context.Context, args []string) error {
  36. return runWatch(ctx, backend, opts, args)
  37. }),
  38. ValidArgsFunction: completeServiceNames(p),
  39. }
  40. cmd.Flags().BoolVar(&opts.quiet, "quiet", false, "hide build output")
  41. return cmd
  42. }
  43. func runWatch(ctx context.Context, backend api.Service, opts watchOptions, services []string) error {
  44. fmt.Fprintln(os.Stderr, "watch command is EXPERIMENTAL")
  45. project, err := opts.ToProject(nil)
  46. if err != nil {
  47. return err
  48. }
  49. return backend.Watch(ctx, project, services, api.WatchOptions{})
  50. }