events.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "encoding/json"
  17. "fmt"
  18. "github.com/docker/cli/cli/command"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/docker/compose/v2/pkg/compose"
  21. "github.com/spf13/cobra"
  22. )
  23. type eventsOpts struct {
  24. *composeOptions
  25. json bool
  26. since string
  27. until string
  28. }
  29. func eventsCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  30. opts := eventsOpts{
  31. composeOptions: &composeOptions{
  32. ProjectOptions: p,
  33. },
  34. }
  35. cmd := &cobra.Command{
  36. Use: "events [OPTIONS] [SERVICE...]",
  37. Short: "Receive real time events from containers",
  38. RunE: Adapt(func(ctx context.Context, args []string) error {
  39. return runEvents(ctx, dockerCli, backendOptions, opts, args)
  40. }),
  41. ValidArgsFunction: completeServiceNames(dockerCli, p),
  42. }
  43. cmd.Flags().BoolVar(&opts.json, "json", false, "Output events as a stream of json objects")
  44. cmd.Flags().StringVar(&opts.since, "since", "", "Show all events created since timestamp")
  45. cmd.Flags().StringVar(&opts.until, "until", "", "Stream events until this timestamp")
  46. return cmd
  47. }
  48. func runEvents(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts eventsOpts, services []string) error {
  49. name, err := opts.toProjectName(ctx, dockerCli)
  50. if err != nil {
  51. return err
  52. }
  53. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  54. if err != nil {
  55. return err
  56. }
  57. return backend.Events(ctx, name, api.EventsOptions{
  58. Services: services,
  59. Since: opts.since,
  60. Until: opts.until,
  61. Consumer: func(event api.Event) error {
  62. if opts.json {
  63. marshal, err := json.Marshal(map[string]interface{}{
  64. "time": event.Timestamp,
  65. "type": "container",
  66. "service": event.Service,
  67. "id": event.Container,
  68. "action": event.Status,
  69. "attributes": event.Attributes,
  70. })
  71. if err != nil {
  72. return err
  73. }
  74. _, _ = fmt.Fprintln(dockerCli.Out(), string(marshal))
  75. } else {
  76. _, _ = fmt.Fprintln(dockerCli.Out(), event)
  77. }
  78. return nil
  79. },
  80. })
  81. }