mirror of
https://github.com/onyx-and-iris/lottery-cli.git
synced 2026-08-17 00:34:25 +00:00
mark --count and --count-prompt as ME.
use AllKinds to build the selection prompt. remove --kind flag validation from PreRunE (its redundant).
This commit is contained in:
@@ -29,17 +29,12 @@ func versionFromBuild() string {
|
|||||||
return strings.Split(info.Main.Version, "-")[0]
|
return strings.Split(info.Main.Version, "-")[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "lottery",
|
Use: "lottery",
|
||||||
Short: "A CLI for National Lottery games.",
|
Short: "A CLI for National Lottery games.",
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
kindStr := viper.GetString("kind")
|
cmd.MarkFlagsMutuallyExclusive("count", "count-prompt")
|
||||||
if kindStr != "" {
|
|
||||||
_, err := lottery.ParseKind(kindStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@@ -49,13 +44,7 @@ var cmd = &cobra.Command{
|
|||||||
huh.NewGroup(
|
huh.NewGroup(
|
||||||
huh.NewSelect[string]().
|
huh.NewSelect[string]().
|
||||||
Title("Pick a lottery.").
|
Title("Pick a lottery.").
|
||||||
Options(
|
Options(kindPromptOptions()...).
|
||||||
huh.NewOption("Lotto", "lotto"),
|
|
||||||
huh.NewOption("EuroMillions", "euromillions"),
|
|
||||||
huh.NewOption("Set For Life", "setforlife"),
|
|
||||||
huh.NewOption("Thunderball", "thunderball"),
|
|
||||||
huh.NewOption("Powerball", "powerball"),
|
|
||||||
).
|
|
||||||
Value(&kindStr),
|
Value(&kindStr),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -77,17 +66,12 @@ var cmd = &cobra.Command{
|
|||||||
|
|
||||||
if countPrompt := viper.GetBool("count-prompt"); countPrompt {
|
if countPrompt := viper.GetBool("count-prompt"); countPrompt {
|
||||||
var count string
|
var count string
|
||||||
countPrompt := huh.NewForm(
|
if err := huh.NewInput().
|
||||||
huh.NewGroup(
|
Title("How many draws would you like to generate?").
|
||||||
huh.NewInput().
|
Value(&count).Run(); err != nil {
|
||||||
Title("How many draws would you like to generate?").
|
|
||||||
Value(&count),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
err := countPrompt.Run()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.Set("count", count)
|
viper.Set("count", count)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,15 +95,41 @@ var cmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func kindPromptLabel(kind lottery.Kind) string {
|
||||||
|
switch kind {
|
||||||
|
case lottery.KindLotto:
|
||||||
|
return "Lotto"
|
||||||
|
case lottery.KindEuroMillions:
|
||||||
|
return "EuroMillions"
|
||||||
|
case lottery.KindSetForLife:
|
||||||
|
return "Set For Life"
|
||||||
|
case lottery.KindThunderball:
|
||||||
|
return "Thunderball"
|
||||||
|
case lottery.KindPowerball:
|
||||||
|
return "Powerball"
|
||||||
|
default:
|
||||||
|
return string(kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func kindPromptOptions() []huh.Option[string] {
|
||||||
|
kinds := lottery.AllKinds()
|
||||||
|
options := make([]huh.Option[string], 0, len(kinds))
|
||||||
|
for _, kind := range kinds {
|
||||||
|
options = append(options, huh.NewOption(kindPromptLabel(kind), string(kind)))
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd.Flags().StringP("kind", "k", "", "Lottery kind to generate draws for.")
|
rootCmd.Flags().StringP("kind", "k", "", "Lottery kind to generate draws for.")
|
||||||
cmd.Flags().IntP("count", "c", 1, "Number of draws to generate.")
|
rootCmd.Flags().IntP("count", "c", 1, "Number of draws to generate.")
|
||||||
cmd.Flags().BoolP("count-prompt", "C", false, "Prompt for the number of draws to generate.")
|
rootCmd.Flags().BoolP("count-prompt", "C", false, "Prompt for the number of draws to generate.")
|
||||||
|
|
||||||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||||
viper.SetEnvPrefix("LOTTERY")
|
viper.SetEnvPrefix("LOTTERY")
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
if err := viper.BindPFlags(cmd.Flags()); err != nil {
|
if err := viper.BindPFlags(rootCmd.Flags()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,7 +137,7 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
if err := fang.Execute(
|
if err := fang.Execute(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
cmd,
|
rootCmd,
|
||||||
fang.WithVersion(versionFromBuild()),
|
fang.WithVersion(versionFromBuild()),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user