From dad7e99ea5247f33ebd74a57201aed92a8a28a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harry=20M=C3=ADchal?= Date: Thu, 25 Jun 2020 02:03:30 +0200 Subject: [PATCH] cmd/list, cmd/rm: Adjust for changes in 'podman ps' in Podman 2.0 These keys in 'podman ps --format json' were renamed in Podman 2.0: * "ID" to "Id" * "Created" to "CreatedAt" * "Status" to "State" The key containing the container's name (ie., "Names") is no longer a string, but a slice of strings. https://github.com/containers/toolbox/pull/471 --- src/cmd/list.go | 42 +++++++++++++++++++++++++++++++++++------- src/cmd/rm.go | 11 +++++++++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/cmd/list.go b/src/cmd/list.go index 3c4bba960..7188701b7 100644 --- a/src/cmd/list.go +++ b/src/cmd/list.go @@ -119,8 +119,15 @@ func listContainers() ([]map[string]interface{}, error) { return nil, errors.New("failed to list containers with label=com.github.debarshiray.toolbox=true") } - containers := utils.JoinJSON("ID", containers_old, containers_new) - containers = utils.SortJSON(containers, "Names", false) + var containers []map[string]interface{} + if podman.CheckVersion("2.0.0") { + containers = utils.JoinJSON("Id", containers_old, containers_new) + containers = utils.SortJSON(containers, "Names", true) + } else { + containers = utils.JoinJSON("ID", containers_old, containers_new) + containers = utils.SortJSON(containers, "Names", false) + } + return containers, nil } @@ -194,6 +201,7 @@ func listOutput(images, containers []map[string]interface{}) { nameKey = "names" createdKey = "created" } + for _, image := range images { id := utils.ShortID(image[idKey].(string)) name := image[nameKey].([]interface{})[0].(string) @@ -218,13 +226,33 @@ func listOutput(images, containers []map[string]interface{}) { "STATUS", "IMAGE NAME") + var idKey, createdKey, statusKey string + if podman.CheckVersion("2.0.0") { + idKey = "Id" + createdKey = "CreatedAt" + statusKey = "State" + } else { + idKey = "ID" + createdKey = "Created" + statusKey = "Status" + } + for _, container := range containers { - id := utils.ShortID(container["ID"].(string)) - name := container["Names"].(string) - created := container["Created"].(string) - status := container["Status"].(string) + id := utils.ShortID(container[idKey].(string)) + + var nameString string + switch name := container["Names"].(type) { + case string: + nameString = name + case []interface{}: + nameString = name[0].(string) + } + + created := container[createdKey].(string) + status := container[statusKey].(string) imageName := container["Image"].(string) - fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s\n", id, name, created, status, imageName) + + fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s\n", id, nameString, created, status, imageName) } writer.Flush() diff --git a/src/cmd/rm.go b/src/cmd/rm.go index a331e9854..f20f90112 100644 --- a/src/cmd/rm.go +++ b/src/cmd/rm.go @@ -85,10 +85,17 @@ func rm(cmd *cobra.Command, args []string) error { return errors.New("failed to list containers with com.github.debarshiray.toolbox=true") } - containers := utils.JoinJSON("ID", containers_old, containers_new) + var idKey string + if podman.CheckVersion("2.0.0") { + idKey = "Id" + } else { + idKey = "ID" + } + + containers := utils.JoinJSON(idKey, containers_old, containers_new) for _, container := range containers { - containerID := container["ID"].(string) + containerID := container[idKey].(string) if err := removeContainer(containerID); err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err) continue