feat(init): Improve check and user input

This commit is contained in:
Matthieu 'JP' DERASSE 2022-08-01 21:55:50 +00:00
parent 397540c1b1
commit fabf8fe1eb
Signed by: mderasse
GPG Key ID: 55141C777B16A705
9 changed files with 126 additions and 108 deletions

View File

@ -104,37 +104,46 @@ func runInitAction(cmd *cobra.Command, args []string) {
log.Error("Impossible to load that API Type generator") 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 // launch GetInitializeUserInput from selected api type
log.Debug("Get user input for the 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) { if err != nil && !errors.Is(err, errors.NotImplemented) {
log.Errorf("Fail to get all the required input. The following error happen: %s", err.Error()) log.Errorf("Fail to get all the required input. The following error happen: %s", err.Error())
return return
} }
// XXX: // XXX:
// Check we are in gopath or ask for the gomod name
// Create directory // Create directory
// Move to dir // Move to dir
// git init // git init
// create .gouick // create .gouick
// move templated file // 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
// }
} }

View 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())
}

View File

@ -6,7 +6,7 @@ import (
"github.com/juju/errors" "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()) return nil, errors.NotImplementedf("GetInitializeUserInput not implemented for %s", a.GetName())
} }

View File

@ -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
}

View 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
}

View File

@ -12,14 +12,9 @@ import (
) )
// GetInitializeUserInput // 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") log.Debugf("Starting %s user input", a.GetName())
// Initialize a default userInput
userInput := models.UserInputParams{
DB: false,
}
// Get current path // Get current path
log.Debug("Getting current location") log.Debug("Getting current location")
@ -30,20 +25,6 @@ func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
return nil, errors.Trace(err) 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 // Check if we are in gopath
log.Debug("Checking 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") log.Debug("We are not in GoPath, ask extra info")
goModulePath := helpers.StringInput() goModulePath := helpers.StringInput()
userInput.GoModuleName = &goModulePath log.Info("Go Module name:")
params.GoModuleName = &goModulePath
} }
log.Info("Do you want to enable database integration ?") log.Info("Do you want to enable database integration ?")
userInput.DB = helpers.YesOrNoInput() params.DB = helpers.YesOrNoInput()
return &userInput, nil return params, nil
} }

View File

@ -4,6 +4,7 @@ import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
// ApiTypeInterface // ApiTypeInterface
type ApiTypeInterface interface { type ApiTypeInterface interface {
CheckInitialize() error
GetName() models.ApiTypeName GetName() models.ApiTypeName
GetInitializeUserInput() (*models.UserInputParams, error) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error)
} }

View File

@ -3,6 +3,7 @@ package helpers
import ( import (
"bufio" "bufio"
"os" "os"
"regexp"
"strings" "strings"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models" "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 // StringInput
func StringInput() string { func StringInput() string {
@ -92,6 +117,11 @@ func ApiTypeNameInput() models.ApiTypeName {
scanner.Scan() scanner.Scan()
userInput := scanner.Text() userInput := scanner.Text()
if userInput == "" {
log.Warn("Empty input, try again")
continue
}
apiTypeName, err := models.NewApiTypeNameFromInput(userInput) apiTypeName, err := models.NewApiTypeNameFromInput(userInput)
if err != nil { if err != nil {
log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", ")) log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", "))

View File

@ -2,6 +2,10 @@ package models
// UserInputParams // UserInputParams
type UserInputParams struct { type UserInputParams struct {
GoModuleName *string
DB bool DB bool
GoModuleName *string
ProjectDescription string
ProjectDirectory string
ProjectName string
ProjectOwner string
} }