2022-07-15 11:49:53 +00:00
|
|
|
/*
|
2022-07-16 15:46:09 +00:00
|
|
|
Copyright © 2022 Matthieu Derasse <git@derasse.fr>
|
2022-07-15 11:49:53 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-07-16 22:16:29 +00:00
|
|
|
"os"
|
2022-08-03 19:59:33 +00:00
|
|
|
"os/exec"
|
2022-08-02 20:30:46 +00:00
|
|
|
"path/filepath"
|
2022-07-25 20:20:10 +00:00
|
|
|
"strings"
|
2022-07-16 22:16:29 +00:00
|
|
|
|
2022-08-01 19:47:46 +00:00
|
|
|
"github.com/juju/errors"
|
2022-07-15 11:49:53 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-08-01 19:47:46 +00:00
|
|
|
"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"
|
|
|
|
|
2022-07-15 11:49:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-08-03 11:17:15 +00:00
|
|
|
// initCmd represents the init command.
|
2022-07-15 11:49:53 +00:00
|
|
|
var initCmd = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize a new project",
|
|
|
|
Long: `Initialize a new project by creating a new directory and:
|
|
|
|
- Initialize a Git Repository
|
|
|
|
- Create a Go-Swagger Struct
|
|
|
|
- Add default routes
|
|
|
|
- Generate the API.
|
|
|
|
|
|
|
|
That command is Interactive or can be control through args`,
|
|
|
|
Run: runInitAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(initCmd)
|
|
|
|
}
|
|
|
|
|
2022-07-16 22:16:29 +00:00
|
|
|
// runInitAction
|
|
|
|
// Steps:
|
2022-08-03 11:17:15 +00:00
|
|
|
// 1 - Check that we have the dependencies.
|
|
|
|
// 2 - Check that we are not in project directory.
|
|
|
|
// 3 - Ask info to the user.
|
2022-07-15 11:49:53 +00:00
|
|
|
func runInitAction(cmd *cobra.Command, args []string) {
|
|
|
|
log.Debugf("Starting command Init")
|
|
|
|
|
2022-07-25 20:20:10 +00:00
|
|
|
log.Debugf("Checking dependencies")
|
2022-07-15 11:49:53 +00:00
|
|
|
|
2022-07-16 22:16:29 +00:00
|
|
|
if !checkDependencies() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current path
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to get current path. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Working in directory: %s", currentPath)
|
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
// Check if we are in gouick directory
|
|
|
|
log.Debug("Checking if we are in the same directory as Gouick")
|
2022-07-16 22:16:29 +00:00
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
isGouickDir, err := helpers.IsGouickDirectory(currentPath)
|
2022-07-16 22:16:29 +00:00
|
|
|
if err != nil {
|
2022-07-25 20:43:53 +00:00
|
|
|
log.Errorf("Fail to check if we are in gouick directory. The following error happen: %s", err.Error())
|
2022-07-16 22:16:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
if isGouickDir {
|
|
|
|
log.Error("You cannot initialize a new project in the same dir than gouick\nGouick should be added to your path")
|
2022-07-16 22:16:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
// Check if we are in a gouick project
|
|
|
|
log.Debug("Checking if we are in a gouick project")
|
2022-07-16 22:16:29 +00:00
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
isGouickProject, err := helpers.IsGouickProject(currentPath)
|
2022-07-16 22:16:29 +00:00
|
|
|
if err != nil {
|
2022-07-25 20:43:53 +00:00
|
|
|
log.Errorf("Fail to check if we are in a gouick project. The following error happen: %s", err.Error())
|
2022-07-16 22:16:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
if isGouickProject {
|
|
|
|
log.Error("You cannot initialize a new project in an already gouick project")
|
2022-07-16 22:16:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:20:00 +00:00
|
|
|
// let's start to work on project configuration
|
|
|
|
config := &models.Config{}
|
|
|
|
|
|
|
|
// ask which API type we want to use.
|
2022-08-03 11:17:15 +00:00
|
|
|
possibleAPITypes := make([]string, 0)
|
2022-08-02 20:33:47 +00:00
|
|
|
for _, apiType := range models.GetListOfAPITypeName() {
|
|
|
|
possibleAPITypes = append(possibleAPITypes, string(apiType))
|
2022-07-25 20:20:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:47 +00:00
|
|
|
log.Infof("Which kind of API do you want to init (possible values: %s)", strings.Join(possibleAPITypes, ", "))
|
|
|
|
apiTypeName := helpers.APITypeNameInput()
|
2022-07-25 20:57:00 +00:00
|
|
|
|
|
|
|
log.Debugf("Using api type : %s", string(apiTypeName))
|
|
|
|
|
2022-08-02 20:33:47 +00:00
|
|
|
apiType, err := api_types.GetAPIType(apiTypeName)
|
2022-07-25 20:57:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Impossible to load that API Type generator")
|
|
|
|
}
|
2022-07-25 20:20:10 +00:00
|
|
|
|
2022-08-05 14:20:00 +00:00
|
|
|
config.ProjectType = apiTypeName
|
|
|
|
|
2022-08-01 21:55:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ask project directory
|
|
|
|
log.Info("Name of the project directory:")
|
2022-08-05 14:20:00 +00:00
|
|
|
projectDirectory := helpers.AlphanumericalInput()
|
2022-08-01 21:55:50 +00:00
|
|
|
|
|
|
|
// ask project name
|
|
|
|
log.Info("Name of the project:")
|
2022-08-05 14:20:00 +00:00
|
|
|
config.ProjectName = helpers.StringInput()
|
2022-08-01 21:55:50 +00:00
|
|
|
|
|
|
|
// ask project description
|
|
|
|
log.Info("Description of the project:")
|
2022-08-05 14:20:00 +00:00
|
|
|
config.ProjectDescription = helpers.StringInput()
|
2022-08-01 21:55:50 +00:00
|
|
|
|
|
|
|
// ask project owner
|
|
|
|
log.Info("Mail address of the developer team:")
|
2022-08-05 14:20:00 +00:00
|
|
|
config.ProjectOwner = helpers.StringInput()
|
2022-08-01 21:55:50 +00:00
|
|
|
|
2022-08-01 19:47:46 +00:00
|
|
|
// launch GetInitializeUserInput from selected api type
|
|
|
|
log.Debug("Get user input for the selected API type")
|
|
|
|
|
2022-08-05 14:20:00 +00:00
|
|
|
config, err = apiType.GetInitializeUserInput(config)
|
2022-08-01 19:47:46 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:20:00 +00:00
|
|
|
projectDirPath := filepath.Join(currentPath, projectDirectory)
|
2022-08-02 20:30:46 +00:00
|
|
|
|
|
|
|
log.Debugf("Our project directory path is: %s", projectDirPath)
|
|
|
|
|
|
|
|
// create project directory
|
2022-08-03 19:59:33 +00:00
|
|
|
log.Infof("Creating project directory")
|
2022-08-02 20:30:46 +00:00
|
|
|
|
|
|
|
dirCreated, err := helpers.CheckAndCreateDir(projectDirPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to create project directory. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dirCreated {
|
|
|
|
log.Error("A directory already exist with your project directory name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:59:33 +00:00
|
|
|
// Move to project directory.
|
2022-08-05 14:20:00 +00:00
|
|
|
log.Infof("Moving to the project directory: %s", projectDirectory)
|
2022-08-03 19:59:33 +00:00
|
|
|
|
2022-08-02 20:30:46 +00:00
|
|
|
err = os.Chdir(projectDirPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to move to project directory. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:59:33 +00:00
|
|
|
// Execute git init.
|
|
|
|
log.Info("Initializing Git")
|
|
|
|
|
|
|
|
gitinit := exec.Command("git", "init")
|
|
|
|
_, err = gitinit.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to git init. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Creating gouick configuration file")
|
2022-08-05 14:20:00 +00:00
|
|
|
err = helpers.WriteConfig(projectDirPath, config)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to write configuration. The following error happen: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-08-03 19:59:33 +00:00
|
|
|
|
|
|
|
log.Info("Creating API skeleton")
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|