gouick/cmd/generate.go

92 lines
2.3 KiB
Go
Raw Permalink Normal View History

/*
Copyright © 2022 Matthieu Derasse <git@derasse.fr>
*/
package cmd
import (
"fmt"
"os"
"git.dev.m-and-m.ovh/mderasse/gouick/helpers"
"git.dev.m-and-m.ovh/mderasse/gouick/models"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
// generateCmd represents the generate command.
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate all the files (API, DB, ...)",
Long: `Generate will automatically create all the files of the Project.
You can also generate files for each 'module' by using the command bellows`,
Run: runGenerate,
}
func init() {
rootCmd.AddCommand(generateCmd)
}
// 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)
}
}
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
}