diff --git a/cmd/init.go b/cmd/init.go index 29223e3..30329e5 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -104,37 +104,46 @@ func runInitAction(cmd *cobra.Command, args []string) { 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 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) { 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 // Move to dir // git init // create .gouick // 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 - // } } diff --git a/helpers/api_types/base/check_initialize.go b/helpers/api_types/base/check_initialize.go new file mode 100644 index 0000000..2a5a329 --- /dev/null +++ b/helpers/api_types/base/check_initialize.go @@ -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()) +} diff --git a/helpers/api_types/base/get_user_input.go b/helpers/api_types/base/get_user_input.go index b44fce7..66ffe35 100644 --- a/helpers/api_types/base/get_user_input.go +++ b/helpers/api_types/base/get_user_input.go @@ -6,7 +6,7 @@ import ( "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()) } diff --git a/helpers/api_types/gin_gonic/get_user_input.go b/helpers/api_types/gin_gonic/get_user_input.go deleted file mode 100644 index c861f39..0000000 --- a/helpers/api_types/gin_gonic/get_user_input.go +++ /dev/null @@ -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 -} diff --git a/helpers/api_types/go_swagger/check_initialize.go b/helpers/api_types/go_swagger/check_initialize.go new file mode 100644 index 0000000..c76585c --- /dev/null +++ b/helpers/api_types/go_swagger/check_initialize.go @@ -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 +} diff --git a/helpers/api_types/go_swagger/get_user_input.go b/helpers/api_types/go_swagger/get_user_input.go index bbfb348..aa8d2d4 100644 --- a/helpers/api_types/go_swagger/get_user_input.go +++ b/helpers/api_types/go_swagger/get_user_input.go @@ -12,14 +12,9 @@ import ( ) // 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") - - // Initialize a default userInput - userInput := models.UserInputParams{ - DB: false, - } + log.Debugf("Starting %s user input", a.GetName()) // Get current path log.Debug("Getting current location") @@ -30,20 +25,6 @@ func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, 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") @@ -52,11 +33,12 @@ func (a ApiType) GetInitializeUserInput() (*models.UserInputParams, error) { log.Debug("We are not in GoPath, ask extra info") goModulePath := helpers.StringInput() - userInput.GoModuleName = &goModulePath + log.Info("Go Module name:") + params.GoModuleName = &goModulePath } log.Info("Do you want to enable database integration ?") - userInput.DB = helpers.YesOrNoInput() + params.DB = helpers.YesOrNoInput() - return &userInput, nil + return params, nil } diff --git a/helpers/api_types/interface.go b/helpers/api_types/interface.go index 87af4e0..6655b51 100644 --- a/helpers/api_types/interface.go +++ b/helpers/api_types/interface.go @@ -4,6 +4,7 @@ import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models" // ApiTypeInterface type ApiTypeInterface interface { + CheckInitialize() error GetName() models.ApiTypeName - GetInitializeUserInput() (*models.UserInputParams, error) + GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) } diff --git a/helpers/input.go b/helpers/input.go index a8a0665..df3957e 100644 --- a/helpers/input.go +++ b/helpers/input.go @@ -3,6 +3,7 @@ package helpers import ( "bufio" "os" + "regexp" "strings" "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 func StringInput() string { @@ -92,6 +117,11 @@ func ApiTypeNameInput() models.ApiTypeName { scanner.Scan() userInput := scanner.Text() + if userInput == "" { + log.Warn("Empty input, try again") + continue + } + apiTypeName, err := models.NewApiTypeNameFromInput(userInput) if err != nil { log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", ")) diff --git a/helpers/models/api_type.go b/helpers/models/api_type.go index 6c32afd..2bd22f2 100644 --- a/helpers/models/api_type.go +++ b/helpers/models/api_type.go @@ -2,6 +2,10 @@ package models // UserInputParams type UserInputParams struct { - GoModuleName *string - DB bool + DB bool + GoModuleName *string + ProjectDescription string + ProjectDirectory string + ProjectName string + ProjectOwner string }