2022-09-05 18:25:54 +00:00
|
|
|
package prompt
|
2022-05-14 17:55:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-05 18:25:54 +00:00
|
|
|
"io"
|
2022-05-14 17:55:55 +00:00
|
|
|
|
|
|
|
"github.com/databricks/bricks/project"
|
|
|
|
"github.com/manifoldco/promptui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Results map[string]Answer
|
|
|
|
|
|
|
|
type Question interface {
|
|
|
|
Ask(res Results) (key string, ans Answer, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Questions []Question
|
|
|
|
|
|
|
|
func (qq Questions) Ask(res Results) error {
|
|
|
|
for _, v := range qq {
|
|
|
|
key, ans, err := v.Ask(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res[key] = ans
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Text struct {
|
2022-09-05 18:25:54 +00:00
|
|
|
Key string
|
2022-05-14 17:55:55 +00:00
|
|
|
Label string
|
|
|
|
Default func(res Results) string
|
|
|
|
Callback AnswerCallback
|
2022-09-05 18:25:54 +00:00
|
|
|
Stdin io.ReadCloser
|
2022-05-14 17:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t Text) Ask(res Results) (string, Answer, error) {
|
|
|
|
def := ""
|
|
|
|
if t.Default != nil {
|
|
|
|
def = t.Default(res)
|
|
|
|
}
|
|
|
|
v, err := (&promptui.Prompt{
|
|
|
|
Label: t.Label,
|
|
|
|
Default: def,
|
2022-09-05 18:25:54 +00:00
|
|
|
Stdin: t.Stdin,
|
2022-05-14 17:55:55 +00:00
|
|
|
}).Run()
|
2022-09-05 18:25:54 +00:00
|
|
|
return t.Key, Answer{
|
2022-05-14 17:55:55 +00:00
|
|
|
Value: v,
|
|
|
|
Callback: t.Callback,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type Choice struct {
|
2022-09-05 18:25:54 +00:00
|
|
|
Key string
|
2022-05-14 17:55:55 +00:00
|
|
|
Label string
|
|
|
|
Answers []Answer
|
2022-09-05 18:25:54 +00:00
|
|
|
Stdin io.ReadCloser
|
2022-05-14 17:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q Choice) Ask(res Results) (string, Answer, error) {
|
2022-05-16 10:50:50 +00:00
|
|
|
// TODO: validate and re-ask
|
2022-05-14 17:55:55 +00:00
|
|
|
prompt := promptui.Select{
|
|
|
|
Label: q.Label,
|
|
|
|
Items: q.Answers,
|
|
|
|
Templates: &promptui.SelectTemplates{
|
|
|
|
Label: `{{ .Value }}`,
|
|
|
|
Details: `{{ .Details | green }}`,
|
|
|
|
Selected: fmt.Sprintf(`{{ "%s" | faint }}: {{ .Value | bold }}`, q.Label),
|
|
|
|
},
|
2022-09-05 18:25:54 +00:00
|
|
|
Stdin: q.Stdin,
|
2022-05-14 17:55:55 +00:00
|
|
|
}
|
|
|
|
i, _, err := prompt.Run()
|
2022-09-05 18:25:54 +00:00
|
|
|
return q.Key, q.Answers[i], err
|
2022-05-14 17:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Answers []Answer
|
|
|
|
|
|
|
|
type AnswerCallback func(ans Answer, prj *project.Project, res Results)
|
|
|
|
|
|
|
|
type Answer struct {
|
|
|
|
Value string
|
|
|
|
Details string
|
|
|
|
Callback AnswerCallback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Answer) String() string {
|
|
|
|
return a.Value
|
|
|
|
}
|