images.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "os"
  19. "sort"
  20. "strings"
  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. }
  32. func imagesCommand(p *projectOptions, backend api.Service) *cobra.Command {
  33. opts := imageOptions{
  34. projectOptions: p,
  35. }
  36. imgCmd := &cobra.Command{
  37. Use: "images [SERVICE...]",
  38. Short: "List images used by the created containers",
  39. RunE: Adapt(func(ctx context.Context, args []string) error {
  40. return runImages(ctx, backend, opts, args)
  41. }),
  42. ValidArgsFunction: serviceCompletion(p),
  43. }
  44. imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  45. return imgCmd
  46. }
  47. func runImages(ctx context.Context, backend api.Service, opts imageOptions, services []string) error {
  48. projectName, err := opts.toProjectName()
  49. if err != nil {
  50. return err
  51. }
  52. images, err := backend.Images(ctx, projectName, api.ImagesOptions{
  53. Services: services,
  54. })
  55. if err != nil {
  56. return err
  57. }
  58. if opts.Quiet {
  59. ids := []string{}
  60. for _, img := range images {
  61. id := img.ID
  62. if i := strings.IndexRune(img.ID, ':'); i >= 0 {
  63. id = id[i+1:]
  64. }
  65. if !utils.StringContains(ids, id) {
  66. ids = append(ids, id)
  67. }
  68. }
  69. for _, img := range ids {
  70. fmt.Println(img)
  71. }
  72. return nil
  73. }
  74. sort.Slice(images, func(i, j int) bool {
  75. return images[i].ContainerName < images[j].ContainerName
  76. })
  77. return formatter.Print(images, formatter.PRETTY, os.Stdout,
  78. func(w io.Writer) {
  79. for _, img := range images {
  80. id := stringid.TruncateID(img.ID)
  81. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  82. repo := img.Repository
  83. if repo == "" {
  84. repo = "<none>"
  85. }
  86. tag := img.Tag
  87. if tag == "" {
  88. tag = "<none>"
  89. }
  90. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
  91. }
  92. },
  93. "Container", "Repository", "Tag", "Image Id", "Size")
  94. }