diff --git a/cmd/init.go b/cmd/init.go index e94ab53..29223e3 100644 --- a/cmd/init.go +++ b/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 diff --git a/helpers/api_type/api_type.go b/helpers/api_type/api_type.go deleted file mode 100644 index 11678f4..0000000 --- a/helpers/api_type/api_type.go +++ /dev/null @@ -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") -} diff --git a/helpers/api_type/base/get_user_input.go b/helpers/api_type/base/get_user_input.go deleted file mode 100644 index 966d6d8..0000000 --- a/helpers/api_type/base/get_user_input.go +++ /dev/null @@ -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 -} diff --git a/helpers/api_type/interface.go b/helpers/api_type/interface.go deleted file mode 100644 index 535c426..0000000 --- a/helpers/api_type/interface.go +++ /dev/null @@ -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) -} diff --git a/helpers/api_types/api_type.go b/helpers/api_types/api_type.go new file mode 100644 index 0000000..fcb4435 --- /dev/null +++ b/helpers/api_types/api_type.go @@ -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") +} diff --git a/helpers/api_type/base/api_type.go b/helpers/api_types/base/api_type.go similarity index 100% rename from helpers/api_type/base/api_type.go rename to helpers/api_types/base/api_type.go diff --git a/helpers/api_types/base/get_name.go b/helpers/api_types/base/get_name.go new file mode 100644 index 0000000..d44983c --- /dev/null +++ b/helpers/api_types/base/get_name.go @@ -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 +} diff --git a/helpers/api_types/base/get_user_input.go b/helpers/api_types/base/get_user_input.go new file mode 100644 index 0000000..b44fce7 --- /dev/null +++ b/helpers/api_types/base/get_user_input.go @@ -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()) +} diff --git a/helpers/api_type/gin_gonic/api_type.go b/helpers/api_types/gin_gonic/api_type.go similarity index 50% rename from helpers/api_type/gin_gonic/api_type.go rename to helpers/api_types/gin_gonic/api_type.go index b393d99..691774a 100644 --- a/helpers/api_type/gin_gonic/api_type.go +++ b/helpers/api_types/gin_gonic/api_type.go @@ -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 { diff --git a/helpers/api_types/gin_gonic/get_name.go b/helpers/api_types/gin_gonic/get_name.go new file mode 100644 index 0000000..ca1eacf --- /dev/null +++ b/helpers/api_types/gin_gonic/get_name.go @@ -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 +} diff --git a/helpers/api_types/gin_gonic/get_user_input.go b/helpers/api_types/gin_gonic/get_user_input.go new file mode 100644 index 0000000..c861f39 --- /dev/null +++ b/helpers/api_types/gin_gonic/get_user_input.go @@ -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 +} diff --git a/helpers/api_type/go_swagger/api_type.go b/helpers/api_types/go_swagger/api_type.go similarity index 50% rename from helpers/api_type/go_swagger/api_type.go rename to helpers/api_types/go_swagger/api_type.go index 60b7107..41fd5e1 100644 --- a/helpers/api_type/go_swagger/api_type.go +++ b/helpers/api_types/go_swagger/api_type.go @@ -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 { diff --git a/helpers/api_types/go_swagger/get_name.go b/helpers/api_types/go_swagger/get_name.go new file mode 100644 index 0000000..0960212 --- /dev/null +++ b/helpers/api_types/go_swagger/get_name.go @@ -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 +} diff --git a/helpers/api_types/go_swagger/get_user_input.go b/helpers/api_types/go_swagger/get_user_input.go new file mode 100644 index 0000000..bbfb348 --- /dev/null +++ b/helpers/api_types/go_swagger/get_user_input.go @@ -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 +} diff --git a/helpers/api_types/interface.go b/helpers/api_types/interface.go new file mode 100644 index 0000000..87af4e0 --- /dev/null +++ b/helpers/api_types/interface.go @@ -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) +} diff --git a/helpers/api_type/mojolicious/api_type.go b/helpers/api_types/mojolicious/api_type.go similarity index 50% rename from helpers/api_type/mojolicious/api_type.go rename to helpers/api_types/mojolicious/api_type.go index 005507b..ab8b63d 100644 --- a/helpers/api_type/mojolicious/api_type.go +++ b/helpers/api_types/mojolicious/api_type.go @@ -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 { diff --git a/helpers/api_types/mojolicious/get_name.go b/helpers/api_types/mojolicious/get_name.go new file mode 100644 index 0000000..61a12a5 --- /dev/null +++ b/helpers/api_types/mojolicious/get_name.go @@ -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 +} diff --git a/helpers/file.go b/helpers/file.go index 7e14ac0..5b8f054 100644 --- a/helpers/file.go +++ b/helpers/file.go @@ -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) diff --git a/helpers/golang.go b/helpers/golang.go new file mode 100644 index 0000000..b60d3e7 --- /dev/null +++ b/helpers/golang.go @@ -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) +} diff --git a/helpers/input.go b/helpers/input.go index fca7ecb..a8a0665 100644 --- a/helpers/input.go +++ b/helpers/input.go @@ -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 diff --git a/helpers/models/api_type.go b/helpers/models/api_type.go new file mode 100644 index 0000000..6c32afd --- /dev/null +++ b/helpers/models/api_type.go @@ -0,0 +1,7 @@ +package models + +// UserInputParams +type UserInputParams struct { + GoModuleName *string + DB bool +} diff --git a/helpers/api_type/enum.go b/helpers/models/api_type_name_enum.go similarity index 98% rename from helpers/api_type/enum.go rename to helpers/models/api_type_name_enum.go index c8e85e0..c037a9d 100644 --- a/helpers/api_type/enum.go +++ b/helpers/models/api_type_name_enum.go @@ -1,4 +1,4 @@ -package api_type +package models import ( "strings" diff --git a/helpers/dependencies/enum.go b/helpers/models/dependency_name_enum.go similarity index 91% rename from helpers/dependencies/enum.go rename to helpers/models/dependency_name_enum.go index 25a5a67..cae3f46 100644 --- a/helpers/dependencies/enum.go +++ b/helpers/models/dependency_name_enum.go @@ -1,4 +1,4 @@ -package dependencies +package models // DependencyName type DependencyName string diff --git a/helpers/structure/api_type.go b/helpers/structure/api_type.go deleted file mode 100644 index d2c4529..0000000 --- a/helpers/structure/api_type.go +++ /dev/null @@ -1,5 +0,0 @@ -package structure - -// UserInputParams -type UserInputParams struct { -}