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,3 +1,4 @@
|
||||
// Package store provides a SQLite-backed cache for parsed session data.
|
||||
package store
|
||||
|
||||
import (
|
||||
@@ -9,7 +10,7 @@ import (
|
||||
|
||||
"cburn/internal/model"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
_ "modernc.org/sqlite" // register sqlite driver
|
||||
)
|
||||
|
||||
// Cache provides SQLite-backed session caching.
|
||||
@@ -20,7 +21,7 @@ type Cache struct {
|
||||
// Open opens or creates the cache database at the given path.
|
||||
func Open(dbPath string) (*Cache, error) {
|
||||
dir := filepath.Dir(dbPath)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
||||
return nil, fmt.Errorf("creating cache dir: %w", err)
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ func Open(dbPath string) (*Cache, error) {
|
||||
}
|
||||
|
||||
if _, err := db.Exec(schemaSQL); err != nil {
|
||||
db.Close()
|
||||
_ = db.Close()
|
||||
return nil, fmt.Errorf("creating schema: %w", err)
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ func (c *Cache) GetTrackedFiles() (map[string]FileInfo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
result := make(map[string]FileInfo)
|
||||
for rows.Next() {
|
||||
@@ -74,7 +75,7 @@ func (c *Cache) SaveSession(s model.SessionStats, mtimeNs, sizeBytes int64) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
startTime := ""
|
||||
@@ -147,7 +148,7 @@ func (c *Cache) LoadAllSessions() ([]model.SessionStats, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var sessions []model.SessionStats
|
||||
for rows.Next() {
|
||||
@@ -195,7 +196,7 @@ func (c *Cache) LoadAllSessions() ([]model.SessionStats, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer modelRows.Close()
|
||||
defer func() { _ = modelRows.Close() }()
|
||||
|
||||
// Build session index for fast lookup
|
||||
sessionIdx := make(map[string]int)
|
||||
|
||||
Reference in New Issue
Block a user