Skip to content

Commit

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

#519
  • Loading branch information
HarryMichal committed Aug 14, 2020
1 parent 2ecaaf8 commit ff1fab0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
38 changes: 2 additions & 36 deletions src/cmd/rmi.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 @@ -98,7 +97,7 @@ func rmi(cmd *cobra.Command, args []string) error {

for _, image := range images {
imageID := image[idKey].(string)
if err := removeImage(imageID); err != nil {
if err := podman.RemoveImage(imageID, rmiFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
continue
}
Expand All @@ -119,7 +118,7 @@ func rmi(cmd *cobra.Command, args []string) error {
continue
}

if err := removeImage(image); err != nil {
if err := podman.RemoveImage(image, rmiFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
continue
}
Expand Down Expand Up @@ -149,36 +148,3 @@ func rmiHelp(cmd *cobra.Command, args []string) {
return
}
}

func removeImage(image string) error {
logrus.Debugf("Removing image %s", image)

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

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

args = append(args, image)

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

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 @@ -273,6 +273,39 @@ func RemoveContainer(container string, forceDelete bool) error {
return nil
}

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

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

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

args = append(args, image)

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

if err != nil {
return err
}

return nil
}

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

0 comments on commit ff1fab0

Please sign in to comment.