From a21240a319c145928e7f8240446360583eb3ad4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 18 Aug 2021 17:08:30 +0300 Subject: [PATCH] Simplify some if blocks --- internal/common/common_linux.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index c4be7f1..ca01c75 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -75,6 +75,18 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { return 0, err } + if statFile == "uptime" { + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseFloat(f[0], 64) + if err != nil { + return 0, err + } + t := uint64(time.Now().Unix()) - uint64(b) + return t, nil + } if statFile == "stat" { for _, line := range lines { if strings.HasPrefix(line, "btime") { @@ -90,17 +102,6 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { return t, nil } } - } else if statFile == "uptime" { - if len(lines) != 1 { - return 0, fmt.Errorf("wrong uptime format") - } - f := strings.Fields(lines[0]) - b, err := strconv.ParseFloat(f[0], 64) - if err != nil { - return 0, err - } - t := uint64(time.Now().Unix()) - uint64(b) - return t, nil } return 0, fmt.Errorf("could not find btime") @@ -136,10 +137,8 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) { if PathExists(filepath.Join(filename, "capabilities")) { contents, err := ReadLines(filepath.Join(filename, "capabilities")) - if err == nil { - if StringsContains(contents, "control_d") { - role = "host" - } + if err == nil && StringsContains(contents, "control_d") { + role = "host" } } }