ps.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 cmd
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "strings"
  19. "text/tabwriter"
  20. "github.com/docker/compose-cli/utils/formatter"
  21. "github.com/pkg/errors"
  22. "github.com/spf13/cobra"
  23. "github.com/docker/compose-cli/client"
  24. formatter2 "github.com/docker/compose-cli/formatter"
  25. )
  26. type psOpts struct {
  27. all bool
  28. quiet bool
  29. json bool
  30. }
  31. func (o psOpts) validate() error {
  32. if o.quiet && o.json {
  33. return errors.New(`cannot combine "quiet" and "json" options`)
  34. }
  35. return nil
  36. }
  37. // PsCommand lists containers
  38. func PsCommand() *cobra.Command {
  39. var opts psOpts
  40. cmd := &cobra.Command{
  41. Use: "ps",
  42. Short: "List containers",
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. return runPs(cmd.Context(), opts)
  45. },
  46. }
  47. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
  48. cmd.Flags().BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
  49. cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
  50. return cmd
  51. }
  52. func runPs(ctx context.Context, opts psOpts) error {
  53. err := opts.validate()
  54. if err != nil {
  55. return err
  56. }
  57. c, err := client.New(ctx)
  58. if err != nil {
  59. return errors.Wrap(err, "cannot connect to backend")
  60. }
  61. containers, err := c.ContainerService().List(ctx, opts.all)
  62. if err != nil {
  63. return errors.Wrap(err, "fetch containers")
  64. }
  65. if opts.quiet {
  66. for _, c := range containers {
  67. fmt.Println(c.ID)
  68. }
  69. return nil
  70. }
  71. if opts.json {
  72. j, err := formatter2.ToStandardJSON(containers)
  73. if err != nil {
  74. return err
  75. }
  76. fmt.Println(j)
  77. return nil
  78. }
  79. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  80. fmt.Fprintf(w, "CONTAINER ID\tIMAGE\tCOMMAND\tSTATUS\tPORTS\n")
  81. format := "%s\t%s\t%s\t%s\t%s\n"
  82. for _, c := range containers {
  83. fmt.Fprintf(w, format, c.ID, c.Image, c.Command, c.Status, strings.Join(formatter.PortsToStrings(c.Ports), ", "))
  84. }
  85. return w.Flush()
  86. }