|
| 1 | +/* |
| 2 | + * Copyright © 2025 – 2026 Red Hat Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package podman |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + |
| 22 | + "github.com/containers/toolbox/pkg/utils" |
| 23 | +) |
| 24 | + |
| 25 | +type Image interface { |
| 26 | + Created() string |
| 27 | + ID() string |
| 28 | + IsToolbx() bool |
| 29 | + Labels() map[string]string |
| 30 | + Name() string |
| 31 | + Names() []string |
| 32 | +} |
| 33 | + |
| 34 | +type Images struct { |
| 35 | + data []imageImages |
| 36 | + i int |
| 37 | +} |
| 38 | + |
| 39 | +type imageImages struct { |
| 40 | + created string |
| 41 | + id string |
| 42 | + labels map[string]string |
| 43 | + names []string |
| 44 | +} |
| 45 | + |
| 46 | +func (images *Images) Get() Image { |
| 47 | + if images.i < 1 { |
| 48 | + panic("called Images.Get() without calling Images.Next()") |
| 49 | + } |
| 50 | + |
| 51 | + image := &images.data[images.i-1] |
| 52 | + return image |
| 53 | +} |
| 54 | + |
| 55 | +func (images *Images) Len() int { |
| 56 | + if images == nil { |
| 57 | + return 0 |
| 58 | + } |
| 59 | + |
| 60 | + return len(images.data) |
| 61 | +} |
| 62 | + |
| 63 | +func (images *Images) Next() bool { |
| 64 | + available := images.i < len(images.data) |
| 65 | + if available { |
| 66 | + images.i++ |
| 67 | + } |
| 68 | + |
| 69 | + return available |
| 70 | +} |
| 71 | + |
| 72 | +func (images *Images) Reset() { |
| 73 | + images.i = 0 |
| 74 | +} |
| 75 | + |
| 76 | +func (image *imageImages) Created() string { |
| 77 | + return image.created |
| 78 | +} |
| 79 | + |
| 80 | +func (image *imageImages) flattenNames(fillNameWithID bool) []imageImages { |
| 81 | + var ret []imageImages |
| 82 | + |
| 83 | + names := image.Names() |
| 84 | + namesCount := len(names) |
| 85 | + if namesCount == 0 { |
| 86 | + flattenedImage := *image |
| 87 | + |
| 88 | + if fillNameWithID { |
| 89 | + id := image.ID() |
| 90 | + shortID := utils.ShortID(id) |
| 91 | + flattenedImage.names = []string{shortID} |
| 92 | + } else { |
| 93 | + flattenedImage.names = []string{"<none>"} |
| 94 | + } |
| 95 | + |
| 96 | + ret = []imageImages{flattenedImage} |
| 97 | + return ret |
| 98 | + } |
| 99 | + |
| 100 | + ret = make([]imageImages, 0, namesCount) |
| 101 | + |
| 102 | + for _, name := range names { |
| 103 | + flattenedImage := *image |
| 104 | + flattenedImage.names = []string{name} |
| 105 | + ret = append(ret, flattenedImage) |
| 106 | + } |
| 107 | + |
| 108 | + return ret |
| 109 | +} |
| 110 | + |
| 111 | +func (image *imageImages) ID() string { |
| 112 | + return image.id |
| 113 | +} |
| 114 | + |
| 115 | +func (image *imageImages) IsToolbx() bool { |
| 116 | + return isToolbx(image.labels) |
| 117 | +} |
| 118 | + |
| 119 | +func (image *imageImages) Labels() map[string]string { |
| 120 | + return image.labels |
| 121 | +} |
| 122 | + |
| 123 | +func (image *imageImages) Name() string { |
| 124 | + if len(image.names) != 1 { |
| 125 | + panic("cannot get name from unflattened Image") |
| 126 | + } |
| 127 | + return image.names[0] |
| 128 | +} |
| 129 | + |
| 130 | +func (image *imageImages) Names() []string { |
| 131 | + if image.names == nil { |
| 132 | + return nil |
| 133 | + } |
| 134 | + |
| 135 | + namesCount := len(image.names) |
| 136 | + ret := make([]string, namesCount) |
| 137 | + copy(ret, image.names) |
| 138 | + return ret |
| 139 | +} |
| 140 | + |
| 141 | +func (image *imageImages) UnmarshalJSON(data []byte) error { |
| 142 | + var raw struct { |
| 143 | + Created interface{} |
| 144 | + ID string |
| 145 | + Labels map[string]string |
| 146 | + Names []string |
| 147 | + } |
| 148 | + |
| 149 | + if err := json.Unmarshal(data, &raw); err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + // Until Podman 2.0.x the field 'Created' held a human-readable string in |
| 154 | + // format "5 minutes ago". Since Podman 2.1 the field holds an integer with |
| 155 | + // Unix time. Go interprets numbers in JSON as float64. |
| 156 | + switch value := raw.Created.(type) { |
| 157 | + case string: |
| 158 | + image.created = value |
| 159 | + case float64: |
| 160 | + image.created = utils.HumanDuration(int64(value)) |
| 161 | + } |
| 162 | + |
| 163 | + image.id = raw.ID |
| 164 | + image.labels = raw.Labels |
| 165 | + image.names = raw.Names |
| 166 | + return nil |
| 167 | +} |
0 commit comments