refato(global); Fix error on input, better log
This commit is contained in:
parent
eb8befc77c
commit
3536298742
@ -2,9 +2,11 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
|
|
||||||
|
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckInitialize will do preliminary check before initializing a new project.
|
// CheckInitialize will do preliminary check before initializing a new project.
|
||||||
func (a APIType) CheckInitialize() error {
|
func (a APIType) CheckInitialize() error {
|
||||||
return errors.NotImplementedf("CheckInitialize not implemented for %s", a.GetName())
|
return errors.NotImplementedf("%s not implemented for %s", helpers.GetCurrentFuncName(), a.GetName())
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package base
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.home.m-and-m.ovh/mderasse/gouick/models"
|
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
|
|
||||||
|
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||||
|
"git.home.m-and-m.ovh/mderasse/gouick/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetInitializeUserInput will ask user to provide information that allow initialization of a project.
|
// GetInitializeUserInput will ask user to provide information that allow initialization of a project.
|
||||||
func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config, error) {
|
func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config, error) {
|
||||||
return nil, errors.NotImplementedf("GetInitializeUserInput not implemented for %s", a.GetName())
|
return nil, errors.NotImplementedf("%s not implemented for %s", helpers.GetCurrentFuncName(), a.GetName())
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// CheckInitialize will do preliminary check before initializing a new project.
|
// CheckInitialize will do preliminary check before initializing a new project.
|
||||||
func (a APIType) CheckInitialize() error {
|
func (a APIType) CheckInitialize() error {
|
||||||
log.Debugf("Starting %s check initialize", string(a.GetName()))
|
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||||
|
|
||||||
// Get current path
|
// Get current path
|
||||||
log.Debug("Getting current location")
|
log.Debug("Getting current location")
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
// GetInitializeUserInput will ask user to provide information that allow initialization of a project.
|
// GetInitializeUserInput will ask user to provide information that allow initialization of a project.
|
||||||
func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config, error) {
|
func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config, error) {
|
||||||
log.Debugf("Starting %s user input", a.GetName())
|
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||||
|
|
||||||
// Get current path
|
// Get current path
|
||||||
log.Debug("Getting current location")
|
log.Debug("Getting current location")
|
||||||
|
12
helpers/debug.go
Normal file
12
helpers/debug.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetCurrentFuncName will return the current function's name.
|
||||||
|
// It can be used for a better log debug system.
|
||||||
|
func GetCurrentFuncName() string {
|
||||||
|
pc, _, _, _ := runtime.Caller(1)
|
||||||
|
return runtime.FuncForPC(pc).Name()
|
||||||
|
}
|
@ -54,6 +54,32 @@ func AlphanumericalInput() string {
|
|||||||
|
|
||||||
if !is_alphanumeric {
|
if !is_alphanumeric {
|
||||||
log.Warn("Please use a-z, A-Z, 0-9 and - or _ characters only.")
|
log.Warn("Please use a-z, A-Z, 0-9 and - or _ characters only.")
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
return userInput
|
return userInput
|
||||||
|
Loading…
Reference in New Issue
Block a user