logs.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/spf13/cobra"
  18. "github.com/docker/compose-cli/api/context/store"
  19. "github.com/docker/compose-cli/cli/formatter"
  20. "github.com/docker/compose-cli/pkg/api"
  21. )
  22. type logsOptions struct {
  23. *projectOptions
  24. composeOptions
  25. follow bool
  26. tail string
  27. since string
  28. until string
  29. noColor bool
  30. noPrefix bool
  31. timestamps bool
  32. }
  33. func logsCommand(p *projectOptions, contextType string, backend api.Service) *cobra.Command {
  34. opts := logsOptions{
  35. projectOptions: p,
  36. }
  37. logsCmd := &cobra.Command{
  38. Use: "logs [SERVICE...]",
  39. Short: "View output from containers",
  40. RunE: Adapt(func(ctx context.Context, args []string) error {
  41. return runLogs(ctx, backend, opts, args)
  42. }),
  43. ValidArgsFunction: serviceCompletion(p),
  44. }
  45. flags := logsCmd.Flags()
  46. flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output.")
  47. flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
  48. flags.StringVar(&opts.until, "until", "", "Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
  49. flags.BoolVar(&opts.noColor, "no-color", false, "Produce monochrome output.")
  50. flags.BoolVar(&opts.noPrefix, "no-log-prefix", false, "Don't print prefix in logs.")
  51. flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps.")
  52. if contextType == store.DefaultContextType {
  53. flags.StringVar(&opts.tail, "tail", "all", "Number of lines to show from the end of the logs for each container.")
  54. }
  55. return logsCmd
  56. }
  57. func runLogs(ctx context.Context, backend api.Service, opts logsOptions, services []string) error {
  58. projectName, err := opts.toProjectName()
  59. if err != nil {
  60. return err
  61. }
  62. consumer := formatter.NewLogConsumer(ctx, os.Stdout, !opts.noColor, !opts.noPrefix)
  63. return backend.Logs(ctx, projectName, consumer, api.LogOptions{
  64. Services: services,
  65. Follow: opts.follow,
  66. Tail: opts.tail,
  67. Since: opts.since,
  68. Until: opts.until,
  69. Timestamps: opts.timestamps,
  70. })
  71. }