From 24ef8bb16e38ec4837a821a4853836adfd79c2eb Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 3 Sep 2026 19:14:03 +0200 Subject: [PATCH] run modernize -fix ./... Since I use go 1.26 the go fix does not have all the rules built in, there are newer ones in modernize so run the explicitly to fix more code for go 1.26. Signed-off-by: Paul Holzinger (cherry picked from commit 6c6eac3a5ca8a1c63d28a084af83f34cd6a2e12a) NOTE: not a clean cherry-pick. Signed-off-by: Lokesh Mandvekar --- cmd/podman-testing/main.go | 3 +-- cmd/podman/compose.go | 2 +- cmd/podman/machine/init.go | 3 +-- cmd/podman/machine/ssh.go | 3 +-- cmd/podman/utils/error.go | 3 +-- libpod/oci_conmon_common.go | 3 +-- pkg/api/handlers/compat/auth.go | 3 +-- pkg/api/handlers/decoder.go | 3 ++- pkg/api/handlers/libpod/artifacts.go | 3 +-- pkg/api/handlers/utils/errors.go | 2 +- pkg/bindings/errors.go | 13 ++++++------- pkg/bindings/internal/util/util.go | 4 ++-- pkg/domain/infra/tunnel/artifact.go | 3 +-- pkg/domain/infra/tunnel/images.go | 6 ++---- pkg/machine/wsl/machine.go | 4 ++-- pkg/machine/wsl/usermodenet.go | 4 ++-- pkg/machine/wsl/wutil/wutil.go | 3 +-- test/e2e/common_test.go | 5 +++-- 18 files changed, 30 insertions(+), 40 deletions(-) diff --git a/cmd/podman-testing/main.go b/cmd/podman-testing/main.go index f2b8e9c1f7..715eb0eee0 100644 --- a/cmd/podman-testing/main.go +++ b/cmd/podman-testing/main.go @@ -101,8 +101,7 @@ func main() { } else { fmt.Fprintf(os.Stderr, "Error: %v\n", err) } - var ee *exec.ExitError - if errors.As(err, &ee) { + if ee, ok := errors.AsType[*exec.ExitError](err); ok { if w, ok := ee.Sys().(syscall.WaitStatus); ok { exitCode = w.ExitStatus() } diff --git a/cmd/podman/compose.go b/cmd/podman/compose.go index 9333c73cb4..2570f09efc 100644 --- a/cmd/podman/compose.go +++ b/cmd/podman/compose.go @@ -219,7 +219,7 @@ func composeProviderExec(args []string, stdout io.Writer, stderr io.Writer, warn if err := cmd.Run(); err != nil { // Make sure podman returns with the same exit code as the compose provider. - if exitErr, isExit := err.(*exec.ExitError); isExit { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { registry.SetExitCode(exitErr.ExitCode()) } // Format the error to make it explicit that error did not come diff --git a/cmd/podman/machine/init.go b/cmd/podman/machine/init.go index eb20439958..a0dfd0cfb4 100644 --- a/cmd/podman/machine/init.go +++ b/cmd/podman/machine/init.go @@ -213,13 +213,12 @@ func initMachine(cmd *cobra.Command, args []string) error { } // Check if machine already exists - var errNotExists *define.ErrVMDoesNotExist _, _, err := shim.VMExists(initOpts.Name) // if nil, means we found a vm and need to reject it by name if err == nil { return &define.ErrVMAlreadyExists{Name: initOpts.Name} } - if !errors.As(err, &errNotExists) { + if _, ok := errors.AsType[*define.ErrVMDoesNotExist](err); !ok { return err } diff --git a/cmd/podman/machine/ssh.go b/cmd/podman/machine/ssh.go index 77e91658fc..04813aaf8c 100644 --- a/cmd/podman/machine/ssh.go +++ b/cmd/podman/machine/ssh.go @@ -60,8 +60,7 @@ func ssh(_ *cobra.Command, args []string) error { // it implies podman cannot read its machine files, which is bad mc, vmProvider, err = shim.VMExists(args[0]) if err != nil { - var vmNotExistsErr *define.ErrVMDoesNotExist - if !errors.As(err, &vmNotExistsErr) { + if _, ok := errors.AsType[*define.ErrVMDoesNotExist](err); !ok { return err } sshOpts.Args = append(sshOpts.Args, args[0]) diff --git a/cmd/podman/utils/error.go b/cmd/podman/utils/error.go index 46b1f66135..c6e3b5e6b6 100644 --- a/cmd/podman/utils/error.go +++ b/cmd/podman/utils/error.go @@ -60,8 +60,7 @@ func HandleOSExecError(err error) error { if err == nil { return nil } - var exitError *exec.ExitError - if errors.As(err, &exitError) { + if exitError, ok := errors.AsType[*exec.ExitError](err); ok { // the user command inside the unshare/ssh env has failed // we set the exit code, do not return the error to the user // otherwise "exit status X" will be printed diff --git a/libpod/oci_conmon_common.go b/libpod/oci_conmon_common.go index 28108dab48..af5872fea0 100644 --- a/libpod/oci_conmon_common.go +++ b/libpod/oci_conmon_common.go @@ -673,8 +673,7 @@ func (r *ConmonOCIRuntime) HTTPAttach(ctr *Container, req *http.Request, w http. // isRetryable returns whether the error was caused by a blocked syscall or the // specified operation on a non blocking file descriptor wasn't ready for completion. func isRetryable(err error) bool { - var errno syscall.Errno - if errors.As(err, &errno) { + if errno, ok := errors.AsType[syscall.Errno](err); ok { return errno == syscall.EINTR || errno == syscall.EAGAIN } return false diff --git a/pkg/api/handlers/compat/auth.go b/pkg/api/handlers/compat/auth.go index 67416573f4..e51fb8dbcc 100644 --- a/pkg/api/handlers/compat/auth.go +++ b/pkg/api/handlers/compat/auth.go @@ -49,8 +49,7 @@ func Auth(w http.ResponseWriter, r *http.Request) { } else { var msg string - var unauthErr DockerClient.ErrUnauthorizedForCredentials - if errors.As(err, &unauthErr) { + if _, ok := errors.AsType[DockerClient.ErrUnauthorizedForCredentials](err); ok { msg = "401 Unauthorized" } else { msg = err.Error() diff --git a/pkg/api/handlers/decoder.go b/pkg/api/handlers/decoder.go index 3f46498f0b..64b48d2d8e 100644 --- a/pkg/api/handlers/decoder.go +++ b/pkg/api/handlers/decoder.go @@ -4,6 +4,7 @@ package handlers import ( "encoding/json" + "errors" "reflect" "strconv" "strings" @@ -128,7 +129,7 @@ func convertTimeString(query string) reflect.Value { return reflect.ValueOf(t) } - if _, isParseError := err.(*time.ParseError); isParseError { + if _, ok := errors.AsType[*time.ParseError](err); ok { // Try next format continue } else { diff --git a/pkg/api/handlers/libpod/artifacts.go b/pkg/api/handlers/libpod/artifacts.go index df8d89e464..eb1091ee1a 100644 --- a/pkg/api/handlers/libpod/artifacts.go +++ b/pkg/api/handlers/libpod/artifacts.go @@ -390,8 +390,7 @@ func PushArtifact(w http.ResponseWriter, r *http.Request) { } } - var notFoundErr layout.ImageNotFoundError - if errors.As(err, ¬FoundErr) { + if notFoundErr, ok := errors.AsType[layout.ImageNotFoundError](err); ok { utils.ArtifactNotFound(w, name, notFoundErr) return } diff --git a/pkg/api/handlers/utils/errors.go b/pkg/api/handlers/utils/errors.go index 3f9d3a5700..69be45040a 100644 --- a/pkg/api/handlers/utils/errors.go +++ b/pkg/api/handlers/utils/errors.go @@ -133,7 +133,7 @@ func GetInternalServerError(err error) *BuildError { } func ProcessBuildError(w http.ResponseWriter, err error) { - if buildErr, ok := err.(*BuildError); ok { + if buildErr, ok := errors.AsType[*BuildError](err); ok { Error(w, buildErr.code, buildErr.err) return } diff --git a/pkg/bindings/errors.go b/pkg/bindings/errors.go index bce1ef75eb..989ebb63f5 100644 --- a/pkg/bindings/errors.go +++ b/pkg/bindings/errors.go @@ -55,14 +55,13 @@ func (h *APIResponse) ProcessWithError(unmarshalInto any, unmarshalErrorInto any } func CheckResponseCode(inError error) (int, error) { - switch e := inError.(type) { - case *errorhandling.ErrorModel: - return e.Code(), nil - case *errorhandling.PodConflictErrorModel: - return e.Code(), nil - default: - return -1, errors.New("is not type ErrorModel") + if errModel, ok := errors.AsType[*errorhandling.ErrorModel](inError); ok { + return errModel.Code(), nil } + if podConflictModel, ok := errors.AsType[*errorhandling.PodConflictErrorModel](inError); ok { + return podConflictModel.Code(), nil + } + return -1, errors.New("is not type ErrorModel") } type APIVersionError struct { diff --git a/pkg/bindings/internal/util/util.go b/pkg/bindings/internal/util/util.go index c43dc54064..5c292c4305 100644 --- a/pkg/bindings/internal/util/util.go +++ b/pkg/bindings/internal/util/util.go @@ -12,7 +12,7 @@ import ( ) func IsSimpleType(f reflect.Value) bool { - if _, ok := f.Interface().(fmt.Stringer); ok { + if _, ok := reflect.TypeAssert[fmt.Stringer](f); ok { return true } @@ -25,7 +25,7 @@ func IsSimpleType(f reflect.Value) bool { } func SimpleTypeToParam(f reflect.Value) string { - if s, ok := f.Interface().(fmt.Stringer); ok { + if s, ok := reflect.TypeAssert[fmt.Stringer](f); ok { return s.String() } diff --git a/pkg/domain/infra/tunnel/artifact.go b/pkg/domain/infra/tunnel/artifact.go index 54c37bb7e3..28718f5be7 100644 --- a/pkg/domain/infra/tunnel/artifact.go +++ b/pkg/domain/infra/tunnel/artifact.go @@ -118,8 +118,7 @@ func (ir *ImageEngine) ArtifactAdd(_ context.Context, name string, artifactBlob if err == nil { continue } - var errModel *errorhandling.ErrorModel - if errors.As(err, &errModel) { + if errModel, ok := errors.AsType[*errorhandling.ErrorModel](err); ok { switch errModel.ResponseCode { case http.StatusNotFound, http.StatusMethodNotAllowed: default: diff --git a/pkg/domain/infra/tunnel/images.go b/pkg/domain/infra/tunnel/images.go index 7b34120f8a..d97f5527dc 100644 --- a/pkg/domain/infra/tunnel/images.go +++ b/pkg/domain/infra/tunnel/images.go @@ -225,8 +225,7 @@ func (ir *ImageEngine) Load(_ context.Context, opts entities.ImageLoadOptions) ( if err == nil { return report, nil } - var errModel *errorhandling.ErrorModel - if errors.As(err, &errModel) { + if errModel, ok := errors.AsType[*errorhandling.ErrorModel](err); ok { switch errModel.ResponseCode { case http.StatusNotFound, http.StatusMethodNotAllowed: default: @@ -420,8 +419,7 @@ func (ir *ImageEngine) Build(_ context.Context, containerFiles []string, opts en } logrus.Debugf("BuildLocal failed: %v", err) - var errModel *errorhandling.ErrorModel - if errors.As(err, &errModel) { + if errModel, ok := errors.AsType[*errorhandling.ErrorModel](err); ok { switch errModel.ResponseCode { case http.StatusNotFound, http.StatusMethodNotAllowed: default: diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index 9690c83443..c4f96d462b 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -333,7 +333,7 @@ func launchElevate(operation string) error { } err := winutil.RelaunchElevatedWait() if err != nil { - if eerr, ok := err.(*winutil.ExitCodeError); ok { + if eerr, ok := errors.AsType[*winutil.ExitCodeError](err); ok { if eerr.Code == ErrorSuccessRebootRequired { fmt.Println("Reboot is required to continue installation, please reboot at your convenience") return define.ErrRelaunchSucceeded @@ -374,7 +374,7 @@ func isMsiError(err error) bool { return false } - if eerr, ok := err.(*exec.ExitError); ok { + if eerr, ok := errors.AsType[*exec.ExitError](err); ok { switch eerr.ExitCode() { case 0: fallthrough diff --git a/pkg/machine/wsl/usermodenet.go b/pkg/machine/wsl/usermodenet.go index ca210106c9..2384a87f14 100644 --- a/pkg/machine/wsl/usermodenet.go +++ b/pkg/machine/wsl/usermodenet.go @@ -153,7 +153,7 @@ func stopUserModeNetworking(mc *vmconfigs.MachineConfig) error { err = wslPipe(stopUserModeNet, userModeDist, "bash") if err != nil { - if exitErr, ok := err.(*exec.ExitError); ok { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { switch exitErr.ExitCode() { case 2: err = fmt.Errorf("startup state was missing") @@ -184,7 +184,7 @@ func launchUserModeNetDist(exeFile string) error { if err := wslPipe(cmdStr, userModeDist, "bash"); err != nil { _ = terminateDist(userModeDist) - if exitErr, ok := err.(*exec.ExitError); ok { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { switch exitErr.ExitCode() { case 2: return fmt.Errorf("another user-mode network is running, only one can be used at a time: shut down all machines and run wsl --shutdown if this is unexpected") diff --git a/pkg/machine/wsl/wutil/wutil.go b/pkg/machine/wsl/wutil/wutil.go index 40dd358dd1..6ed0840b95 100644 --- a/pkg/machine/wsl/wutil/wutil.go +++ b/pkg/machine/wsl/wutil/wutil.go @@ -67,8 +67,7 @@ func parseWSLStatus() wslStatus { var outputErr error status, outputErr = matchOutputLine(out) err = cmd.Wait() - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if _, ok := errors.AsType[*exec.ExitError](err); ok { // If `wsl --status` returns an exit error // we assume that WSL isn't installed and // override whatever was returned by diff --git a/test/e2e/common_test.go b/test/e2e/common_test.go index 52ab9bea01..56abdfb46b 100644 --- a/test/e2e/common_test.go +++ b/test/e2e/common_test.go @@ -1122,7 +1122,7 @@ func SkipIfSystemdNotRunning(reason string) { cmd := exec.Command("systemctl", "list-units") err := cmd.Run() if err != nil { - if _, ok := err.(*exec.Error); ok { + if _, ok := errors.AsType[*exec.Error](err); ok { Skip("[notSystemd]: not running " + reason) } Expect(err).ToNot(HaveOccurred()) @@ -1366,7 +1366,8 @@ func (p *PodmanTestIntegration) makeOptions(args []string, options PodmanExecOpt eventsType = "none" } - podmanOptions = append(podmanOptions, + podmanOptions = append( + podmanOptions, "--root", p.Root, "--runroot", p.RunRoot, "--runtime", p.OCIRuntime,