diff --git a/src/cmd/completion.go b/src/cmd/completion.go index e5c4eea7e..0e6fb1fe4 100644 --- a/src/cmd/completion.go +++ b/src/cmd/completion.go @@ -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) } @@ -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 { diff --git a/src/cmd/list.go b/src/cmd/list.go index eb1e96d9e..2646cb17c 100644 --- a/src/cmd/list.go +++ b/src/cmd/list.go @@ -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 { @@ -96,9 +96,12 @@ 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") } } @@ -106,26 +109,6 @@ func list(cmd *cobra.Command, args []string) error { 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() { @@ -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") @@ -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" @@ -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() diff --git a/src/cmd/rm.go b/src/cmd/rm.go index 5b6246ac7..d4d0aac68 100644 --- a/src/cmd/rm.go +++ b/src/cmd/rm.go @@ -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" ) @@ -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) diff --git a/src/cmd/run.go b/src/cmd/run.go index 1de96c7a4..ed421aa68 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -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 { @@ -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) diff --git a/src/pkg/podman/container.go b/src/pkg/podman/container.go index bbc41a4e4..05c1d7d21 100644 --- a/src/pkg/podman/container.go +++ b/src/pkg/podman/container.go @@ -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 { diff --git a/src/pkg/podman/podman.go b/src/pkg/podman/podman.go index 26ff02781..7c2a6388f 100644 --- a/src/pkg/podman/podman.go +++ b/src/pkg/podman/podman.go @@ -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 @@ -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