Support integer config values

This commit is contained in:
Philipp Wolfer 2024-01-13 14:11:58 +01:00
parent 8c459f4d2f
commit 97e93553a1
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
3 changed files with 37 additions and 0 deletions

View file

@ -17,6 +17,7 @@ package cli
import (
"fmt"
"strconv"
"github.com/manifoldco/promptui"
"go.uploadedlobster.com/scotty/internal/i18n"
@ -31,6 +32,8 @@ func Prompt(opt models.BackendOption) (any, error) {
return PromptSecret(opt)
case models.String:
return PromptString(opt)
case models.Int:
return PromptInt(opt)
default:
return nil, fmt.Errorf("unknown prompt type %v", opt.Type)
}
@ -78,3 +81,28 @@ func PromptYesNo(label string, defaultValue bool) (bool, error) {
_, val, err := sel.Run()
return val == yes, err
}
func PromptInt(opt models.BackendOption) (int, error) {
validate := func(s string) error {
if opt.Validate != nil {
if err := opt.Validate(s); err != nil {
return err
}
}
if _, err := strconv.Atoi(s); err != nil {
return err
}
return nil
}
prompt := promptui.Prompt{
Label: opt.Label,
Validate: validate,
Default: opt.Default,
}
val, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.Atoi(val)
}

View file

@ -62,6 +62,14 @@ func (c *ServiceConfig) GetBool(key string, defaultValue bool) bool {
}
}
func (c *ServiceConfig) GetInt(key string, defaultValue int) int {
if c.IsSet(key) {
return cast.ToInt(c.ConfigValues[key])
} else {
return defaultValue
}
}
func (c *ServiceConfig) IsSet(key string) bool {
_, ok := c.ConfigValues[key]
return ok

View file

@ -21,6 +21,7 @@ const (
Bool OptionType = "bool"
Secret OptionType = "secret"
String OptionType = "string"
Int OptionType = "int"
)
type BackendOption struct {