images.go 2.8 KB

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