From b5de5efb3e2c119bffa0083d39b7ff50409fb44d Mon Sep 17 00:00:00 2001 From: Osama Abdelkader Date: Sat, 27 Sep 2025 23:51:12 +0300 Subject: [PATCH] cmd/podman/system: fix error handling in renumber and migrate commands - Change function signatures to return error instead of calling os.Exit() - Update cobra commands to use RunE instead of Run for proper error handling - Remove unused imports (fmt, os, define) - Remove FIXME comments about error handling inconsistency This allows defer statements to run properly and improves testability. Signed-off-by: Osama Abdelkader --- cmd/podman/system/migrate.go | 17 +++-------------- cmd/podman/system/renumber.go | 16 +++------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/cmd/podman/system/migrate.go b/cmd/podman/system/migrate.go index 3373af027f..d84e1af497 100644 --- a/cmd/podman/system/migrate.go +++ b/cmd/podman/system/migrate.go @@ -3,12 +3,8 @@ package system import ( - "fmt" - "os" - "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" - "github.com/containers/podman/v5/libpod/define" "github.com/containers/podman/v5/pkg/domain/entities" "github.com/spf13/cobra" "go.podman.io/common/pkg/completion" @@ -30,7 +26,7 @@ var ( Args: validate.NoArgs, Short: "Migrate containers", Long: migrateDescription, - Run: migrate, + RunE: migrate, ValidArgsFunction: completion.AutocompleteNone, } ) @@ -52,13 +48,6 @@ func init() { _ = migrateCommand.RegisterFlagCompletionFunc(newRuntimeFlagName, completion.AutocompleteNone) } -func migrate(cmd *cobra.Command, args []string) { - if err := registry.ContainerEngine().Migrate(registry.Context(), migrateOptions); err != nil { - fmt.Println(err) - - // FIXME change this to return the error like other commands - // defer will never run on os.Exit() - os.Exit(define.ExecErrorCodeGeneric) - } - os.Exit(0) +func migrate(cmd *cobra.Command, args []string) error { + return registry.ContainerEngine().Migrate(registry.Context(), migrateOptions) } diff --git a/cmd/podman/system/renumber.go b/cmd/podman/system/renumber.go index 0f755d0bb8..cee6ce66f6 100644 --- a/cmd/podman/system/renumber.go +++ b/cmd/podman/system/renumber.go @@ -3,12 +3,8 @@ package system import ( - "fmt" - "os" - "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" - "github.com/containers/podman/v5/libpod/define" "github.com/spf13/cobra" "go.podman.io/common/pkg/completion" ) @@ -27,7 +23,7 @@ var ( Args: validate.NoArgs, Short: "Migrate lock numbers", Long: renumberDescription, - Run: renumber, + RunE: renumber, ValidArgsFunction: completion.AutocompleteNone, } ) @@ -39,12 +35,6 @@ func init() { }) } -func renumber(cmd *cobra.Command, args []string) { - if err := registry.ContainerEngine().Renumber(registry.Context()); err != nil { - fmt.Println(err) - // FIXME change this to return the error like other commands - // defer will never run on os.Exit() - os.Exit(define.ExecErrorCodeGeneric) - } - os.Exit(0) +func renumber(cmd *cobra.Command, args []string) error { + return registry.ContainerEngine().Renumber(registry.Context()) }