mirror of
https://github.com/onyx-and-iris/lottery-cli.git
synced 2026-08-17 00:34:25 +00:00
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:
41
cmd/lottery/validate.go
Normal file
41
cmd/lottery/validate.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user