Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions ShowQuestion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import (
"errors"
"fmt"
"strings"

"github.com/manifoldco/promptui"
)

func ShowQuestion(message string) bool {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extended this with
func ShowQuestion(message string, defaultValueFlag bool) bool
then use that flag to set Default to either y or n so I can have different question types.

allowedValues := [...]string{"y", "yes", "no", "n"}

validate := func(input string) error {
for _, value := range allowedValues {
if strings.ToLower(input) == value {
return nil
}
}
return errors.New(fmt.Sprintf("Number should be one of the values %v", allowedValues))
}

prompt := promptui.Prompt{
Label: message,
Validate: validate,
Default: "y",
}

result, err := prompt.Run()
if err != nil {
return ShowQuestion(message)
}

result = strings.ToLower(result)

return result == "y" || result == "yes"
}