/* Copyright © 2022 Matthieu Derasse */ package cmd import ( "os" "strings" "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" ) // initCmd represents the init command 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) } // runInitAction // Steps: // 1 - Check that we have the dependencies // 2 - Check that we are not in project directory // 3 - Ask info to the user func runInitAction(cmd *cobra.Command, args []string) { log.Debugf("Starting command Init") log.Debugf("Checking dependencies") 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) // Check if we are in gouick directory log.Debug("Checking if we are in the same directory as Gouick") isGouickDir, err := helpers.IsGouickDirectory(currentPath) if err != nil { log.Errorf("Fail to check if we are in gouick directory. The following error happen: %s", err.Error()) return } if isGouickDir { log.Error("You cannot initialize a new project in the same dir than gouick\nGouick should be added to your path") return } // 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 } if isGouickProject { log.Error("You cannot initialize a new project in an already gouick project") return } // ask which API type we want to use var possibleApiTypes []string for _, apiType := range models.GetListOfApiTypeName() { possibleApiTypes = append(possibleApiTypes, string(apiType)) } log.Infof("Which kind of API do you want to init (possible values: %s)", strings.Join(possibleApiTypes, ", ")) apiTypeName := helpers.ApiTypeNameInput() log.Debugf("Using api type : %s", string(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 // 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 // } }