feat(init): Get User input for config + small refacto

This commit is contained in:
Matthieu 'JP' DERASSE
2022-08-01 19:47:46 +00:00
parent 13903329c8
commit 9103e7ba60
24 changed files with 314 additions and 86 deletions

View File

@ -0,0 +1,31 @@
package api_types
import (
"github.com/juju/errors"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/gin_gonic"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/go_swagger"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/mojolicious"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
)
func GetApiType(in models.ApiTypeName) (ApiTypeInterface, error) {
if in == "" {
return nil, errors.BadRequestf("missing parameter")
}
if !in.IsValid() {
return nil, errors.NotValidf("invalid parameter")
}
switch in {
case models.ApyTypeName_GIN_GONIC:
return gin_gonic.ApiType{}, nil
case models.ApyTypeName_GO_SWAGGER:
return go_swagger.ApiType{}, nil
case models.ApyTypeName_MOJOLICIOUS:
return mojolicious.ApiType{}, nil
}
return nil, errors.NotFoundf("Unknown Api Type")
}

View File

@ -0,0 +1,5 @@
package base
// ApiType
type ApiType struct {
}

View File

@ -0,0 +1,9 @@
package base
import (
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
)
func (a ApiType) GetName() models.ApiTypeName {
return models.ApyTypeName_NULL
}

View File

@ -0,0 +1,12 @@
package base
import (
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
"github.com/juju/errors"
)
func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
return nil, errors.NotImplementedf("GetInitializeUserInput not implemented for %s", a.GetName())
}

View File

@ -0,0 +1,8 @@
package gin_gonic
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// ApiType
type ApiType struct {
base.ApiType
}

View File

@ -0,0 +1,9 @@
package gin_gonic
import (
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
)
func (a ApiType) GetName() models.ApiTypeName {
return models.ApyTypeName_GIN_GONIC
}

View File

@ -0,0 +1,61 @@
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,8 @@
package go_swagger
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// ApiType
type ApiType struct {
base.ApiType
}

View File

@ -0,0 +1,9 @@
package go_swagger
import (
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
)
func (a ApiType) GetName() models.ApiTypeName {
return models.ApyTypeName_GO_SWAGGER
}

View File

@ -0,0 +1,62 @@
package go_swagger
import (
"os"
"github.com/juju/errors"
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
log "github.com/sirupsen/logrus"
)
// GetInitializeUserInput
func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) {
log.Debug("Starting Go Swagger 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,9 @@
package api_types
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
// ApiTypeInterface
type ApiTypeInterface interface {
GetName() models.ApiTypeName
GetInitializeUserInput() (*models.UserInputParams, error)
}

View File

@ -0,0 +1,8 @@
package mojolicious
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// ApiType
type ApiType struct {
base.ApiType
}

View File

@ -0,0 +1,9 @@
package mojolicious
import (
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
)
func (a ApiType) GetName() models.ApiTypeName {
return models.ApyTypeName_MOJOLICIOUS
}