mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-19 11:39:29 +02:00
Support integer config values
This commit is contained in:
parent
8c459f4d2f
commit
97e93553a1
3 changed files with 37 additions and 0 deletions
|
@ -17,6 +17,7 @@ package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/manifoldco/promptui"
|
"github.com/manifoldco/promptui"
|
||||||
"go.uploadedlobster.com/scotty/internal/i18n"
|
"go.uploadedlobster.com/scotty/internal/i18n"
|
||||||
|
@ -31,6 +32,8 @@ func Prompt(opt models.BackendOption) (any, error) {
|
||||||
return PromptSecret(opt)
|
return PromptSecret(opt)
|
||||||
case models.String:
|
case models.String:
|
||||||
return PromptString(opt)
|
return PromptString(opt)
|
||||||
|
case models.Int:
|
||||||
|
return PromptInt(opt)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown prompt type %v", opt.Type)
|
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()
|
_, val, err := sel.Run()
|
||||||
return val == yes, err
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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 {
|
func (c *ServiceConfig) IsSet(key string) bool {
|
||||||
_, ok := c.ConfigValues[key]
|
_, ok := c.ConfigValues[key]
|
||||||
return ok
|
return ok
|
||||||
|
|
|
@ -21,6 +21,7 @@ const (
|
||||||
Bool OptionType = "bool"
|
Bool OptionType = "bool"
|
||||||
Secret OptionType = "secret"
|
Secret OptionType = "secret"
|
||||||
String OptionType = "string"
|
String OptionType = "string"
|
||||||
|
Int OptionType = "int"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BackendOption struct {
|
type BackendOption struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue