feat(init): Improve check and user input
This commit is contained in:
parent
397540c1b1
commit
fabf8fe1eb
47
cmd/init.go
47
cmd/init.go
@ -104,37 +104,46 @@ func runInitAction(cmd *cobra.Command, args []string) {
|
||||
log.Error("Impossible to load that API Type generator")
|
||||
}
|
||||
|
||||
// XXX: split in to: checkBeforeInitialize + GetInitializeUserInput
|
||||
log.Debug("Check before init for the selected API type")
|
||||
|
||||
err = apiType.CheckInitialize()
|
||||
if err != nil && !errors.Is(err, errors.NotImplemented) {
|
||||
log.Errorf("Impossible to initialize the project. The following error happen: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize a default userInput
|
||||
userInput := models.UserInputParams{}
|
||||
|
||||
// ask project directory
|
||||
log.Info("Name of the project directory:")
|
||||
userInput.ProjectDirectory = helpers.AlphanumericalInput()
|
||||
|
||||
// ask project name
|
||||
log.Info("Name of the project:")
|
||||
userInput.ProjectName = helpers.StringInput()
|
||||
|
||||
// ask project description
|
||||
log.Info("Description of the project:")
|
||||
userInput.ProjectDescription = helpers.StringInput()
|
||||
|
||||
// ask project owner
|
||||
log.Info("Mail address of the developer team:")
|
||||
userInput.ProjectOwner = helpers.StringInput()
|
||||
|
||||
// launch GetInitializeUserInput from selected api type
|
||||
log.Debug("Get user input for the selected API type")
|
||||
|
||||
_, err = apiType.GetInitializeUserInput()
|
||||
_, err = apiType.GetInitializeUserInput(&userInput)
|
||||
if err != nil && !errors.Is(err, errors.NotImplemented) {
|
||||
log.Errorf("Fail to get all the required input. The following error happen: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// XXX:
|
||||
// Check we are in gopath or ask for the gomod name
|
||||
// Create directory
|
||||
// Move to dir
|
||||
// git init
|
||||
// create .gouick
|
||||
// move templated file
|
||||
|
||||
// if isGoProject, err := helpers.IsGoProject(); err != nil {
|
||||
// log.Error("An error occured when checking your directory.")
|
||||
// return
|
||||
// } else if isGoProject {
|
||||
// log.Error("Cannot initialize a new project because you are already in a Golang Project Directory.")
|
||||
// return
|
||||
// }
|
||||
|
||||
// if isStrapProject, err := helpers.IsStrapProject(); err != nil {
|
||||
// log.Error("An error occured when checking your directory.")
|
||||
// return
|
||||
// } else if isStrapProject {
|
||||
// log.Error("Cannot initialize a new project because you are already in a Strap Project Directory.")
|
||||
// return
|
||||
// }
|
||||
}
|
||||
|
11
helpers/api_types/base/check_initialize.go
Normal file
11
helpers/api_types/base/check_initialize.go
Normal file
@ -0,0 +1,11 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
// CheckInitialize
|
||||
func (a ApiType) CheckInitialize() error {
|
||||
|
||||
return errors.NotImplementedf("CheckInitialize not implemented for %s", a.GetName())
|
||||
}
|
@ -6,7 +6,7 @@ import (
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
|
||||
func (a ApiType) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) {
|
||||
|
||||
return nil, errors.NotImplementedf("GetInitializeUserInput not implemented for %s", a.GetName())
|
||||
}
|
||||
|
@ -1,61 +0,0 @@
|
||||
package gin_gonic
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
|
||||
"github.com/juju/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// GetInitializeUserInput
|
||||
func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
|
||||
|
||||
log.Debug("Starting Gin Gonic user input")
|
||||
|
||||
// Initialize a default userInput
|
||||
userInput := models.UserInputParams{
|
||||
DB: false,
|
||||
}
|
||||
|
||||
// Get current path
|
||||
log.Debug("Getting current location")
|
||||
|
||||
currentPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get current path. The following error happen: %s", err.Error())
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
// Check if we are in a go project already
|
||||
log.Debug("Checking if we are in a Go Project")
|
||||
|
||||
isGoProject, err := helpers.IsGoProject(currentPath)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to check if we are in a Go Project. The following error happen: %s", err.Error())
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
if isGoProject {
|
||||
log.Error("We already are in a golang project directory")
|
||||
return nil, errors.NotValidf("We already are in a golang project directory")
|
||||
}
|
||||
|
||||
// Check if we are in gopath
|
||||
log.Debug("Checking if we are in GoPath")
|
||||
|
||||
isInGoPath := helpers.IsInGoPath(currentPath)
|
||||
if !isInGoPath {
|
||||
|
||||
log.Debug("We are not in GoPath, ask extra info")
|
||||
goModulePath := helpers.StringInput()
|
||||
userInput.GoModuleName = &goModulePath
|
||||
}
|
||||
|
||||
log.Info("Do you want to enable database integration ?")
|
||||
userInput.DB = helpers.YesOrNoInput()
|
||||
|
||||
return &userInput, nil
|
||||
}
|
42
helpers/api_types/go_swagger/check_initialize.go
Normal file
42
helpers/api_types/go_swagger/check_initialize.go
Normal file
@ -0,0 +1,42 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// CheckInitialize
|
||||
func (a ApiType) CheckInitialize() error {
|
||||
|
||||
log.Debugf("Starting %s check initialize", string(a.GetName()))
|
||||
|
||||
// Get current path
|
||||
log.Debug("Getting current location")
|
||||
|
||||
currentPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get current path. The following error happen: %s", err.Error())
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Check if we are in a go project already
|
||||
log.Debug("Checking if we are in a Go Project")
|
||||
|
||||
isGoProject, err := helpers.IsGoProject(currentPath)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to check if we are in a Go Project. The following error happen: %s", err.Error())
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if isGoProject {
|
||||
log.Error("We already are in a golang project directory")
|
||||
return errors.NotValidf("We already are in a golang project directory")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -12,14 +12,9 @@ import (
|
||||
)
|
||||
|
||||
// GetInitializeUserInput
|
||||
func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
|
||||
func (a ApiType) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) {
|
||||
|
||||
log.Debug("Starting Go Swagger user input")
|
||||
|
||||
// Initialize a default userInput
|
||||
userInput := models.UserInputParams{
|
||||
DB: false,
|
||||
}
|
||||
log.Debugf("Starting %s user input", a.GetName())
|
||||
|
||||
// Get current path
|
||||
log.Debug("Getting current location")
|
||||
@ -30,20 +25,6 @@ func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
// Check if we are in a go project already
|
||||
log.Debug("Checking if we are in a Go Project")
|
||||
|
||||
isGoProject, err := helpers.IsGoProject(currentPath)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to check if we are in a Go Project. The following error happen: %s", err.Error())
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
if isGoProject {
|
||||
log.Error("We already are in a golang project directory")
|
||||
return nil, errors.NotValidf("We already are in a golang project directory")
|
||||
}
|
||||
|
||||
// Check if we are in gopath
|
||||
log.Debug("Checking if we are in GoPath")
|
||||
|
||||
@ -52,11 +33,12 @@ func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
|
||||
|
||||
log.Debug("We are not in GoPath, ask extra info")
|
||||
goModulePath := helpers.StringInput()
|
||||
userInput.GoModuleName = &goModulePath
|
||||
log.Info("Go Module name:")
|
||||
params.GoModuleName = &goModulePath
|
||||
}
|
||||
|
||||
log.Info("Do you want to enable database integration ?")
|
||||
userInput.DB = helpers.YesOrNoInput()
|
||||
params.DB = helpers.YesOrNoInput()
|
||||
|
||||
return &userInput, nil
|
||||
return params, nil
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
|
||||
// ApiTypeInterface
|
||||
type ApiTypeInterface interface {
|
||||
CheckInitialize() error
|
||||
GetName() models.ApiTypeName
|
||||
GetInitializeUserInput() (*models.UserInputParams, error)
|
||||
GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error)
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package helpers
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
@ -36,6 +37,30 @@ func YesOrNoInput() bool {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// StringInput
|
||||
func StringInput() string {
|
||||
|
||||
@ -92,6 +117,11 @@ func ApiTypeNameInput() models.ApiTypeName {
|
||||
scanner.Scan()
|
||||
userInput := scanner.Text()
|
||||
|
||||
if userInput == "" {
|
||||
log.Warn("Empty input, try again")
|
||||
continue
|
||||
}
|
||||
|
||||
apiTypeName, err := models.NewApiTypeNameFromInput(userInput)
|
||||
if err != nil {
|
||||
log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", "))
|
||||
|
@ -2,6 +2,10 @@ package models
|
||||
|
||||
// UserInputParams
|
||||
type UserInputParams struct {
|
||||
GoModuleName *string
|
||||
DB bool
|
||||
DB bool
|
||||
GoModuleName *string
|
||||
ProjectDescription string
|
||||
ProjectDirectory string
|
||||
ProjectName string
|
||||
ProjectOwner string
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user