From dd8983f96cf4d45395c81101c7385c393d72db18 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 8 Oct 2022 19:17:53 +0200 Subject: [PATCH] pkg/pidfile: reduce cyclomatic complexity, and small optimisation Use bytes.TrimSpace instead of using the strings package, which is more performant, and allows us to skip the intermediate variable. Also combined some "if" statements to reduce cyclomatic complexity. Signed-off-by: Sebastiaan van Stijn --- pkg/pidfile/pidfile.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/pidfile/pidfile.go b/pkg/pidfile/pidfile.go index 19a23f2f0f..04c68e20a0 100644 --- a/pkg/pidfile/pidfile.go +++ b/pkg/pidfile/pidfile.go @@ -4,11 +4,11 @@ package pidfile // import "github.com/docker/docker/pkg/pidfile" import ( + "bytes" "fmt" "os" "path/filepath" "strconv" - "strings" "github.com/docker/docker/pkg/system" ) @@ -26,11 +26,9 @@ func checkPIDFileAlreadyExists(path string) error { } return err } - pidString := strings.TrimSpace(string(pidByte)) - if pid, err := strconv.Atoi(pidString); err == nil { - if processExists(pid) { - return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path) - } + pid, err := strconv.Atoi(string(bytes.TrimSpace(pidByte))) + if err == nil && processExists(pid) { + return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path) } return nil }