Skip to content

Commit 616501d

Browse files
DaliborKrdebarshiray
authored andcommitted
cmd/list, pkg/podman: Make podman.GetImages() flatten, not getImages()
This is a step towards eliminating or reducing the code in getImages() by doing everything or more in podman.GetImages(). If nothing else, this removes the need to export Image.FlattenNames() from the podman package. The getImages() function originated as listContainers(), when it invoked 'podman images --filter label=...' through podman.GetImages() separately for each label used to identify Toolbx images, and then joined and sorted the results. This changed in commit 2369da5 when getImages() started invoking 'podman images' only once through podman.GetImages() to get all available images, and then filtered them itself based on the labels. Before this change in commit 2369da5, it probably made some sense to keep podman.GetImages() only as a thin wrapper to invoke 'podman images' with different options, and to do everything else in getImages(). However, since then more is being done inside the podman package (eg., unmarshalling the 'podman images' JSON in commit 5f324d5 and flattening the images in commit 6aab0a6), and getImages() is the only caller of podman.GetImages(). Therefore, it looks awkward to have the code to get all Toolbx images split across two functions in different packages. containers#1724 containers#1772
1 parent 52ef378 commit 616501d

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/cmd/list.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,15 @@ func listHelp(cmd *cobra.Command, args []string) {
154154
func getImages(fillNameWithID bool) ([]podman.Image, error) {
155155
logrus.Debug("Fetching all images")
156156
var args []string
157-
images, err := podman.GetImages(args...)
157+
images, err := podman.GetImages(fillNameWithID, args...)
158158
if err != nil {
159159
logrus.Debugf("Fetching all images failed: %s", err)
160160
return nil, errors.New("failed to get images")
161161
}
162162

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

166165
for _, image := range images {
167-
if _, ok := processed[image.ID]; ok {
168-
continue
169-
}
170-
171-
processed[image.ID] = struct{}{}
172166
var isToolboxImage bool
173167

174168
for label := range toolboxLabels {
@@ -179,8 +173,7 @@ func getImages(fillNameWithID bool) ([]podman.Image, error) {
179173
}
180174

181175
if isToolboxImage {
182-
flattenedImages := image.FlattenNames(fillNameWithID)
183-
toolboxImages = append(toolboxImages, flattenedImages...)
176+
toolboxImages = append(toolboxImages, image)
184177
}
185178

186179
}

src/pkg/podman/podman.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353
LogLevel = logrus.ErrorLevel
5454
)
5555

56-
func (image *Image) FlattenNames(fillNameWithID bool) []Image {
56+
func (image *Image) flattenNames(fillNameWithID bool) []Image {
5757
var ret []Image
5858

5959
if len(image.Names) == 0 {
@@ -190,12 +190,14 @@ func GetContainers(args ...string) (*Containers, error) {
190190

191191
// GetImages is a wrapper function around `podman images --format json` command.
192192
//
193+
// Parameter fillNameWithID is a boolean that indicates if the image names should be filled with the ID, when there
194+
// are no names.
193195
// Parameter args accepts an array of strings to be passed to the wrapped command (eg. ["-a", "--filter", "123"]).
194196
//
195197
// Returned value is a slice of Images.
196198
//
197199
// If a problem happens during execution, first argument is nil and second argument holds the error message.
198-
func GetImages(args ...string) ([]Image, error) {
200+
func GetImages(fillNameWithID bool, args ...string) ([]Image, error) {
199201
var stdout bytes.Buffer
200202

201203
logLevelString := LogLevel.String()
@@ -210,6 +212,19 @@ func GetImages(args ...string) ([]Image, error) {
210212
return nil, err
211213
}
212214

215+
processedIDs := make(map[string]struct{})
216+
var processedImages []Image
217+
218+
for _, image := range images {
219+
if _, ok := processedIDs[image.ID]; ok {
220+
continue
221+
}
222+
223+
processedIDs[image.ID] = struct{}{}
224+
flattenedImages := image.flattenNames(fillNameWithID)
225+
processedImages = append(processedImages, flattenedImages...)
226+
}
227+
213228
return images, nil
214229
}
215230

0 commit comments

Comments
 (0)