images.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "io"
  18. "sort"
  19. "strings"
  20. "github.com/docker/cli/cli/command"
  21. "github.com/docker/docker/pkg/stringid"
  22. "github.com/docker/go-units"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v2/cmd/formatter"
  25. "github.com/docker/compose/v2/pkg/api"
  26. "github.com/docker/compose/v2/pkg/utils"
  27. )
  28. type imageOptions struct {
  29. *ProjectOptions
  30. Quiet bool
  31. Format string
  32. }
  33. func imagesCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  34. opts := imageOptions{
  35. ProjectOptions: p,
  36. }
  37. imgCmd := &cobra.Command{
  38. Use: "images [OPTIONS] [SERVICE...]",
  39. Short: "List images used by the created containers",
  40. RunE: Adapt(func(ctx context.Context, args []string) error {
  41. return runImages(ctx, dockerCli, backend, opts, args)
  42. }),
  43. ValidArgsFunction: completeServiceNames(dockerCli, p),
  44. }
  45. imgCmd.Flags().StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json]")
  46. imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  47. return imgCmd
  48. }
  49. func runImages(ctx context.Context, dockerCli command.Cli, backend api.Service, opts imageOptions, services []string) error {
  50. projectName, err := opts.toProjectName(ctx, dockerCli)
  51. if err != nil {
  52. return err
  53. }
  54. images, err := backend.Images(ctx, projectName, api.ImagesOptions{
  55. Services: services,
  56. })
  57. if err != nil {
  58. return err
  59. }
  60. if opts.Quiet {
  61. ids := []string{}
  62. for _, img := range images {
  63. id := img.ID
  64. if i := strings.IndexRune(img.ID, ':'); i >= 0 {
  65. id = id[i+1:]
  66. }
  67. if !utils.StringContains(ids, id) {
  68. ids = append(ids, id)
  69. }
  70. }
  71. for _, img := range ids {
  72. _, _ = fmt.Fprintln(dockerCli.Out(), img)
  73. }
  74. return nil
  75. }
  76. sort.Slice(images, func(i, j int) bool {
  77. return images[i].ContainerName < images[j].ContainerName
  78. })
  79. return formatter.Print(images, opts.Format, dockerCli.Out(),
  80. func(w io.Writer) {
  81. for _, img := range images {
  82. id := stringid.TruncateID(img.ID)
  83. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  84. repo := img.Repository
  85. if repo == "" {
  86. repo = "<none>"
  87. }
  88. tag := img.Tag
  89. if tag == "" {
  90. tag = "<none>"
  91. }
  92. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
  93. }
  94. },
  95. "CONTAINER", "REPOSITORY", "TAG", "IMAGE ID", "SIZE")
  96. }