feat(init): Get User input for config + small refacto
This commit is contained in:
parent
13903329c8
commit
9103e7ba60
21
cmd/init.go
21
cmd/init.go
@ -8,10 +8,13 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_type"
|
||||
"github.com/juju/errors"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -87,7 +90,7 @@ func runInitAction(cmd *cobra.Command, args []string) {
|
||||
|
||||
// ask which API type we want to use
|
||||
var possibleApiTypes []string
|
||||
for _, apiType := range api_type.GetListOfApiTypeName() {
|
||||
for _, apiType := range models.GetListOfApiTypeName() {
|
||||
possibleApiTypes = append(possibleApiTypes, string(apiType))
|
||||
}
|
||||
|
||||
@ -96,11 +99,21 @@ func runInitAction(cmd *cobra.Command, args []string) {
|
||||
|
||||
log.Debugf("Using api type : %s", string(apiTypeName))
|
||||
|
||||
_, err = api_type.GetApiType(apiTypeName)
|
||||
apiType, err := api_types.GetApiType(apiTypeName)
|
||||
if err != nil {
|
||||
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:
|
||||
// Check we are in gopath or ask for the gomod name
|
||||
// Create directory
|
||||
|
@ -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")
|
||||
}
|
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
31
helpers/api_types/api_type.go
Normal file
31
helpers/api_types/api_type.go
Normal 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")
|
||||
}
|
9
helpers/api_types/base/get_name.go
Normal file
9
helpers/api_types/base/get_name.go
Normal 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
|
||||
}
|
12
helpers/api_types/base/get_user_input.go
Normal file
12
helpers/api_types/base/get_user_input.go
Normal 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())
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
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
|
||||
type ApiType struct {
|
9
helpers/api_types/gin_gonic/get_name.go
Normal file
9
helpers/api_types/gin_gonic/get_name.go
Normal 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
|
||||
}
|
61
helpers/api_types/gin_gonic/get_user_input.go
Normal file
61
helpers/api_types/gin_gonic/get_user_input.go
Normal 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
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
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
|
||||
type ApiType struct {
|
9
helpers/api_types/go_swagger/get_name.go
Normal file
9
helpers/api_types/go_swagger/get_name.go
Normal 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
|
||||
}
|
62
helpers/api_types/go_swagger/get_user_input.go
Normal file
62
helpers/api_types/go_swagger/get_user_input.go
Normal 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
|
||||
}
|
9
helpers/api_types/interface.go
Normal file
9
helpers/api_types/interface.go
Normal 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)
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
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
|
||||
type ApiType struct {
|
9
helpers/api_types/mojolicious/get_name.go
Normal file
9
helpers/api_types/mojolicious/get_name.go
Normal 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
|
||||
}
|
@ -41,29 +41,6 @@ func IsGouickProject(path string) (bool, error) {
|
||||
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) {
|
||||
|
||||
fileInfo, err := os.Stat(path)
|
||||
|
47
helpers/golang.go
Normal file
47
helpers/golang.go
Normal 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)
|
||||
}
|
@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
"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"
|
||||
)
|
||||
@ -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
|
||||
func PathInput() string {
|
||||
|
||||
@ -45,6 +63,11 @@ func PathInput() string {
|
||||
scanner.Scan()
|
||||
userInput := scanner.Text()
|
||||
|
||||
if userInput == "" {
|
||||
log.Warn("Empty input, try again")
|
||||
continue
|
||||
}
|
||||
|
||||
err := CheckAndCreateDir(userInput)
|
||||
if err != nil {
|
||||
log.Warnf("please, try again")
|
||||
@ -56,10 +79,10 @@ func PathInput() string {
|
||||
}
|
||||
|
||||
// ApiTypeNameInput
|
||||
func ApiTypeNameInput() api_type.ApiTypeName {
|
||||
func ApiTypeNameInput() models.ApiTypeName {
|
||||
|
||||
var possibleApiTypes []string
|
||||
for _, apiType := range api_type.GetListOfApiTypeName() {
|
||||
for _, apiType := range models.GetListOfApiTypeName() {
|
||||
possibleApiTypes = append(possibleApiTypes, string(apiType))
|
||||
}
|
||||
|
||||
@ -69,7 +92,7 @@ func ApiTypeNameInput() api_type.ApiTypeName {
|
||||
scanner.Scan()
|
||||
userInput := scanner.Text()
|
||||
|
||||
apiTypeName, err := api_type.NewApiTypeNameFromInput(userInput)
|
||||
apiTypeName, err := models.NewApiTypeNameFromInput(userInput)
|
||||
if err != nil {
|
||||
log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", "))
|
||||
continue
|
||||
|
7
helpers/models/api_type.go
Normal file
7
helpers/models/api_type.go
Normal file
@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
// UserInputParams
|
||||
type UserInputParams struct {
|
||||
GoModuleName *string
|
||||
DB bool
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package api_type
|
||||
package models
|
||||
|
||||
import (
|
||||
"strings"
|
@ -1,4 +1,4 @@
|
||||
package dependencies
|
||||
package models
|
||||
|
||||
// DependencyName
|
||||
type DependencyName string
|
@ -1,5 +0,0 @@
|
||||
package structure
|
||||
|
||||
// UserInputParams
|
||||
type UserInputParams struct {
|
||||
}
|
Loading…
Reference in New Issue
Block a user