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/spf13/cobra"
  22. "github.com/docker/compose-cli/api/compose"
  23. "github.com/docker/compose-cli/cli/formatter"
  24. "github.com/docker/compose-cli/utils"
  25. "github.com/docker/docker/pkg/stringid"
  26. "github.com/docker/go-units"
  27. )
  28. type imageOptions struct {
  29. *projectOptions
  30. Quiet bool
  31. }
  32. func imagesCommand(p *projectOptions, backend compose.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: func(cmd *cobra.Command, args []string) error {
  40. return runImages(cmd.Context(), backend, opts, args)
  41. },
  42. }
  43. imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  44. return imgCmd
  45. }
  46. func runImages(ctx context.Context, backend compose.Service, opts imageOptions, services []string) error {
  47. projectName, err := opts.toProjectName()
  48. if err != nil {
  49. return err
  50. }
  51. images, err := backend.Images(ctx, projectName, compose.ImagesOptions{
  52. Services: services,
  53. })
  54. if err != nil {
  55. return err
  56. }
  57. if opts.Quiet {
  58. ids := []string{}
  59. for _, img := range images {
  60. id := img.ID
  61. if i := strings.IndexRune(img.ID, ':'); i >= 0 {
  62. id = id[i+1:]
  63. }
  64. if !utils.StringContains(ids, id) {
  65. ids = append(ids, id)
  66. }
  67. }
  68. for _, img := range ids {
  69. fmt.Println(img)
  70. }
  71. return nil
  72. }
  73. sort.Slice(images, func(i, j int) bool {
  74. return images[i].ContainerName < images[j].ContainerName
  75. })
  76. return formatter.Print(images, formatter.PRETTY, os.Stdout,
  77. func(w io.Writer) {
  78. for _, img := range images {
  79. id := stringid.TruncateID(img.ID)
  80. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  81. repo := img.Repository
  82. if repo == "" {
  83. repo = "<none>"
  84. }
  85. tag := img.Tag
  86. if tag == "" {
  87. tag = "<none>"
  88. }
  89. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
  90. }
  91. },
  92. "Container", "Repository", "Tag", "Image Id", "Size")
  93. }