Skip to content

Commit

Permalink
cmd/list, cmd/rm: Adjust for changes in 'podman ps' in Podman 2.0
Browse files Browse the repository at this point in the history
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.

#471
  • Loading branch information
HarryMichal authored and debarshiray committed Jun 25, 2020
1 parent d0af915 commit dad7e99
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
42 changes: 35 additions & 7 deletions src/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
11 changes: 9 additions & 2 deletions src/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dad7e99

Please sign in to comment.