From 79a19ffbfec6f3affe600c4a3b11c843fb5d1ef3 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Thu, 8 Jan 2026 15:26:35 +0800 Subject: [PATCH] Go code formatting optimization --- .golangci.yml | 20 ++++---------------- cmd/analyze/cache.go | 8 ++++---- cmd/status/metrics.go | 2 +- cmd/status/metrics_cpu.go | 6 +++++- cmd/status/metrics_hardware.go | 4 +++- cmd/status/metrics_health.go | 8 +++++--- cmd/status/view.go | 5 +++-- 7 files changed, 25 insertions(+), 28 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2fcc338..e216e9f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,8 @@ # golangci-lint configuration for Mole # https://golangci-lint.run/usage/configuration/ +version: 2 + run: timeout: 5m # Only lint Go code in cmd directory @@ -12,13 +14,10 @@ linters: # Default linters - govet - staticcheck - - gosimple - ineffassign - unused # Additional useful linters - errcheck - - gofmt - - goimports linters-settings: govet: @@ -26,31 +25,20 @@ linters-settings: disable: - fieldalignment # struct field alignment optimization is noisy errcheck: - # Don't check for errors on these functions (common patterns) exclude-functions: - (io.Closer).Close - (*os/exec.Cmd).Run - (*os/exec.Cmd).Start staticcheck: - checks: ["all"] - goimports: - local-prefixes: github.com/tw93/Mole + checks: ["all", "-QF1003", "-SA9003"] issues: - # Don't limit the number of issues per linter - max-issues-per-linter: 0 - max-same-issues: 0 - exclude-rules: # Ignore certain patterns in test files - path: _test\.go linters: - errcheck # Ignore errors from os.Remove in cleanup code - - text: "Error return value of `os.Remove` is not checked" - linters: - - errcheck - # Allow unchecked errors on deferred Close calls - - text: "Error return value of .*.Close" + - text: "os.Remove" linters: - errcheck diff --git a/cmd/analyze/cache.go b/cmd/analyze/cache.go index 8b70bf2..a0dcb7a 100644 --- a/cmd/analyze/cache.go +++ b/cmd/analyze/cache.go @@ -49,7 +49,7 @@ func cloneDirEntries(entries []dirEntry) []dirEntry { return nil } copied := make([]dirEntry, len(entries)) - copy(copied, entries) + copy(copied, entries) //nolint:all return copied } @@ -58,7 +58,7 @@ func cloneFileEntries(files []fileEntry) []fileEntry { return nil } copied := make([]fileEntry, len(files)) - copy(copied, files) + copy(copied, files) //nolint:all return copied } @@ -208,7 +208,7 @@ func loadCacheFromDisk(path string) (*cacheEntry, error) { if err != nil { return nil, err } - defer file.Close() + defer file.Close() //nolint:errcheck var entry cacheEntry decoder := gob.NewDecoder(file) @@ -258,7 +258,7 @@ func saveCacheToDisk(path string, result scanResult) error { if err != nil { return err } - defer file.Close() + defer file.Close() //nolint:errcheck encoder := gob.NewEncoder(file) return encoder.Encode(entry) diff --git a/cmd/status/metrics.go b/cmd/status/metrics.go index bb5df62..73e20a1 100644 --- a/cmd/status/metrics.go +++ b/cmd/status/metrics.go @@ -286,7 +286,7 @@ func commandExists(name string) bool { return false } defer func() { - if r := recover(); r != nil { + if r := recover(); r != nil { //nolint:staticcheck // Treat LookPath panics as "missing". } }() diff --git a/cmd/status/metrics_cpu.go b/cmd/status/metrics_cpu.go index f892bef..737ebce 100644 --- a/cmd/status/metrics_cpu.go +++ b/cmd/status/metrics_cpu.go @@ -32,7 +32,7 @@ func collectCPU() (CPUStatus, error) { } // Two-call pattern for more reliable CPU usage. - cpu.Percent(0, true) + warmUpCpu() time.Sleep(cpuSampleInterval) percents, err := cpu.Percent(0, true) var totalPercent float64 @@ -255,3 +255,7 @@ func fallbackCPUUtilization(logical int) (float64, []float64, error) { } return avg, perCore, nil } + +func warmUpCpu() { + cpu.Percent(0, true) //nolint:errcheck +} diff --git a/cmd/status/metrics_hardware.go b/cmd/status/metrics_hardware.go index 8117e57..54ff7ba 100644 --- a/cmd/status/metrics_hardware.go +++ b/cmd/status/metrics_hardware.go @@ -130,6 +130,8 @@ func parseInt(s string) int { return 0 } var num int - fmt.Sscanf(cleaned, "%d", &num) + if _, err := fmt.Sscanf(cleaned, "%d", &num); err != nil { + return 0 + } return num } diff --git a/cmd/status/metrics_health.go b/cmd/status/metrics_health.go index 3a556f2..4b92693 100644 --- a/cmd/status/metrics_health.go +++ b/cmd/status/metrics_health.go @@ -70,10 +70,12 @@ func calculateHealthScore(cpu CPUStatus, mem MemoryStatus, disks []DiskStatus, d } // Memory pressure penalty. - if mem.Pressure == "warn" { + // Memory pressure penalty. + switch mem.Pressure { + case "warn": score -= memPressureWarnPenalty issues = append(issues, "Memory Pressure") - } else if mem.Pressure == "critical" { + case "critical": score -= memPressureCritPenalty issues = append(issues, "Critical Memory") } @@ -131,7 +133,7 @@ func calculateHealthScore(cpu CPUStatus, mem MemoryStatus, disks []DiskStatus, d } // Build message. - msg := "Excellent" + var msg string if score >= 90 { msg = "Excellent" } else if score >= 75 { diff --git a/cmd/status/view.go b/cmd/status/view.go index 2691452..0a3f540 100644 --- a/cmd/status/view.go +++ b/cmd/status/view.go @@ -308,9 +308,10 @@ func renderMemoryCard(mem MemoryStatus) cardData { if mem.Pressure != "" { pressureStyle := okStyle pressureText := "Status " + mem.Pressure - if mem.Pressure == "warn" { + switch mem.Pressure { + case "warn": pressureStyle = warnStyle - } else if mem.Pressure == "critical" { + case "critical": pressureStyle = dangerStyle } lines = append(lines, pressureStyle.Render(pressureText))