Fail fast if count value is invalid when count-prompt is not set.

move lottery draw logic into runLottery()

move MarkFlagsMutuallyExclusive into init()
This commit is contained in:
2026-08-03 04:46:00 +01:00
parent 98c94b4947
commit 1282da4e0e
2 changed files with 101 additions and 42 deletions

41
cmd/lottery/validate.go Normal file
View File

@@ -0,0 +1,41 @@
package main
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/viper"
)
// validateNonPromptCount checks if the count is valid when the count-prompt flag is not set.
func validateNonPromptCount() error {
if viper.GetBool("count-prompt") {
return nil
}
return validateCount(viper.GetInt("count"))
}
// validateCount checks if the count is valid.
func validateCount(count int) error {
if count < 1 {
return fmt.Errorf("count must be greater than 0")
}
return nil
}
// parseCount parses the count from a string and validates it.
func parseCount(raw string) (int, error) {
count, err := strconv.Atoi(strings.TrimSpace(raw))
if err != nil {
return 0, fmt.Errorf("count must be a whole number")
}
if err := validateCount(count); err != nil {
return 0, err
}
return count, nil
}