From c7f30070c068151b43f65a0cab1217e83fd75bc1 Mon Sep 17 00:00:00 2001 From: Kharec Date: Fri, 21 Nov 2025 14:55:51 +0100 Subject: [PATCH] feat: reject negative/nonsensical flag values with a clear error instead of letting slice/channel allocations panic --- cmd/goyco/commands/seed.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/goyco/commands/seed.go b/cmd/goyco/commands/seed.go index 8e76348..3f4fd3c 100644 --- a/cmd/goyco/commands/seed.go +++ b/cmd/goyco/commands/seed.go @@ -69,6 +69,16 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po return err } + if *numUsers < 0 { + return fmt.Errorf("invalid value for --users: %d (must be >= 0)", *numUsers) + } + if *numPosts <= 0 { + return fmt.Errorf("invalid value for --posts: %d (must be > 0)", *numPosts) + } + if *votesPerPost < 0 { + return fmt.Errorf("invalid value for --votes-per-post: %d (must be >= 0)", *votesPerPost) + } + if !IsJSONOutput() { fmt.Println("Starting database seeding...") } @@ -93,7 +103,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po processor := NewParallelProcessor() var progress *ProgressIndicator - if !IsJSONOutput() { + if !IsJSONOutput() && *numUsers > 0 { progress = NewProgressIndicator(*numUsers, "Creating users (parallel)") } users, err := processor.CreateUsersInParallel(userRepo, *numUsers, progress) @@ -106,7 +116,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po allUsers := append([]database.User{*seedUser}, users...) - if !IsJSONOutput() { + if !IsJSONOutput() && *numPosts > 0 { progress = NewProgressIndicator(*numPosts, "Creating posts (parallel)") } posts, err := processor.CreatePostsInParallel(postRepo, seedUser.ID, *numPosts, progress) @@ -117,7 +127,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po progress.Complete() } - if !IsJSONOutput() { + if !IsJSONOutput() && len(posts) > 0 { progress = NewProgressIndicator(len(posts), "Creating votes (parallel)") } votes, err := processor.CreateVotesInParallel(voteRepo, allUsers, posts, *votesPerPost, progress) @@ -128,7 +138,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po progress.Complete() } - if !IsJSONOutput() { + if !IsJSONOutput() && len(posts) > 0 { progress = NewProgressIndicator(len(posts), "Updating scores (parallel)") } err = processor.UpdatePostScoresInParallel(postRepo, voteRepo, posts, progress)