(req dto.PageImage)
| 51 | return &ImageService{} |
| 52 | } |
| 53 | func (u *ImageService) Page(req dto.PageImage) (int64, interface{}, error) { |
| 54 | var ( |
| 55 | list []image.Summary |
| 56 | records []dto.ImageInfo |
| 57 | backDatas []dto.ImageInfo |
| 58 | ) |
| 59 | client, err := docker.NewDockerClient() |
| 60 | if err != nil { |
| 61 | return 0, nil, err |
| 62 | } |
| 63 | defer client.Close() |
| 64 | list, err = client.ImageList(context.Background(), image.ListOptions{}) |
| 65 | if err != nil { |
| 66 | return 0, nil, err |
| 67 | } |
| 68 | containers, _ := client.ContainerList(context.Background(), container.ListOptions{All: true}) |
| 69 | if len(req.Name) != 0 { |
| 70 | length, count := len(list), 0 |
| 71 | for count < length { |
| 72 | hasTag := false |
| 73 | for _, tag := range list[count].RepoTags { |
| 74 | if strings.Contains(tag, req.Name) { |
| 75 | hasTag = true |
| 76 | break |
| 77 | } |
| 78 | } |
| 79 | if !hasTag { |
| 80 | list = append(list[:count], list[(count+1):]...) |
| 81 | length-- |
| 82 | } else { |
| 83 | count++ |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for _, image := range list { |
| 89 | records = append(records, dto.ImageInfo{ |
| 90 | ID: image.ID, |
| 91 | Tags: image.RepoTags, |
| 92 | IsUsed: checkUsed(image.ID, containers), |
| 93 | CreatedAt: time.Unix(image.Created, 0), |
| 94 | Size: image.Size, |
| 95 | }) |
| 96 | } |
| 97 | switch req.OrderBy { |
| 98 | case "size": |
| 99 | sort.Slice(records, func(i, j int) bool { |
| 100 | if req.Order == constant.OrderAsc { |
| 101 | return records[i].Size < records[j].Size |
| 102 | } |
| 103 | return records[i].Size > records[j].Size |
| 104 | }) |
| 105 | case "isUsed": |
| 106 | sort.Slice(records, func(i, j int) bool { |
| 107 | if req.Order == constant.OrderAsc { |
| 108 | return !records[i].IsUsed |
| 109 | } |
| 110 | return records[i].IsUsed |
nothing calls this directly
no test coverage detected