2022-07-15 11:49:53 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
2022-07-25 20:19:38 +00:00
|
|
|
"bufio"
|
|
|
|
"os"
|
2022-08-01 21:55:50 +00:00
|
|
|
"regexp"
|
2022-07-15 11:49:53 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-08-01 19:47:46 +00:00
|
|
|
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
2022-07-25 20:57:00 +00:00
|
|
|
|
2022-07-15 11:49:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// YesOrNoInput
|
|
|
|
func YesOrNoInput() bool {
|
|
|
|
|
2022-07-25 20:19:38 +00:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
2022-07-15 11:49:53 +00:00
|
|
|
for {
|
|
|
|
|
2022-07-25 20:19:38 +00:00
|
|
|
scanner.Scan()
|
|
|
|
userInput := scanner.Text()
|
2022-07-15 11:49:53 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:55:50 +00:00
|
|
|
// AlphanumericalInput
|
|
|
|
func AlphanumericalInput() string {
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for {
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
userInput := scanner.Text()
|
|
|
|
|
|
|
|
if userInput == "" {
|
|
|
|
log.Warn("Empty input, try again")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
is_alphanumeric := regexp.MustCompile(`^[a-zA-Z0-9\-\_]*$`).MatchString(userInput)
|
|
|
|
|
|
|
|
if !is_alphanumeric {
|
|
|
|
log.Warn("Please use a-z, A-Z, 0-9 and - or _ characters only.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return userInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 19:47:46 +00:00
|
|
|
// StringInput
|
|
|
|
func StringInput() string {
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for {
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
userInput := scanner.Text()
|
|
|
|
|
|
|
|
if userInput == "" {
|
|
|
|
log.Warn("Empty input, try again")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return userInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:19:38 +00:00
|
|
|
// PathInput
|
|
|
|
func PathInput() string {
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
2022-07-15 11:49:53 +00:00
|
|
|
for {
|
|
|
|
|
2022-07-25 20:19:38 +00:00
|
|
|
scanner.Scan()
|
|
|
|
userInput := scanner.Text()
|
|
|
|
|
2022-08-01 19:47:46 +00:00
|
|
|
if userInput == "" {
|
|
|
|
log.Warn("Empty input, try again")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:30:46 +00:00
|
|
|
_, err := CheckAndCreateDir(userInput)
|
2022-07-15 11:49:53 +00:00
|
|
|
if err != nil {
|
2022-07-25 20:19:38 +00:00
|
|
|
log.Warnf("please, try again")
|
2022-07-15 11:49:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:19:38 +00:00
|
|
|
return userInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:47 +00:00
|
|
|
// APITypeNameInput
|
|
|
|
func APITypeNameInput() models.APITypeName {
|
2022-07-25 20:19:38 +00:00
|
|
|
|
2022-08-02 20:33:47 +00:00
|
|
|
var possibleAPITypes []string
|
|
|
|
for _, apiType := range models.GetListOfAPITypeName() {
|
|
|
|
possibleAPITypes = append(possibleAPITypes, string(apiType))
|
2022-07-25 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for {
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
userInput := scanner.Text()
|
|
|
|
|
2022-08-01 21:55:50 +00:00
|
|
|
if userInput == "" {
|
|
|
|
log.Warn("Empty input, try again")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:47 +00:00
|
|
|
apiTypeName, err := models.NewAPITypeNameFromInput(userInput)
|
2022-07-15 11:49:53 +00:00
|
|
|
if err != nil {
|
2022-08-02 20:33:47 +00:00
|
|
|
log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleAPITypes, ", "))
|
2022-07-15 11:49:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:19:38 +00:00
|
|
|
return apiTypeName
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
}
|