watch.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. "log"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/fsnotify/fsnotify"
  21. "github.com/mitchellh/mapstructure"
  22. "github.com/pkg/errors"
  23. "golang.org/x/sync/errgroup"
  24. )
  25. type DevelopmentConfig struct {
  26. }
  27. func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error {
  28. fmt.Fprintln(s.stderr(), "not implemented yet")
  29. eg, ctx := errgroup.WithContext(ctx)
  30. err := project.WithServices(services, func(service types.ServiceConfig) error {
  31. var config DevelopmentConfig
  32. if y, ok := service.Extensions["x-develop"]; ok {
  33. err := mapstructure.Decode(y, &config)
  34. if err != nil {
  35. return err
  36. }
  37. }
  38. if service.Build == nil {
  39. return errors.New("can't watch a service without a build section")
  40. }
  41. context := service.Build.Context
  42. watcher, err := fsnotify.NewWatcher()
  43. if err != nil {
  44. return err
  45. }
  46. fmt.Println("watching " + context)
  47. err = watcher.Add(context)
  48. if err != nil {
  49. return err
  50. }
  51. eg.Go(func() error {
  52. defer watcher.Close() //nolint:errcheck
  53. for {
  54. select {
  55. case <-ctx.Done():
  56. return nil
  57. case event := <-watcher.Events:
  58. log.Println("fs event :", event.String())
  59. case err := <-watcher.Errors:
  60. return err
  61. }
  62. }
  63. })
  64. return nil
  65. })
  66. if err != nil {
  67. return err
  68. }
  69. return eg.Wait()
  70. }