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
Signed by: mderasse
GPG Key ID: 55141C777B16A705
24 changed files with 314 additions and 86 deletions

View File

@ -8,10 +8,13 @@ import (
"os" "os"
"strings" "strings"
"git.home.m-and-m.ovh/mderasse/gouick/helpers" "github.com/juju/errors"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_type"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -87,7 +90,7 @@ func runInitAction(cmd *cobra.Command, args []string) {
// ask which API type we want to use // ask which API type we want to use
var possibleApiTypes []string var possibleApiTypes []string
for _, apiType := range api_type.GetListOfApiTypeName() { for _, apiType := range models.GetListOfApiTypeName() {
possibleApiTypes = append(possibleApiTypes, string(apiType)) possibleApiTypes = append(possibleApiTypes, string(apiType))
} }
@ -96,11 +99,21 @@ func runInitAction(cmd *cobra.Command, args []string) {
log.Debugf("Using api type : %s", string(apiTypeName)) log.Debugf("Using api type : %s", string(apiTypeName))
_, err = api_type.GetApiType(apiTypeName) apiType, err := api_types.GetApiType(apiTypeName)
if err != nil { if err != nil {
log.Error("Impossible to load that API Type generator") log.Error("Impossible to load that API Type generator")
} }
// XXX: split in to: checkBeforeInitialize + GetInitializeUserInput
// launch GetInitializeUserInput from selected api type
log.Debug("Get user input for the selected API type")
_, err = apiType.GetInitializeUserInput()
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: // XXX:
// Check we are in gopath or ask for the gomod name // Check we are in gopath or ask for the gomod name
// Create directory // Create directory

View File

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

View File

@ -1,7 +0,0 @@
package base
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/structure"
func (a ApiType) GetUserInput() (*structure.UserInputParams, error) {
return nil, nil
}

View File

@ -1,8 +0,0 @@
package api_type
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/structure"
// ApiTypeInterface
type ApiTypeInterface interface {
GetUserInput() (*structure.UserInputParams, error)
}

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,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

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

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

@ -1,6 +1,6 @@
package go_swagger package go_swagger
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_type/base" import "git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types/base"
// ApiType // ApiType
type ApiType struct { type ApiType struct {

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

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

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
}

View File

@ -41,29 +41,6 @@ func IsGouickProject(path string) (bool, error) {
return true, nil return true, nil
} }
func IsGoProject(path string) (bool, error) {
exist, err := fileExists(filepath.Join(path, "go.mod"))
if err != nil {
return false, errors.Trace(err)
}
if exist {
return true, nil
}
exist, err = fileExists(filepath.Join(path, "go.sum"))
if err != nil {
return false, errors.Trace(err)
}
if exist {
return true, nil
}
return false, nil
}
func fileExists(path string) (bool, error) { func fileExists(path string) (bool, error) {
fileInfo, err := os.Stat(path) fileInfo, err := os.Stat(path)

47
helpers/golang.go Normal file
View File

@ -0,0 +1,47 @@
package helpers
import (
"os"
"path/filepath"
"strings"
"github.com/juju/errors"
)
// IsGoProject
func IsGoProject(path string) (bool, error) {
exist, err := fileExists(filepath.Join(path, "go.mod"))
if err != nil {
return false, errors.Trace(err)
}
if exist {
return true, nil
}
exist, err = fileExists(filepath.Join(path, "go.sum"))
if err != nil {
return false, errors.Trace(err)
}
if exist {
return true, nil
}
return false, nil
}
// IsInGoPath
func IsInGoPath(path string) bool {
gopath := os.Getenv("GOPATH")
if gopath == "" {
return false
}
// clean path
gopath = filepath.Clean(gopath)
return strings.HasPrefix(path, gopath)
}

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"strings" "strings"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_type" "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -36,6 +36,24 @@ func YesOrNoInput() bool {
} }
} }
// StringInput
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
}
}
// PathInput // PathInput
func PathInput() string { func PathInput() string {
@ -45,6 +63,11 @@ func PathInput() string {
scanner.Scan() scanner.Scan()
userInput := scanner.Text() userInput := scanner.Text()
if userInput == "" {
log.Warn("Empty input, try again")
continue
}
err := CheckAndCreateDir(userInput) err := CheckAndCreateDir(userInput)
if err != nil { if err != nil {
log.Warnf("please, try again") log.Warnf("please, try again")
@ -56,10 +79,10 @@ func PathInput() string {
} }
// ApiTypeNameInput // ApiTypeNameInput
func ApiTypeNameInput() api_type.ApiTypeName { func ApiTypeNameInput() models.ApiTypeName {
var possibleApiTypes []string var possibleApiTypes []string
for _, apiType := range api_type.GetListOfApiTypeName() { for _, apiType := range models.GetListOfApiTypeName() {
possibleApiTypes = append(possibleApiTypes, string(apiType)) possibleApiTypes = append(possibleApiTypes, string(apiType))
} }
@ -69,7 +92,7 @@ func ApiTypeNameInput() api_type.ApiTypeName {
scanner.Scan() scanner.Scan()
userInput := scanner.Text() userInput := scanner.Text()
apiTypeName, err := api_type.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, ", "))
continue continue

View File

@ -0,0 +1,7 @@
package models
// UserInputParams
type UserInputParams struct {
GoModuleName *string
DB bool
}

View File

@ -1,4 +1,4 @@
package api_type package models
import ( import (
"strings" "strings"

View File

@ -1,4 +1,4 @@
package dependencies package models
// DependencyName // DependencyName
type DependencyName string type DependencyName string

View File

@ -1,5 +0,0 @@
package structure
// UserInputParams
type UserInputParams struct {
}