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 (
|
|
|
|
"fmt"
|
2022-09-20 21:49:37 +00:00
|
|
|
"os"
|
2022-07-15 11:49:53 +00:00
|
|
|
|
2022-11-26 22:36:00 +00:00
|
|
|
"git.dev.m-and-m.ovh/mderasse/gouick/helpers"
|
|
|
|
"git.dev.m-and-m.ovh/mderasse/gouick/models"
|
2022-07-15 11:49:53 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-09-20 21:49:37 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-07-15 11:49:53 +00:00
|
|
|
)
|
|
|
|
|
2022-08-03 11:17:15 +00:00
|
|
|
// generateCmd represents the generate command.
|
2022-07-15 11:49:53 +00:00
|
|
|
var generateCmd = &cobra.Command{
|
|
|
|
Use: "generate",
|
|
|
|
Short: "Generate all the files (API, DB, ...)",
|
|
|
|
Long: `Generate will automatically create all the files of the Project.
|
2022-11-11 17:52:44 +00:00
|
|
|
You can also generate files for each 'module' by using the command bellows`,
|
|
|
|
Run: runGenerate,
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(generateCmd)
|
|
|
|
}
|
2022-09-20 21:49:37 +00:00
|
|
|
|
2022-11-11 17:52:44 +00:00
|
|
|
// runGenerate will launch sub generate function for the current project.
|
|
|
|
func runGenerate(cmd *cobra.Command, args []string) {
|
|
|
|
runGenerateAPI(cmd, args)
|
|
|
|
runGenerateDocker(cmd, args)
|
|
|
|
runGenerateHelm(cmd, args)
|
|
|
|
runGenerateLauncher(cmd, args)
|
|
|
|
|
|
|
|
// Load config to check if we need to generat DB
|
|
|
|
_, config, err := loadProjectAndConfig()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Features.DatabaseModels.Enabled {
|
|
|
|
runGenerateDB(cmd, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 21:49:37 +00:00
|
|
|
func loadProjectAndConfig() (string, *models.Config, error) {
|
|
|
|
|
|
|
|
log.Debugf("Checking dependencies")
|
|
|
|
|
|
|
|
if !checkDependencies() {
|
|
|
|
return "", nil, fmt.Errorf("failed to check dependencies")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Working in directory: %s", currentPath)
|
|
|
|
|
|
|
|
// Check if we are in a gouick project
|
|
|
|
log.Debug("Checking if we are in a gouick project")
|
|
|
|
|
|
|
|
isGouickProject, err := helpers.IsGouickProject(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fail to check if we are in a gouick project. The following error happen: %s", err.Error())
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isGouickProject {
|
|
|
|
log.Error("That command need to be executed in a Gouick Project Directory")
|
|
|
|
return "", nil, fmt.Errorf("that command need to be executed in a gouick project directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading project configuration
|
|
|
|
log.Debug("Reading project configuration")
|
|
|
|
|
|
|
|
config, err := helpers.ReadConfig(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to read configuration file. The following error happen: %s", err.Error())
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Using api type : %s", string(config.ProjectType))
|
|
|
|
|
|
|
|
return currentPath, config, nil
|
|
|
|
}
|