stats.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/cli/cli/command/container"
  19. "github.com/docker/docker/api/types/filters"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose/v2/pkg/api"
  22. )
  23. type statsOptions struct {
  24. ProjectOptions *ProjectOptions
  25. all bool
  26. format string
  27. noStream bool
  28. noTrunc bool
  29. }
  30. func statsCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  31. opts := statsOptions{
  32. ProjectOptions: p,
  33. }
  34. cmd := &cobra.Command{
  35. Use: "stats [OPTIONS] [SERVICE]",
  36. Short: "Display a live stream of container(s) resource usage statistics",
  37. Args: cobra.MaximumNArgs(1),
  38. RunE: Adapt(func(ctx context.Context, args []string) error {
  39. return runStats(ctx, dockerCli, opts, args)
  40. }),
  41. ValidArgsFunction: completeServiceNames(dockerCli, p),
  42. }
  43. flags := cmd.Flags()
  44. flags.BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
  45. flags.StringVar(&opts.format, "format", "", `Format output using a custom template:
  46. 'table': Print output in table format with column headers (default)
  47. 'table TEMPLATE': Print output in table format using the given Go template
  48. 'json': Print in JSON format
  49. 'TEMPLATE': Print output using the given Go template.
  50. Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates`)
  51. flags.BoolVar(&opts.noStream, "no-stream", false, "Disable streaming stats and only pull the first result")
  52. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
  53. return cmd
  54. }
  55. func runStats(ctx context.Context, dockerCli command.Cli, opts statsOptions, service []string) error {
  56. name, err := opts.ProjectOptions.toProjectName(ctx, dockerCli)
  57. if err != nil {
  58. return err
  59. }
  60. filter := []filters.KeyValuePair{
  61. filters.Arg("label", fmt.Sprintf("%s=%s", api.ProjectLabel, name)),
  62. }
  63. if len(service) > 0 {
  64. filter = append(filter, filters.Arg("label", fmt.Sprintf("%s=%s", api.ServiceLabel, service[0])))
  65. }
  66. args := filters.NewArgs(filter...)
  67. return container.RunStats(ctx, dockerCli, &container.StatsOptions{
  68. All: opts.all,
  69. NoStream: opts.noStream,
  70. NoTrunc: opts.noTrunc,
  71. Format: opts.format,
  72. Filters: &args,
  73. })
  74. }