cmd/rm, pkg/podman: Move 'removeContainer' func to podman pkg
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. https://github.com/containers/toolbox/pull/519
This commit is contained in:
parent
6741ff3a26
commit
2ecaaf8ba3
2 changed files with 35 additions and 36 deletions
|
@ -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"
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue