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-09 15:53:40 +00:00
|
|
|
"git.home.m-and-m.ovh/mderasse/gouick/models"
|
2022-07-25 20:57:00 +00:00
|
|
|
|
2022-07-15 11:49:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// YesOrNoInput ask user for a yes or no reply and try until we get a possible answer.
|
2022-07-15 11:49:53 +00:00
|
|
|
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-03 13:36:13 +00:00
|
|
|
// AlphanumericalInput ask user for a alphanumerical string and try until we get a possible answer.
|
2022-08-01 21:55:50 +00:00
|
|
|
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.")
|
2022-08-09 21:37:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return userInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlphanumericalAndSpaceInput ask user for a alphanumerical+space string and try until we get a possible answer.
|
|
|
|
func AlphanumericalAndSpaceInput() 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\-\_\s]*$`).MatchString(userInput)
|
|
|
|
|
|
|
|
if !is_alphanumeric {
|
|
|
|
log.Warn("Please use a-z, A-Z, 0-9, - , space or _ characters only.")
|
|
|
|
continue
|
2022-08-01 21:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return userInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// StringInput ask user for a string and try until we get a possible answer.
|
2022-08-01 19:47:46 +00:00
|
|
|
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-08-03 13:36:13 +00:00
|
|
|
// PathInput ask user for a path string and try until we get a possible answer.
|
2022-07-25 20:19:38 +00:00
|
|
|
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-03 13:36:13 +00:00
|
|
|
// APITypeNameInput ask user for a string that is a valid API Type name and try until we get a possible answer.
|
2022-08-02 20:33:47 +00:00
|
|
|
func APITypeNameInput() models.APITypeName {
|
2022-07-25 20:19:38 +00:00
|
|
|
|
2022-08-03 11:17:15 +00:00
|
|
|
possibleAPITypes := make([]string, 0)
|
2022-08-02 20:33:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|