Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ func completionImageNames(cmd *cobra.Command, _ []string, _ string) ([]string, c
if images, err := podman.GetImages(true); err != nil {
logrus.Debugf("Getting all images failed: %s", err)
} else {
for _, image := range images {
if len(image.Names) != 1 {
panic("cannot complete unflattened Image")
}

imageNames = append(imageNames, image.Names[0])
for images.Next() {
image := images.Get()
name := image.Name()
imageNames = append(imageNames, name)
}
}

Expand All @@ -170,15 +168,12 @@ func completionImageNamesFiltered(_ *cobra.Command, args []string, _ string) ([]
if images, err := podman.GetImages(true); err != nil {
logrus.Debugf("Getting all images failed: %s", err)
} else {
for _, image := range images {
for images.Next() {
image := images.Get()
name := image.Name()
skip := false

if len(image.Names) != 1 {
panic("cannot complete unflattened Image")
}

for _, arg := range args {
if arg == image.Names[0] {
if arg == name {
skip = true
break
}
Expand All @@ -188,7 +183,7 @@ func completionImageNamesFiltered(_ *cobra.Command, args []string, _ string) ([]
continue
}

imageNames = append(imageNames, image.Names[0])
imageNames = append(imageNames, name)
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func list(cmd *cobra.Command, args []string) error {
lsImages = false
}

var images []podman.Image
var images *podman.Images
var containers *podman.Containers
var err error

Expand Down Expand Up @@ -130,26 +130,26 @@ func listHelp(cmd *cobra.Command, args []string) {
}
}

func listOutput(images []podman.Image, containers *podman.Containers) {
if len(images) != 0 {
func listOutput(images *podman.Images, containers *podman.Containers) {
if images.Len() != 0 {
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(writer, "%s\t%s\t%s\n", "IMAGE ID", "IMAGE NAME", "CREATED")

for _, image := range images {
if len(image.Names) != 1 {
panic("cannot list unflattened Image")
}
for images.Next() {
image := images.Get()
created := image.Created()
name := image.Name()

id := image.ID()
shortID := utils.ShortID(id)

fmt.Fprintf(writer, "%s\t%s\t%s\n",
utils.ShortID(image.ID),
image.Names[0],
image.Created)
fmt.Fprintf(writer, "%s\t%s\t%s\n", shortID, name, created)
}

writer.Flush()
}

if len(images) != 0 && containers.Len() != 0 {
if images.Len() != 0 && containers.Len() != 0 {
fmt.Println()
}

Expand Down Expand Up @@ -199,11 +199,15 @@ func listOutput(images []podman.Image, containers *podman.Containers) {
}

created := container.Created()
id := container.ID()
image := container.Image()
name := container.Name()

id := container.ID()
shortID := utils.ShortID(id)

status := container.Status()
fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s", utils.ShortID(id), name, created, status, image)

fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s", shortID, name, created, status, image)

if term.IsTerminal(os.Stdout) {
fmt.Fprintf(writer, "%s", resetColor)
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ func rmi(cmd *cobra.Command, args []string) error {
return errors.New("failed to get images")
}

for _, image := range toolboxImages {
imageID := image.ID
for toolboxImages.Next() {
image := toolboxImages.Get()
imageID := image.ID()
if err := podman.RemoveImage(imageID, rmiFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
continue
Expand Down
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ sources = files(
'pkg/nvidia/nvidia.go',
'pkg/podman/container.go',
'pkg/podman/errors.go',
'pkg/podman/image.go',
'pkg/podman/imageImages_test.go',
'pkg/podman/podman.go',
'pkg/podman/containerInspect_test.go',
'pkg/shell/shell.go',
Expand Down
206 changes: 206 additions & 0 deletions src/pkg/podman/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright © 2025 – 2026 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package podman

import (
"encoding/json"

"github.com/containers/toolbox/pkg/utils"
)

type Image interface {
Created() string
ID() string
IsToolbx() bool
Labels() map[string]string
Name() string
Names() []string
}

type Images struct {
data []imageImages
i int
}

type imageImages struct {
created string
id string
labels map[string]string
names []string
}

type imageSlice []imageImages

func (images *Images) Get() Image {
if images == nil {
panic("called Images.Get() on a nil Images")
}

if images.i < 1 {
panic("called Images.Get() without calling Images.Next()")
}

image := &images.data[images.i-1]
return image
}

func (images *Images) Len() int {
if images == nil {
return 0
}

return len(images.data)
}

func (images *Images) Next() bool {
if images == nil {
return false
}

available := images.i < len(images.data)
if available {
images.i++
}

return available
}

func (images *Images) Reset() {
if images == nil {
return
}

images.i = 0
}

func (image *imageImages) Created() string {
return image.created
}

func (image *imageImages) flattenNames(fillNameWithID bool) []imageImages {
var ret []imageImages

names := image.Names()
namesCount := len(names)
if namesCount == 0 {
flattenedImage := *image

if fillNameWithID {
id := image.ID()
shortID := utils.ShortID(id)
flattenedImage.names = []string{shortID}
} else {
flattenedImage.names = []string{"<none>"}
}

ret = []imageImages{flattenedImage}
return ret
}

ret = make([]imageImages, 0, namesCount)

for _, name := range names {
flattenedImage := *image
flattenedImage.names = []string{name}
ret = append(ret, flattenedImage)
}

return ret
}

func (image *imageImages) ID() string {
return image.id
}

func (image *imageImages) IsToolbx() bool {
return isToolbx(image.labels)
}

func (image *imageImages) Labels() map[string]string {
if image.labels == nil {
return nil
}

labelsCount := len(image.labels)
ret := make(map[string]string, labelsCount)
for label, value := range image.labels {
ret[label] = value
}

return ret
}

func (image *imageImages) Name() string {
if len(image.names) != 1 {
panic("cannot get name from unflattened Image")
}

return image.names[0]
}

func (image *imageImages) Names() []string {
if image.names == nil {
return nil
}

namesCount := len(image.names)
ret := make([]string, namesCount)
copy(ret, image.names)
return ret
}

func (image *imageImages) UnmarshalJSON(data []byte) error {
var raw struct {
Created interface{}
ID string
Labels map[string]string
Names []string
}

if err := json.Unmarshal(data, &raw); err != nil {
return err
}

// Until Podman 2.0.x the field 'Created' held a human-readable string in
// format "5 minutes ago". Since Podman 2.1 the field holds an integer with
// Unix time. Go interprets numbers in JSON as float64.
switch value := raw.Created.(type) {
case string:
image.created = value
case float64:
image.created = utils.HumanDuration(int64(value))
}

image.id = raw.ID
image.labels = raw.Labels
image.names = raw.Names
return nil
}

func (images imageSlice) Len() int {
return len(images)
}

func (images imageSlice) Less(i, j int) bool {
nameI := images[i].Name()
nameJ := images[j].Name()
return nameI < nameJ
}

func (images imageSlice) Swap(i, j int) {
images[i], images[j] = images[j], images[i]
}
Loading
Loading