gouick/helpers/input.go
2022-07-28 22:03:57 +00:00

81 lines
1.4 KiB
Go

package helpers
import (
"bufio"
"os"
"strings"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_type"
log "github.com/sirupsen/logrus"
)
// YesOrNoInput
func YesOrNoInput() bool {
scanner := bufio.NewScanner(os.Stdin)
for {
scanner.Scan()
userInput := scanner.Text()
lUserInput := strings.ToLower(userInput)
for _, positiveAnswer := range []string{"yes", "y", "1", "true"} {
if lUserInput == positiveAnswer {
return true
}
}
for _, negativeAnswer := range []string{"no", "n", "0", "false"} {
if lUserInput == negativeAnswer {
return false
}
}
log.Info("Expecting a yes or no answer, Try again")
}
}
// PathInput
func PathInput() string {
scanner := bufio.NewScanner(os.Stdin)
for {
scanner.Scan()
userInput := scanner.Text()
err := CheckAndCreateDir(userInput)
if err != nil {
log.Warnf("please, try again")
continue
}
return userInput
}
}
// ApiTypeNameInput
func ApiTypeNameInput() api_type.ApiTypeName {
var possibleApiTypes []string
for _, apiType := range api_type.GetListOfApiTypeName() {
possibleApiTypes = append(possibleApiTypes, string(apiType))
}
scanner := bufio.NewScanner(os.Stdin)
for {
scanner.Scan()
userInput := scanner.Text()
apiTypeName, err := api_type.NewApiTypeNameFromInput(userInput)
if err != nil {
log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", "))
continue
}
return apiTypeName
}
}