1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 15:04:42 +00:00

Go code formatting optimization

This commit is contained in:
Tw93
2026-01-08 15:26:35 +08:00
parent 3e4674f1e6
commit 79a19ffbfe
7 changed files with 25 additions and 28 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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".
}
}()

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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))