Skip to content

Commit fd715a9

Browse files
committed
cmd, pkg/podman: Turn Image into an interface
In reaction to the conversation in PR containers#1707, I made suggested changes to the implementation in terms of how Image information, received from Podman, is represented and treated in the Toolbx code. I used the same procedure as in the case of representing Containers: - Commit e611969 - Commit ec7eb59 In short, the JSON from 'podman inspect --type image' and 'podman images' are different, so logically, there should be different implementations of the JSON.Unmarshaler interface [1] for them as well. The new Image interface provides access to the values ​​parsed from the JSONs and two different concrete types, which are implemented separately to handle the differences in the JSONs. GetImages() now returns an Images iterator-like structure that handles image flattening and sorting internally. InspectImage() returns an Image interface instead of map[string]interface{}, and the IsToolboxImage() function was replaced with the IsToolbx() method on the Image interface. [1] https://pkg.go.dev/encoding/json#Unmarshaler Signed-off-by: Dalibor Kricka <[email protected]>
1 parent c66ddd1 commit fd715a9

File tree

6 files changed

+317
-159
lines changed

6 files changed

+317
-159
lines changed

src/cmd/completion.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ func completionImageNames(cmd *cobra.Command, _ []string, _ string) ([]string, c
132132
var imageNames []string
133133
if images, err := getImages(true); err == nil {
134134
for _, image := range images {
135-
if len(image.Names) != 1 {
135+
if len(image.Names()) != 1 {
136136
panic("cannot complete unflattened Image")
137137
}
138138

139-
imageNames = append(imageNames, image.Names[0])
139+
imageNames = append(imageNames, image.Names()[0])
140140
}
141141
}
142142

@@ -149,12 +149,12 @@ func completionImageNamesFiltered(_ *cobra.Command, args []string, _ string) ([]
149149
for _, image := range images {
150150
skip := false
151151

152-
if len(image.Names) != 1 {
152+
if len(image.Names()) != 1 {
153153
panic("cannot complete unflattened Image")
154154
}
155155

156156
for _, arg := range args {
157-
if arg == image.Names[0] {
157+
if arg == image.Names()[0] {
158158
skip = true
159159
break
160160
}
@@ -164,7 +164,7 @@ func completionImageNamesFiltered(_ *cobra.Command, args []string, _ string) ([]
164164
continue
165165
}
166166

167-
imageNames = append(imageNames, image.Names[0])
167+
imageNames = append(imageNames, image.Names()[0])
168168
}
169169
}
170170

src/cmd/list.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"os"
23-
"sort"
2423
"text/tabwriter"
2524

2625
"github.com/containers/toolbox/pkg/podman"
@@ -35,12 +34,6 @@ var (
3534
onlyContainers bool
3635
onlyImages bool
3736
}
38-
39-
// toolboxLabels holds labels used by containers/images that mark them as compatible with Toolbx
40-
toolboxLabels = map[string]string{
41-
"com.github.debarshiray.toolbox": "true",
42-
"com.github.containers.toolbox": "true",
43-
}
4437
)
4538

4639
var listCmd = &cobra.Command{
@@ -154,38 +147,20 @@ func listHelp(cmd *cobra.Command, args []string) {
154147
func getImages(fillNameWithID bool) ([]podman.Image, error) {
155148
logrus.Debug("Fetching all images")
156149
var args []string
157-
images, err := podman.GetImages(args...)
150+
images, err := podman.GetImages(fillNameWithID, true, args...)
158151
if err != nil {
159152
logrus.Debugf("Fetching all images failed: %s", err)
160153
return nil, errors.New("failed to get images")
161154
}
162155

163-
processed := make(map[string]struct{})
164156
var toolboxImages []podman.Image
165157

166-
for _, image := range images {
167-
if _, ok := processed[image.ID]; ok {
168-
continue
158+
for images.Next() {
159+
if image := images.Get(); image.IsToolbx() {
160+
toolboxImages = append(toolboxImages, image)
169161
}
170-
171-
processed[image.ID] = struct{}{}
172-
var isToolboxImage bool
173-
174-
for label := range toolboxLabels {
175-
if _, ok := image.Labels[label]; ok {
176-
isToolboxImage = true
177-
break
178-
}
179-
}
180-
181-
if isToolboxImage {
182-
flattenedImages := image.FlattenNames(fillNameWithID)
183-
toolboxImages = append(toolboxImages, flattenedImages...)
184-
}
185-
186162
}
187163

188-
sort.Sort(podman.ImageSlice(toolboxImages))
189164
return toolboxImages, nil
190165
}
191166

@@ -195,14 +170,14 @@ func listOutput(images []podman.Image, containers []podman.Container) {
195170
fmt.Fprintf(writer, "%s\t%s\t%s\n", "IMAGE ID", "IMAGE NAME", "CREATED")
196171

197172
for _, image := range images {
198-
if len(image.Names) != 1 {
173+
if len(image.Names()) != 1 {
199174
panic("cannot list unflattened Image")
200175
}
201176

202177
fmt.Fprintf(writer, "%s\t%s\t%s\n",
203-
utils.ShortID(image.ID),
204-
image.Names[0],
205-
image.Created)
178+
utils.ShortID(image.ID()),
179+
image.Names()[0],
180+
image.Created())
206181
}
207182

208183
writer.Flush()

src/cmd/rmi.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func rmi(cmd *cobra.Command, args []string) error {
7373
}
7474

7575
for _, image := range toolboxImages {
76-
imageID := image.ID
76+
imageID := image.ID()
7777
if err := podman.RemoveImage(imageID, rmiFlags.forceDelete); err != nil {
7878
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
7979
continue
@@ -90,8 +90,14 @@ func rmi(cmd *cobra.Command, args []string) error {
9090
}
9191

9292
for _, image := range args {
93-
if _, err := podman.IsToolboxImage(image); err != nil {
94-
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
93+
imageObj, err := podman.InspectImage(image)
94+
if err != nil {
95+
fmt.Fprintf(os.Stderr, "Error: failed to inspect image %s\n", image)
96+
continue
97+
}
98+
99+
if !imageObj.IsToolbx() {
100+
fmt.Fprintf(os.Stderr, "Error: %s is not a Toolbx image\n", image)
95101
continue
96102
}
97103

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ sources = files(
2323
'pkg/nvidia/nvidia.go',
2424
'pkg/podman/container.go',
2525
'pkg/podman/errors.go',
26+
'pkg/podman/image.go',
2627
'pkg/podman/podman.go',
2728
'pkg/podman/containerInspect_test.go',
2829
'pkg/shell/shell.go',

0 commit comments

Comments
 (0)