Skip to content

Commit 599fe9e

Browse files
authored
Fix all golangci-lint issues across the codebase (#32)
* Fix all golangci-lint issues across the codebase Resolve 77 lint issues reported by golangci-lint with gocritic, gocognit, gocyclo, maintidx, dupl, mnd, unparam, ireturn, goconst, and errcheck enabled. Net reduction of ~175 lines through shared helpers and deduplication. * Suppress staticcheck SA1019 for intentional deprecated field usage The Storage.Path field is deprecated but still read for backwards compatibility with existing configs that haven't migrated to the URL field.
1 parent 1308242 commit 599fe9e

41 files changed

Lines changed: 896 additions & 1071 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/proxy/main.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ import (
103103
"github.com/git-pkgs/proxy/internal/server"
104104
)
105105

106+
const defaultTopN = 10
107+
106108
var (
107109
// Version is set at build time.
108110
Version = "dev"
@@ -211,7 +213,7 @@ func runServe() {
211213
cfg.Storage.URL = *storageURL
212214
}
213215
if *storagePath != "" {
214-
cfg.Storage.Path = *storagePath
216+
cfg.Storage.Path = *storagePath //nolint:staticcheck // backwards compat
215217
}
216218
if *databaseDriver != "" {
217219
cfg.Database.Driver = *databaseDriver
@@ -247,7 +249,6 @@ func runServe() {
247249

248250
// Handle shutdown signals
249251
ctx, cancel := context.WithCancel(context.Background())
250-
defer cancel()
251252

252253
go func() {
253254
sigCh := make(chan os.Signal, 1)
@@ -266,10 +267,12 @@ func runServe() {
266267
// Wait for shutdown or error
267268
select {
268269
case <-ctx.Done():
270+
cancel()
269271
if err := srv.Shutdown(context.Background()); err != nil {
270272
logger.Error("shutdown error", "error", err)
271273
}
272274
case err := <-errCh:
275+
cancel()
273276
if err != nil {
274277
logger.Error("server error", "error", err)
275278
os.Exit(1)
@@ -283,8 +286,8 @@ func runStats() {
283286
databasePath := fs.String("database-path", "./cache/proxy.db", "Path to SQLite database file")
284287
databaseURL := fs.String("database-url", "", "PostgreSQL connection URL")
285288
asJSON := fs.Bool("json", false, "Output as JSON")
286-
popular := fs.Int("popular", 10, "Show top N most popular packages")
287-
recent := fs.Int("recent", 10, "Show N recently cached packages")
289+
popular := fs.Int("popular", defaultTopN, "Show top N most popular packages")
290+
recent := fs.Int("recent", defaultTopN, "Show N recently cached packages")
288291

289292
fs.Usage = func() {
290293
fmt.Fprintf(os.Stderr, "git-pkgs proxy - Show cache statistics\n\n")
@@ -330,32 +333,37 @@ func runStats() {
330333
fmt.Fprintf(os.Stderr, "error opening database: %v\n", err)
331334
os.Exit(1)
332335
}
336+
337+
if err := printStats(db, *popular, *recent, *asJSON); err != nil {
338+
fmt.Fprintf(os.Stderr, "%v\n", err)
339+
os.Exit(1)
340+
}
341+
}
342+
343+
func printStats(db *database.DB, popular, recent int, asJSON bool) error {
333344
defer func() { _ = db.Close() }()
334345

335-
// Get stats
336346
stats, err := db.GetCacheStats()
337347
if err != nil {
338-
fmt.Fprintf(os.Stderr, "error getting stats: %v\n", err)
339-
os.Exit(1)
348+
return fmt.Errorf("error getting stats: %w", err)
340349
}
341350

342-
popularPkgs, err := db.GetMostPopularPackages(*popular)
351+
popularPkgs, err := db.GetMostPopularPackages(popular)
343352
if err != nil {
344-
fmt.Fprintf(os.Stderr, "error getting popular packages: %v\n", err)
345-
os.Exit(1)
353+
return fmt.Errorf("error getting popular packages: %w", err)
346354
}
347355

348-
recentPkgs, err := db.GetRecentlyCachedPackages(*recent)
356+
recentPkgs, err := db.GetRecentlyCachedPackages(recent)
349357
if err != nil {
350-
fmt.Fprintf(os.Stderr, "error getting recent packages: %v\n", err)
351-
os.Exit(1)
358+
return fmt.Errorf("error getting recent packages: %w", err)
352359
}
353360

354-
if *asJSON {
361+
if asJSON {
355362
outputJSON(stats, popularPkgs, recentPkgs)
356363
} else {
357364
outputText(stats, popularPkgs, recentPkgs)
358365
}
366+
return nil
359367
}
360368

361369
type jsonOutput struct {

internal/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ type StorageConfig struct {
109109
URL string `json:"url" yaml:"url"`
110110

111111
// Path is the directory where cached artifacts are stored.
112-
// Deprecated: Use URL with file:// scheme instead.
113112
// If URL is empty, this is used as file://{Path}.
113+
//
114+
// Deprecated: Use URL with file:// scheme instead.
114115
Path string `json:"path" yaml:"path"`
115116

116117
// MaxSize is the maximum cache size (e.g., "10GB", "500MB").

internal/config/config_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import (
66
"testing"
77
)
88

9+
const (
10+
testDriverPostgres = "postgres"
11+
testInvalid = "invalid"
12+
testLevelDebug = "debug"
13+
)
14+
915
func TestDefault(t *testing.T) {
1016
cfg := Default()
1117

@@ -63,27 +69,27 @@ func TestValidate(t *testing.T) {
6369
},
6470
{
6571
name: "postgres without url",
66-
modify: func(c *Config) { c.Database.Driver = "postgres"; c.Database.URL = "" },
72+
modify: func(c *Config) { c.Database.Driver = testDriverPostgres; c.Database.URL = "" },
6773
wantErr: true,
6874
},
6975
{
7076
name: "postgres with url",
71-
modify: func(c *Config) { c.Database.Driver = "postgres"; c.Database.URL = "postgres://localhost/test" },
77+
modify: func(c *Config) { c.Database.Driver = testDriverPostgres; c.Database.URL = "postgres://localhost/test" },
7278
wantErr: false,
7379
},
7480
{
7581
name: "invalid log level",
76-
modify: func(c *Config) { c.Log.Level = "invalid" },
82+
modify: func(c *Config) { c.Log.Level = testInvalid },
7783
wantErr: true,
7884
},
7985
{
8086
name: "invalid log format",
81-
modify: func(c *Config) { c.Log.Format = "invalid" },
87+
modify: func(c *Config) { c.Log.Format = testInvalid },
8288
wantErr: true,
8389
},
8490
{
8591
name: "invalid max size",
86-
modify: func(c *Config) { c.Storage.MaxSize = "invalid" },
92+
modify: func(c *Config) { c.Storage.MaxSize = testInvalid },
8793
wantErr: true,
8894
},
8995
{
@@ -176,8 +182,8 @@ log:
176182
if cfg.Storage.MaxSize != "5GB" {
177183
t.Errorf("Storage.MaxSize = %q, want %q", cfg.Storage.MaxSize, "5GB")
178184
}
179-
if cfg.Log.Level != "debug" {
180-
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, "debug")
185+
if cfg.Log.Level != testLevelDebug {
186+
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, testLevelDebug)
181187
}
182188
if cfg.Log.Format != "json" {
183189
t.Errorf("Log.Format = %q, want %q", cfg.Log.Format, "json")
@@ -215,7 +221,7 @@ func TestLoadFromEnv(t *testing.T) {
215221
t.Setenv("PROXY_LISTEN", ":9000")
216222
t.Setenv("PROXY_BASE_URL", "https://env.example.com")
217223
t.Setenv("PROXY_STORAGE_PATH", "/env/cache")
218-
t.Setenv("PROXY_LOG_LEVEL", "debug")
224+
t.Setenv("PROXY_LOG_LEVEL", testLevelDebug)
219225

220226
cfg.LoadFromEnv()
221227

@@ -228,8 +234,8 @@ func TestLoadFromEnv(t *testing.T) {
228234
if cfg.Storage.Path != "/env/cache" {
229235
t.Errorf("Storage.Path = %q, want %q", cfg.Storage.Path, "/env/cache")
230236
}
231-
if cfg.Log.Level != "debug" {
232-
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, "debug")
237+
if cfg.Log.Level != testLevelDebug {
238+
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, testLevelDebug)
233239
}
234240
}
235241

internal/cooldown/cooldown.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"time"
88
)
99

10+
const hoursPerDay = 24
11+
1012
// Config holds cooldown settings for version filtering.
1113
// Cooldown hides package versions published too recently, giving the community
1214
// time to spot malicious releases before they're pulled into projects.
@@ -112,7 +114,7 @@ func ParseDuration(s string) (time.Duration, error) {
112114
if err != nil {
113115
return 0, fmt.Errorf("invalid duration %q: %w", s, err)
114116
}
115-
return time.Duration(days * float64(24*time.Hour)), nil
117+
return time.Duration(days * float64(hoursPerDay*time.Hour)), nil
116118
}
117119

118120
d, err := time.ParseDuration(s)

internal/database/database.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
const SchemaVersion = 1
1414

15+
const dirPermissions = 0755
16+
1517
type Dialect string
1618

1719
const (
@@ -56,7 +58,7 @@ func Create(path string) (*DB, error) {
5658

5759
func Open(path string) (*DB, error) {
5860
if dir := filepath.Dir(path); dir != "." && dir != "/" {
59-
if err := os.MkdirAll(dir, 0755); err != nil {
61+
if err := os.MkdirAll(dir, dirPermissions); err != nil {
6062
return nil, fmt.Errorf("creating database directory: %w", err)
6163
}
6264
}

0 commit comments

Comments
 (0)