mirror of
https://gitea.com/jolheiser/sip
synced 2024-11-26 07:33:48 +01:00
67326a7948
Signed-off-by: jolheiser <john.olheiser@gmail.com>
156 lines
2.7 KiB
Go
156 lines
2.7 KiB
Go
package prompt
|
|
|
|
import (
|
|
"github.com/AlecAivazis/survey/v2"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func noValidate(_ interface{}) error {
|
|
return nil
|
|
}
|
|
|
|
func Input(ctx *cli.Context, flag *cli.StringFlag, def string, required bool) (string, error) {
|
|
for _, c := range ctx.Lineage() {
|
|
if c.IsSet(flag.Name) {
|
|
return c.String(flag.Name), nil
|
|
}
|
|
}
|
|
|
|
validator := noValidate
|
|
if required {
|
|
validator = survey.Required
|
|
}
|
|
|
|
q := []*survey.Question{
|
|
{
|
|
Name: "q",
|
|
Prompt: &survey.Input{Message: flag.Usage, Default: def},
|
|
Validate: validator,
|
|
},
|
|
}
|
|
a := struct {
|
|
Q string
|
|
}{}
|
|
|
|
if err := survey.Ask(q, &a); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return a.Q, nil
|
|
}
|
|
|
|
func Multi(ctx *cli.Context, flag *cli.StringFlag, def string, required bool) (string, error) {
|
|
for _, c := range ctx.Lineage() {
|
|
if c.IsSet(flag.Name) {
|
|
return c.String(flag.Name), nil
|
|
}
|
|
}
|
|
|
|
validator := noValidate
|
|
if required {
|
|
validator = survey.Required
|
|
}
|
|
|
|
q := []*survey.Question{
|
|
{
|
|
Name: "q",
|
|
Prompt: &survey.Multiline{Message: flag.Usage, Default: def},
|
|
Validate: validator,
|
|
},
|
|
}
|
|
a := struct {
|
|
Q string
|
|
}{}
|
|
|
|
if err := survey.Ask(q, &a); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return a.Q, nil
|
|
}
|
|
|
|
func Select(ctx *cli.Context, flag *cli.StringFlag, options []string, def string, required bool) (string, error) {
|
|
for _, c := range ctx.Lineage() {
|
|
if c.IsSet(flag.Name) {
|
|
return c.String(flag.Name), nil
|
|
}
|
|
}
|
|
|
|
validator := noValidate
|
|
if required {
|
|
validator = survey.Required
|
|
}
|
|
|
|
q := []*survey.Question{
|
|
{
|
|
Name: "q",
|
|
Prompt: &survey.Select{Message: flag.Usage, Options: options, Default: def},
|
|
Validate: validator,
|
|
},
|
|
}
|
|
a := struct {
|
|
Q string
|
|
}{}
|
|
|
|
if err := survey.Ask(q, &a); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return a.Q, nil
|
|
}
|
|
|
|
func Confirm(ctx *cli.Context, flag *cli.BoolFlag, def bool, required bool) (bool, error) {
|
|
for _, c := range ctx.Lineage() {
|
|
if c.IsSet(flag.Name) {
|
|
return c.Bool(flag.Name), nil
|
|
}
|
|
}
|
|
|
|
validator := noValidate
|
|
if required {
|
|
validator = survey.Required
|
|
}
|
|
|
|
q := []*survey.Question{
|
|
{
|
|
Name: "q",
|
|
Prompt: &survey.Confirm{Message: flag.Usage, Default: def},
|
|
Validate: validator,
|
|
},
|
|
}
|
|
a := struct {
|
|
Q bool
|
|
}{}
|
|
|
|
if err := survey.Ask(q, &a); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return a.Q, nil
|
|
}
|
|
|
|
func Custom(ctx *cli.Context, flag string, prompt survey.Prompt, validator func(val interface{}) error) (interface{}, error) {
|
|
for _, c := range ctx.Lineage() {
|
|
if c.IsSet(flag) {
|
|
return c.Generic(flag), nil
|
|
}
|
|
}
|
|
|
|
q := []*survey.Question{
|
|
{
|
|
Name: "q",
|
|
Prompt: prompt,
|
|
Validate: validator,
|
|
},
|
|
}
|
|
a := struct {
|
|
Q interface{}
|
|
}{}
|
|
|
|
if err := survey.Ask(q, &a); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return a.Q, nil
|
|
}
|