fix: linter compliance and code quality improvements across codebase
Address golangci-lint findings and improve error handling throughout:
Package doc comments:
- Add canonical "// Package X ..." comments to source, model, config,
pipeline, cli, store, and main packages for godoc compliance.
Security & correctness:
- Fix directory permissions 0o755 -> 0o750 in store/cache.go Open()
(gosec G301: restrict group write on cache directory)
- Fix config.Save() to check encoder error before closing file, preventing
silent data loss on encode failure
- Add //nolint:gosec annotations with justifications on intentional
patterns (constructed file paths, manual bounds checking, config fields)
- Add //nolint:nilerr on intentional error-swallowing in scanner WalkDir
- Add //nolint:revive on stuttering type names (ModelStats, ModelUsage)
that would break too many call sites to rename
Performance (perfsprint):
- Replace fmt.Sprintf("%d", n) with strconv.FormatInt(n, 10) in format.go
FormatTokens() and FormatNumber() hot paths
- Clean up redundant fmt.Sprintf patterns in FormatCost and FormatDelta
Code cleanup:
- Convert if-else chain to switch in parser.go skipJSONString() for clarity
- Remove unused indexedResult struct from pipeline/loader.go
- Add deferred cache.Close() in pipeline/bench_test.go to prevent leaks
- Add deferred cache.Close() in cmd/root.go data loading path
- Fix doc comment alignment in scanner.go decodeProjectName
- Remove trailing blank line in cmd/costs.go
- Fix duplicate "/day" suffix in cmd/summary.go cost-per-day formatting
- Rename shadowed variable 'max' -> 'maxVal' in cli/render.go Sparkline
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
// Package cli provides formatting and rendering utilities for terminal output.
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -22,14 +24,14 @@ func FormatTokens(n int64) string {
|
||||
case abs >= 1_000:
|
||||
return fmt.Sprintf("%.1fK", float64(n)/1_000)
|
||||
default:
|
||||
return fmt.Sprintf("%d", n)
|
||||
return strconv.FormatInt(n, 10)
|
||||
}
|
||||
}
|
||||
|
||||
// FormatCost formats a USD cost value.
|
||||
func FormatCost(cost float64) string {
|
||||
if cost >= 1000 {
|
||||
return fmt.Sprintf("$%s", FormatNumber(int64(math.Round(cost))))
|
||||
return "$" + FormatNumber(int64(math.Round(cost)))
|
||||
}
|
||||
if cost >= 100 {
|
||||
return fmt.Sprintf("$%.0f", cost)
|
||||
@@ -66,7 +68,7 @@ func FormatNumber(n int64) string {
|
||||
return "-" + FormatNumber(-n)
|
||||
}
|
||||
|
||||
s := fmt.Sprintf("%d", n)
|
||||
s := strconv.FormatInt(n, 10)
|
||||
if len(s) <= 3 {
|
||||
return s
|
||||
}
|
||||
@@ -95,9 +97,9 @@ func FormatPercent(f float64) string {
|
||||
func FormatDelta(current, previous float64) string {
|
||||
delta := current - previous
|
||||
if delta >= 0 {
|
||||
return fmt.Sprintf("+%s", FormatCost(delta))
|
||||
return "+" + FormatCost(delta)
|
||||
}
|
||||
return fmt.Sprintf("-%s", FormatCost(-delta))
|
||||
return "-" + FormatCost(-delta)
|
||||
}
|
||||
|
||||
// FormatDayOfWeek returns a 3-letter day abbreviation from a weekday number.
|
||||
|
||||
Reference in New Issue
Block a user