2020-02-24 19:14:46 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-09-07 16:33:58 +03:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2022-10-28 12:31:03 +03:00
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2020-02-24 19:14:46 +03:00
|
|
|
)
|
|
|
|
|
2021-09-07 16:33:58 +03:00
|
|
|
func GetYesNoPrompt(message string) (promptResp bool, err error) {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.GetYesNoPrompt"
|
2021-09-07 16:33:58 +03:00
|
|
|
prompt := &survey.Confirm{
|
|
|
|
Message: message,
|
|
|
|
Default: true,
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-09-07 16:33:58 +03:00
|
|
|
err = survey.AskOne(prompt, &promptResp)
|
2022-10-28 12:31:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return promptResp, errors.E(op, err)
|
|
|
|
}
|
|
|
|
return promptResp, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSelectPrompt(message string, options []string) (selection string, err error) {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.GetSelectPrompt"
|
2021-09-07 16:33:58 +03:00
|
|
|
prompt := &survey.Select{
|
|
|
|
Message: message,
|
|
|
|
Options: options,
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-09-07 16:33:58 +03:00
|
|
|
err = survey.AskOne(prompt, &selection)
|
2022-10-28 12:31:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return selection, errors.E(op, err)
|
|
|
|
}
|
|
|
|
return selection, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
func GetInputPrompt(message string) (input string, err error) {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.GetInputPrompt"
|
2021-09-07 16:33:58 +03:00
|
|
|
prompt := &survey.Input{
|
|
|
|
Message: message,
|
2021-01-18 20:11:05 +03:00
|
|
|
}
|
2021-09-07 16:33:58 +03:00
|
|
|
err = survey.AskOne(prompt, &input)
|
2022-10-28 12:31:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return input, errors.E(op, err)
|
|
|
|
}
|
|
|
|
return input, nil
|
2021-09-07 16:33:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetInputPromptWithDefault(message string, def string) (input string, err error) {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.GetInputPromptWithDefault"
|
2021-09-07 16:33:58 +03:00
|
|
|
prompt := &survey.Input{
|
|
|
|
Message: message,
|
|
|
|
Default: def,
|
|
|
|
}
|
|
|
|
err = survey.AskOne(prompt, &input)
|
2022-10-28 12:31:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return input, errors.E(op, err)
|
|
|
|
}
|
|
|
|
return input, nil
|
2021-09-07 16:33:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateDirPath(a interface{}) error {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.validateDirPath"
|
2021-09-07 16:33:58 +03:00
|
|
|
err := FSCheckIfDirPathExists(a.(string))
|
2022-10-28 12:31:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.E(op, err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-18 20:11:05 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
func GetFSPathPrompt(message string, def string) (input string, err error) {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.GetFSPathPrompt"
|
2021-09-07 16:33:58 +03:00
|
|
|
prompt := &survey.Input{
|
|
|
|
Message: message,
|
|
|
|
Default: def,
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-09-07 16:33:58 +03:00
|
|
|
err = survey.AskOne(prompt, &input, survey.WithValidator(validateDirPath))
|
2022-10-28 12:31:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return input, errors.E(op, err)
|
|
|
|
}
|
|
|
|
return input, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|