images.go 2.5 KB

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