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
20 changes: 16 additions & 4 deletions src/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ func completionCommands(cmd *cobra.Command, _ []string, _ string) ([]string, cob
}

func completionContainerNames(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
logrus.Debug("Getting all containers")

var containerNames []string
if containers, err := getContainers(); err == nil {
for _, container := range containers {

if containers, err := podman.GetContainers(); err != nil {
logrus.Debugf("Getting all containers failed: %s", err)
} else {
for containers.Next() {
container := containers.Get()
name := container.Name()
containerNames = append(containerNames, name)
}
Expand All @@ -90,9 +96,15 @@ func completionContainerNamesFiltered(cmd *cobra.Command, args []string, _ strin
return nil, cobra.ShellCompDirectiveNoFileComp
}

logrus.Debug("Getting all containers")

var containerNames []string
if containers, err := getContainers(); err == nil {
for _, container := range containers {

if containers, err := podman.GetContainers(); err != nil {
logrus.Debugf("Getting all containers failed: %s", err)
} else {
for containers.Next() {
container := containers.Get()
name := container.Name()
skip := false
for _, arg := range args {
Expand Down
39 changes: 12 additions & 27 deletions src/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func list(cmd *cobra.Command, args []string) error {
}

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

if lsImages {
Expand All @@ -96,36 +96,19 @@ func list(cmd *cobra.Command, args []string) error {
}

if lsContainers {
containers, err = getContainers()
logrus.Debug("Getting all containers")

containers, err = podman.GetContainers()
if err != nil {
return err
logrus.Debugf("Getting all containers failed: %s", err)
return errors.New("failed to get containers")
}
}

listOutput(images, containers)
return nil
}

func getContainers() ([]podman.Container, error) {
logrus.Debug("Fetching all containers")
args := []string{"--all", "--sort", "names"}
containers, err := podman.GetContainers(args...)
if err != nil {
logrus.Debugf("Fetching all containers failed: %s", err)
return nil, errors.New("failed to get containers")
}

var toolboxContainers []podman.Container

for containers.Next() {
if container := containers.Get(); container.IsToolbx() {
toolboxContainers = append(toolboxContainers, container)
}
}

return toolboxContainers, nil
}

func listHelp(cmd *cobra.Command, args []string) {
if utils.IsInsideContainer() {
if !utils.IsInsideToolboxContainer() {
Expand All @@ -147,7 +130,7 @@ func listHelp(cmd *cobra.Command, args []string) {
}
}

func listOutput(images []podman.Image, containers []podman.Container) {
func listOutput(images []podman.Image, containers *podman.Containers) {
if len(images) != 0 {
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(writer, "%s\t%s\t%s\n", "IMAGE ID", "IMAGE NAME", "CREATED")
Expand All @@ -166,11 +149,11 @@ func listOutput(images []podman.Image, containers []podman.Container) {
writer.Flush()
}

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

if len(containers) != 0 {
if containers.Len() != 0 {
const boldGreenColor = "\033[1;32m"
const defaultColor = "\033[0;00m" // identical to resetColor, but same length as boldGreenColor
const resetColor = "\033[0m"
Expand All @@ -195,7 +178,9 @@ func listOutput(images []podman.Image, containers []podman.Container) {

fmt.Fprintf(writer, "\n")

for _, container := range containers {
for containers.Next() {
container := containers.Get()

isRunning := false
if podman.CheckVersion("2.0.0") {
status := container.Status()
Expand Down
11 changes: 8 additions & 3 deletions src/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -67,12 +68,16 @@ func rm(cmd *cobra.Command, args []string) error {
}

if rmFlags.deleteAll {
toolboxContainers, err := getContainers()
logrus.Debug("Getting all containers")

toolboxContainers, err := podman.GetContainers()
if err != nil {
return err
logrus.Debugf("Getting all containers failed: %s", err)
return errors.New("failed to get containers")
}

for _, container := range toolboxContainers {
for toolboxContainers.Next() {
container := toolboxContainers.Get()
containerID := container.ID()
if err := podman.RemoveContainer(containerID, rmFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
Expand Down
11 changes: 8 additions & 3 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,16 @@ func runCommand(container string,
return err
}

containers, err := getContainers()
logrus.Debug("Getting all containers")

containers, err := podman.GetContainers()
if err != nil {
logrus.Debugf("Getting all containers failed: %s", err)
err := createErrorContainerNotFound(container)
return err
}

containersCount := len(containers)
containersCount := containers.Len()
logrus.Debugf("Found %d containers", containersCount)

if containersCount == 0 {
Expand Down Expand Up @@ -228,7 +231,9 @@ func runCommand(container string,
} else if containersCount == 1 && defaultContainer {
fmt.Fprintf(os.Stderr, "Error: container %s not found\n", container)

container = containers[0].Name()
containers.Next()
containerObj := containers.Get()
container = containerObj.Name()
fmt.Fprintf(os.Stderr, "Entering container %s instead.\n", container)
fmt.Fprintf(os.Stderr, "Use the 'create' command to create a different Toolbx.\n")
fmt.Fprintf(os.Stderr, "Run '%s --help' for usage.\n", executableBase)
Expand Down
8 changes: 8 additions & 0 deletions src/pkg/podman/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ func (containers *Containers) Get() Container {
return &container
}

func (containers *Containers) Len() int {
if containers == nil {
return 0
}

return len(containers.data)
}

func (containers *Containers) Next() bool {
available := containers.i < len(containers.data)
if available {
Expand Down
17 changes: 11 additions & 6 deletions src/pkg/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,16 @@ func ContainerExists(container string) (bool, error) {
return true, nil
}

// GetContainers is a wrapper function around `podman ps --format json` command.
//
// Parameter args accepts an array of strings to be passed to the wrapped command (eg. ["-a", "--filter", "123"]).
// GetContainers is a wrapper function around `podman ps --format json` command that returns all Toolbx containers
//
// Returned value is a slice of Containers.
//
// If a problem happens during execution, first argument is nil and second argument holds the error message.
func GetContainers(args ...string) (*Containers, error) {
func GetContainers() (*Containers, error) {
var stdout bytes.Buffer

logLevelString := LogLevel.String()
args = append([]string{"--log-level", logLevelString, "ps", "--format", "json"}, args...)
args := []string{"--log-level", logLevelString, "ps", "--all", "--format", "json", "--sort", "names"}

if err := shell.Run("podman", nil, &stdout, nil, args...); err != nil {
return nil, err
Expand All @@ -186,7 +184,14 @@ func GetContainers(args ...string) (*Containers, error) {
return nil, err
}

return &Containers{containers, 0}, nil
var toolbxContainers []containerPS
for _, container := range containers {
if container.IsToolbx() {
toolbxContainers = append(toolbxContainers, container)
}
}

return &Containers{toolbxContainers, 0}, nil
}

// GetImages is a wrapper function around `podman images --format json` command that returns all Toolbx images
Expand Down
Loading