graphql-engine/cli/util/prompt.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
1.8 KiB
Go
Raw Normal View History

package util
import (
2021-09-07 16:33:58 +03:00
"github.com/AlecAivazis/survey/v2"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
)
2021-09-07 16:33:58 +03:00
func GetYesNoPrompt(message string) (promptResp bool, err error) {
var op errors.Op = "util.GetYesNoPrompt"
2021-09-07 16:33:58 +03:00
prompt := &survey.Confirm{
Message: message,
Default: true,
}
2021-09-07 16:33:58 +03:00
err = survey.AskOne(prompt, &promptResp)
if err != nil {
return promptResp, errors.E(op, err)
}
return promptResp, nil
}
func GetSelectPrompt(message string, options []string) (selection string, err error) {
var op errors.Op = "util.GetSelectPrompt"
2021-09-07 16:33:58 +03:00
prompt := &survey.Select{
Message: message,
Options: options,
}
2021-09-07 16:33:58 +03:00
err = survey.AskOne(prompt, &selection)
if err != nil {
return selection, errors.E(op, err)
}
return selection, nil
}
func GetInputPrompt(message string) (input string, err error) {
var op errors.Op = "util.GetInputPrompt"
2021-09-07 16:33:58 +03:00
prompt := &survey.Input{
Message: message,
}
2021-09-07 16:33:58 +03:00
err = survey.AskOne(prompt, &input)
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) {
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)
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 {
var op errors.Op = "util.validateDirPath"
2021-09-07 16:33:58 +03:00
err := FSCheckIfDirPathExists(a.(string))
if err != nil {
return errors.E(op, err)
}
return nil
}
func GetFSPathPrompt(message string, def string) (input string, err error) {
var op errors.Op = "util.GetFSPathPrompt"
2021-09-07 16:33:58 +03:00
prompt := &survey.Input{
Message: message,
Default: def,
}
2021-09-07 16:33:58 +03:00
err = survey.AskOne(prompt, &input, survey.WithValidator(validateDirPath))
if err != nil {
return input, errors.E(op, err)
}
return input, nil
}