Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func rm(cmd *cobra.Command, args []string) error {
return &exitError{exitCode, err}
}

errCount := 0

if rmFlags.deleteAll {
toolboxContainers, err := getContainers()
if err != nil {
Expand All @@ -76,6 +78,7 @@ func rm(cmd *cobra.Command, args []string) error {
containerID := container.ID()
if err := podman.RemoveContainer(containerID, rmFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
errCount++
continue
}
}
Expand All @@ -93,21 +96,29 @@ func rm(cmd *cobra.Command, args []string) error {
containerObj, err := podman.InspectContainer(container)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to inspect container %s\n", container)
errCount++
continue
}

if !containerObj.IsToolbx() {
fmt.Fprintf(os.Stderr, "Error: %s is not a Toolbx container\n", container)
errCount++
continue
}

if err := podman.RemoveContainer(container, rmFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
errCount++
continue
}
}
}

if errCount != 0 {
errMsg := fmt.Sprintf("failed to remove %d containers", errCount)
return errors.New(errMsg)
Comment on lines +118 to +119
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message failed to remove might not be fully accurate in all cases, as errCount is also incremented for inspection failures or if a container is not a Toolbx container. A more generic message would be more appropriate. Also, you can use fmt.Errorf to create a formatted error directly, which is more idiomatic in Go.

Suggested change
errMsg := fmt.Sprintf("failed to remove %d containers", errCount)
return errors.New(errMsg)
return fmt.Errorf("encountered errors with %d containers", errCount)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true that we can encounter errors that may prevent us from even calling podman to remove them

But my reasoning for still sticking with the failed to remove error was:
Any error encountered during the removal process prevents the container or image from being removed. So, in essence, isn’t it a failure of the removal process as a whole?

}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions src/cmd/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func rmi(cmd *cobra.Command, args []string) error {
return &exitError{exitCode, err}
}

errCount := 0

if rmiFlags.deleteAll {
toolboxImages, err := getImages(false)
if err != nil {
Expand All @@ -76,6 +78,7 @@ func rmi(cmd *cobra.Command, args []string) error {
imageID := image.ID
if err := podman.RemoveImage(imageID, rmiFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
errCount++
continue
}
}
Expand All @@ -92,16 +95,23 @@ func rmi(cmd *cobra.Command, args []string) error {
for _, image := range args {
if _, err := podman.IsToolboxImage(image); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
errCount++
continue
}

if err := podman.RemoveImage(image, rmiFlags.forceDelete); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
errCount++
continue
}
}
}

if errCount != 0 {
errMsg := fmt.Sprintf("failed to remove %d images", errCount)
return errors.New(errMsg)
Comment on lines +111 to +112
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency and to be more idiomatic, consider using fmt.Errorf to create the formatted error message directly. A slightly more generic message could also improve clarity.

Suggested change
errMsg := fmt.Sprintf("failed to remove %d images", errCount)
return errors.New(errMsg)
return fmt.Errorf("encountered errors with %d images", errCount)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a lot of places using errors.New(), especially in the files that this PR touches. So with that reasoning I went forward with errors.New.

But I am not against using it :)

}

return nil
}

Expand Down