Skip to content

Commit

Permalink
cmd/rm, pkg/podman: Move 'removeContainer' func to podman pkg
Browse files Browse the repository at this point in the history
The 'removeContainer' function should go into 'pkg/podman' because it
wraps around Podman's command. Because it no longer has access to the
commands - 'toolbox rm' - parameters it has a new forceDelete parameter.

containers#519
  • Loading branch information
HarryMichal committed Aug 14, 2020
1 parent 6741ff3 commit 2ecaaf8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
38 changes: 2 additions & 36 deletions src/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/shell"
"github.com/containers/toolbox/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -96,7 +95,7 @@ func rm(cmd *cobra.Command, args []string) error {

for _, container := range containers {
containerID := container[idKey].(string)
if err := removeContainer(containerID); err != nil {
if err := podman.RemoveContainer(containerID, rmFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
continue
}
Expand All @@ -117,7 +116,7 @@ func rm(cmd *cobra.Command, args []string) error {
continue
}

if err := removeContainer(container); err != nil {
if err := podman.RemoveContainer(container, rmFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
continue
}
Expand Down Expand Up @@ -147,36 +146,3 @@ func rmHelp(cmd *cobra.Command, args []string) {
return
}
}

func removeContainer(container string) error {
logrus.Debugf("Removing container %s", container)

logLevelString := podman.LogLevel.String()
args := []string{"--log-level", logLevelString, "rm"}

if rmFlags.forceDelete {
args = append(args, "--force")
}

args = append(args, container)

exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
switch exitCode {
case 0:
if err != nil {
panic("unexpected error: 'podman rm' finished successfully")
}
case 1:
err = fmt.Errorf("container %s does not exist", container)
case 2:
err = fmt.Errorf("container %s is running", container)
default:
err = fmt.Errorf("failed to remove container %s", container)
}

if err != nil {
return err
}

return nil
}
33 changes: 33 additions & 0 deletions src/pkg/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,39 @@ func Pull(imageName string) error {
return nil
}

func RemoveContainer(container string, forceDelete bool) error {
logrus.Debugf("Removing container %s", container)

logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "rm"}

if forceDelete {
args = append(args, "--force")
}

args = append(args, container)

exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
switch exitCode {
case 0:
if err != nil {
panic("unexpected error: 'podman rm' finished successfully")
}
case 1:
err = fmt.Errorf("container %s does not exist", container)
case 2:
err = fmt.Errorf("container %s is running", container)
default:
err = fmt.Errorf("failed to remove container %s", container)
}

if err != nil {
return err
}

return nil
}

func SetLogLevel(logLevel logrus.Level) {
LogLevel = logLevel
}
Expand Down

0 comments on commit 2ecaaf8

Please sign in to comment.